• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

702
Views
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();
                
            
           })
    
    })
almost 3 years ago · Juan Pablo Isaza
1 answers
Answer question

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

almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error