admin管理员组文章数量:1307006
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
1 Answer
Reset to default 3It 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
本文标签:
版权声明:本文标题:javascript - Jest test is failing: Cannot spy the getNextPos property because it is not a function; undefined given instead - St 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741829398a2399844.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论