我有一个节点模块的模拟类及其方法,如下所示,测试用例到测试用例,我需要更改methodOne
返回值。
jest.mock("module_name", () => {
return {
__esModule: true,
Abc: class Abc {
constructor(config) {}
async methodOne(params) {
return {
message: {
content:
'This text I need to change',
},
};
}
},
Configuration: class Configuration {
constructor(config) {
return true;
}
},
};
});
describe("getVeganStatus", () => {
it("should handle case one......", async () => {
// methodOne message.content return value should be "ABC"
})
it("should handle case two......", async () => {
// methodOne message.content return value should be "XYZ"
});
})```
1条答案
按热度按时间cbwuti441#
使用
mockImplementation()
或mockImplementationOnce()
替换模拟文件。对
jest.mock
的调用被提升到代码的顶部。您可以稍后指定模拟,例如在beforeAll()
中,通过在现有mock上调用mockImplementation()
(或mockImplementationOnce()
)而不是使用工厂参数。如果需要,还可以在测试之间更改mock:例如
some-module.js
:main.js
:main.test.js
:试验结果: