最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jest test is failing: Cannot spy the getNextPos property because it is not a function; undefined given instead - St

programmeradmin0浏览0评论

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();
                         
        })
    
    })

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();
                         
        })
    
    })
Share Improve this question edited May 12, 2023 at 16:03 zforgo 3,3162 gold badges16 silver badges28 bronze badges asked May 5, 2022 at 16:34 Anamika SinghAnamika Singh 1511 gold badge1 silver badge9 bronze badges 2
  • Do you understand how module imports and exports work in Javascript? Because you don't appear to be exporting your functions anywhere. – Nick Bailey Commented May 5, 2022 at 16:36
  • I have used module.exports = { getNextPos, solution}; in app.js – Anamika Singh Commented May 5, 2022 at 16:37
Add a ment  | 

1 Answer 1

Reset to default 3

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论