htt*_*ete 5 node.js typescript jestjs ts-jest
我正在使用ts-jestESM 导入nodejs。
问题是我的jest.mock不工作,它不是嘲笑。
import { jest } from "@jest/globals";
jest.mock("./helpers"); // I tried before or after import
import { fn } from "./helpers";
describe("SimpleTest", () => {
it("should work", () => {
console.log(fn); // => defined
console.log(fn.mockReturnValue); // => /!\ UNDEFINED, fn is not a jest mock.
});
});
Run Code Online (Sandbox Code Playgroud)
我的笑话配置:
export default {
preset: "ts-jest/presets/default-esm",
extensionsToTreatAsEsm: [".ts"],
globals: {
"ts-jest": {
useESM: true,
},
},
}
Run Code Online (Sandbox Code Playgroud)
我使用的命令:
node --experimental-vm-modules --experimental-specifier-resolution=node $(yarn bin jest)
我正在使用节点v16.13.2和 ts-jest27.1.3
jest.mock适用于 CJS,但不适用于 ESM。
有一个jest.unstable_mockModule开放的 PR 将其变成一个稳定的 API,但它处于不确定状态(Jest 作者失去了动力)。
更多细节:
该问题中的一些有价值的评论可能会帮助您找到部分解决方案。
这对我不起作用。我放弃了它,完全走了另一条路。
| 归档时间: |
|
| 查看次数: |
2789 次 |
| 最近记录: |