Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

86
Vistas
Unit testing a method that calls other methods Jest

I was thinkig a lot on WHAT to test in a method that works like an "orchestrator" and it's only job is to calls functions. So I have this BattleInteractor class

class BattleInteractor {
    constructor(opts) {
        this.battleRepository = opts.battleRepository
        this.battleEntity = opts.battleEntity
        this.battleWinner = opts.battleWinner
        this.typeFactoryService = opts.typeFactory
    }
    
    async execute(pokemonOne, pokemonTwo) {

        const battleResult = this.battle(pokemonOne, pokemonTwo)
        return battleResult
    }

    battle(pokemonOne, pokemonTwo) {
        const strategy = this.typeFactoryService.getStrategy(pokemonOne, pokemonTwo)
        const pokemonsSetted = strategy.setStats(pokemonOne, pokemonTwo)
        const battleResult = this.battleEntity.battleLogic(pokemonsSetted)
        return this.battleWinner.battleWinner(battleResult)
    }

}

module.exports = BattleInteractor

And the test file with all methods mocked:

const BattleInteractor = require('../../../../src/application/useCases/battle/battleInteractor')
describe('BattleInteractor test', () => {

    const pokemonOneMock = 'mock'
    const pokemonTwoMock = 'mock'
    const battleWinnerMock = 'mock'

    class TypeMock {
        setStats() {
            return 'mockStats'
        }
    }

    const battleInteractor = new BattleInteractor({
        battleRepository: {
            savedDatabase: () => true
        },
        typeFactory: {
            getStrategy: () => {
                return new TypeMock()
            },
        },
        battleEntity: {
            battleLogic: () => {
                return "battleLogicMock"
            }
        },
        battleWinner: {
            battleWinner: () => {
                return battleWinnerMock
            }
        },
    })


    test.only('Should return properties formatted', async () => {
        const res = await battleInteractor.execute(pokemonOneMock, pokemonTwoMock)
        expect(res.message).toBe("carnivine and naganadel have tied")
    })


})

All works fine, so I have some questions: What should I test in a method like this? Is this a unit testing or a integration testing? How necessary is it to test a class/method like this?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda