开玩笑嘲笑 toHaveReturnedWith undefined
我正在学习用笑话尤其是模拟模块进行单元测试。我在 math.js 文件中使用一些数学方法编写了一些简单的模块:
const add = (a, b) => a + b;
const subtract = (a, b) => b - a;
const multiply = (a, b) => a * b;
const divide = (a, b) => b / a;
module.exports = {
add,
subtract,
multiply,
divide
}
然后我将其包含在我的主 js 中,并像这样进行模块模拟:
jest.mock('./math.js');
const math = require('./math');
test("calls math.add", () => {
math.add(1, 2);
console.log(math.add.mock);
expect(math.add).toHaveBeenCalled();
expect(math.add).toHaveBeenCalledWith(1, 2);
expect(math.add).toHaveReturned();
expect(math.add).toHaveReturnedWith(3);
});
现在,当我运行测试时,除了最后一个测试之外,所有测试都通过了:
expect(math.add).toHaveReturnedWith(3);
在控制台中我看到:
● 调用 math.add
expect(jest.fn()).toHaveReturnedWith(expected)
Expected: 3
Received: undefined
Number of returns: 1
10 | expect(math.add).toHaveBeenCalledWith(1, 2);
11 | expect(math.add).toHaveReturned();
> 12 | expect(math.add).toHaveReturnedWith(3);
| ^
13 | });
和 console.log(math.add.mock) 给我这个:
console.log
{
calls: [ [ 1, 2 ] ],
instances: [
{
add: [Function],
subtract: [Function],
multiply: [Function],
divide: [Function]
}
],
invocationCallOrder: [ 1 ],
results: [ { type: 'return', value: undefined } ],
lastCall: [ 1, 2 ]
}
所以看来 math.add 模拟函数不返回任何值。我的问题是为什么?我做错了什么?
I'm learning unit testing with jest and particularly mocking modules. I wrote some simple module in math.js file with some math methods:
const add = (a, b) => a + b;
const subtract = (a, b) => b - a;
const multiply = (a, b) => a * b;
const divide = (a, b) => b / a;
module.exports = {
add,
subtract,
multiply,
divide
}
Then I include it in my main js and I do module mocking like this:
jest.mock('./math.js');
const math = require('./math');
test("calls math.add", () => {
math.add(1, 2);
console.log(math.add.mock);
expect(math.add).toHaveBeenCalled();
expect(math.add).toHaveBeenCalledWith(1, 2);
expect(math.add).toHaveReturned();
expect(math.add).toHaveReturnedWith(3);
});
Now when I run my test all tests are passing besides the last one:
expect(math.add).toHaveReturnedWith(3);
In console I see:
● calls math.add
expect(jest.fn()).toHaveReturnedWith(expected)
Expected: 3
Received: undefined
Number of returns: 1
10 | expect(math.add).toHaveBeenCalledWith(1, 2);
11 | expect(math.add).toHaveReturned();
> 12 | expect(math.add).toHaveReturnedWith(3);
| ^
13 | });
and console.log(math.add.mock) gives me this:
console.log
{
calls: [ [ 1, 2 ] ],
instances: [
{
add: [Function],
subtract: [Function],
multiply: [Function],
divide: [Function]
}
],
invocationCallOrder: [ 1 ],
results: [ { type: 'return', value: undefined } ],
lastCall: [ 1, 2 ]
}
So it seems that math.add mocked function does not return any value. My question is why? What I do wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
jest.mock('someModule')
后,jest 将为该模块创建一个自动模拟版本。这意味着从该模块导出的东西都被模拟了。您可以认为模拟的
math.add
方法是jest.fn()
。它没有模拟实现。如果没有给出实现,则模拟函数在调用时将返回undefined
。这就是math.add(1, 2)
返回undefined
的原因。您正在测试 math 模块,那么您不应该模拟它。但如果你坚持要做的话。您可以在调用
math.add(1,2)
之前使用math.add.mockReturnValue(3);
。但这没有意义,你可以给任何你想要的值。您没有测试真正的math.add
方法。您只需将模拟返回值与断言expect(math.add).toHaveReturnedWith(3)
例如
math.js
:math.test.js
:测试结果:
After using
jest.mock('someModule')
, jest will create an auto-mocked version for this module. This means the exported things from this module are all mocked.You can think that the mocked
math.add
method isjest.fn()
. There is no mock implementation for it. If no implementation is given, the mock function will returnundefined
when invoked. That's why themath.add(1, 2)
returnsundefined
.You are testing the
math
module, then you should NOT mock it. But if you insist to do it. You can usemath.add.mockReturnValue(3);
before invoke themath.add(1,2)
. But it doesn't make sense, you can give any value you want. You didn't test the realmath.add
method. You just match your mock return value to the assertionexpect(math.add).toHaveReturnedWith(3)
E.g.
math.js
:math.test.js
:Test result: