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

869
Vistas
Jest test is failing: Cannot spy the getNextPos property because it is not a function; undefined given instead

I'm calling getNextPos from solution function and getting val,pos and dir. But my test case is failing " Cannot spy the getNextPos property because it is not a function; undefined given instead". I just want to test one function from another function and the return values. Here is app.js file

function getNextPos(grid, currPos, currDir, move) {
    const nextDir = nextDirMap[currDir][move];
    const [r, c] = currPos;
    const maxRowLength = grid.length-1;
    const maxColLength = grid[0].length-1;

    switch (nextDir) {
        case "north": {
            if (r <= 0) {
                cost = cost + (uncleardSquare * 3);
                throw new CustomException("Unable to move, there is an attempt to navigate beyond the boundaries of the site.");
            }
            return {
                val: grid[r - 1][c],
                pos: [r - 1, c],
                dir: "north"
            };
        }
}
function solution(grid, index, direction, bulldozerMove) {
    div.innerText = "";
    let currPos = index;
    let currDir = direction;
    bulldozerMove = bulldozerMove.toLowerCase();


    let {
        val,
        pos,
        dir
    } = getNextPos(grid, currPos, currDir, bulldozerMove);
}

Here is the app.test.js file

const { getNextPos, solution} = require('./app');
    
      const grid = [
        ["r", "o", "t", "t"],
        ["o", "r", "o", "t"],
        ["o", "o", "o", "t"],
      ];
    
    
    describe("solution function", () => {
      
           test('should call getNextPos', () => {
      
        const spy = jest.spyOn(solution, 'getNextPos');
      const isCalled = solution.getNextPos();
      expect(spy).toHaveBeenCalled();
      expect(isCalled).toBe(true);
    
      spy.mockRestore();
                
            
           })
    
    })
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It looks like you're mocking/spying getNextPos inside of the solution object, but in fact, solution is not an object, it's a function.

For example:

// calc.js
const calc = {
    sum: (n1, n2) => n1 + n2
}

module.exports = { calc }

then in a test file:

import { calc } from './calc.js'

const sumSpy = jest.spyOn(calc, 'sum')

it('should pass because calc.sum has been mocked', () => {
    sumSpy.mockReturnValue(20);

    expect(calc.sum(5, 5)).toBe(20)
})

that works properly because I am spying a function inside an object. Maybe you can do a refactor to move stuff to separe things into objects

about 4 years ago · Juan Pablo Isaza Denunciar
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