admin管理员组文章数量:1356846
I need to execute an argument that is a callback of a jest mock.
In the jest documentation, their callback example is about testing the first callback. I have behaviors inside nested callbacks I need to test. In the examples of promises, they use resolves
and rejects
. Is there anything like this for nested callbacks? I'm currently executing the mocked call argument, which I'm not sure if it is the remended way.
System under test:
function execute() {
globals.request.post({}, (err, body) => {
// other testable behaviors here.
globals.doSomething(body);
// more testable behaviors here. that may include more calls to request.post()
});
}
The test:
globals.request.post = jest.fn();
globals.doSomething = jest.fn();
execute();
// Is this the right way to execute the argument?
globals.request.post.mock.calls[0][1](null, bodyToAssertAgainst);
expect(globals.doSomething.mock.calls[0][1]).toBe(bodyToAssertAgainst);
My question is in the ments in the code above. Is this the remended way to execute a callback, which is an argument of the mocked function?
I need to execute an argument that is a callback of a jest mock.
In the jest documentation, their callback example is about testing the first callback. I have behaviors inside nested callbacks I need to test. In the examples of promises, they use resolves
and rejects
. Is there anything like this for nested callbacks? I'm currently executing the mocked call argument, which I'm not sure if it is the remended way.
System under test:
function execute() {
globals.request.post({}, (err, body) => {
// other testable behaviors here.
globals.doSomething(body);
// more testable behaviors here. that may include more calls to request.post()
});
}
The test:
globals.request.post = jest.fn();
globals.doSomething = jest.fn();
execute();
// Is this the right way to execute the argument?
globals.request.post.mock.calls[0][1](null, bodyToAssertAgainst);
expect(globals.doSomething.mock.calls[0][1]).toBe(bodyToAssertAgainst);
My question is in the ments in the code above. Is this the remended way to execute a callback, which is an argument of the mocked function?
Share Improve this question edited Jun 21, 2019 at 14:54 xavier 2,0694 gold badges23 silver badges57 bronze badges asked May 7, 2018 at 7:43 Shawn McleanShawn Mclean 57.5k96 gold badges281 silver badges412 bronze badges1 Answer
Reset to default 8Since you don't care about the implementation of your globals.request.post
method you need to extend your mock a bit in order for your test to work.
const bodyToAssertAgainst = {};
globals.request.post = jest.fn().mockImplementation((obj, cb) => {
cb(null, bodyToAssertAgainst);
});
Then you can go onto expect that doSomething
was called with bodyToAssertAgainst
. Also, this way you can easily test if your post
would throw an Error.
本文标签: javascriptExecute mocked jest callback argumentStack Overflow
版权声明:本文标题:javascript - Execute mocked jest callback argument - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744008779a2575182.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论