big*_*ato 8 javascript reactjs jestjs
我正在尝试为使用 node-forge 的代码编写测试。由于某种原因,当我调用时测试挂起forge.md.sha256.create();:
import forge from "node-forge";
const privateKey = "foo";
const storagePin = "bar";
const md = forge.md.sha256.create();
md.update(privateKey + storagePin);
const metadataKey = md.digest().toHex();
Run Code Online (Sandbox Code Playgroud)
作为解决方法,我尝试模拟该方法的实现,以便它只返回一个硬编码的字符串:
import forge from "node-forge";
jest.mock("node-forge");
forge.mockImplementation(() => {
return {
md: {
sha256: {
create: () => {
return {
update: () => {},
digest: () => {
toHex: () => "foobar";
}
};
}
}
}
};
});
// tests
Run Code Online (Sandbox Code Playgroud)
然而,我的测试一直失败:
TypeError: _nodeForge2.default.mockImplementation is not a function
at Object.<anonymous> (src/redux/epics/authentication-epic.test.js:20:27)
at new Promise (<anonymous>)
at Promise.resolve.then.el (node_modules/p-map/index.js:46:16)
at processTicksAndRejections (internal/process/next_tick.js:81:5)
Run Code Online (Sandbox Code Playgroud)
奇怪的是,当我尝试模拟我自己的文件时,这个策略运行得非常好。
模拟第三方库的正确方法是什么?
你尝试过这样吗?更多相关信息请参见此处。
jest.mock('node-forge', () => ({
md: {
sha256: {
create: () => ({
update: () => {},
digest: () => ({
toHex: () => 'foobar'
}),
}),
},
},
}));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13274 次 |
| 最近记录: |