我尝试使用 jest 来模拟导入的函数,但出现此错误,TypeError: Assignment to constant variable.或者TypeError: Cannot assign to read only property 'sum' of object '[object Module]',我希望获得在本测试中模拟的返回值
尝试1
import { jest } from '@jest/globals'
import * as util from "./util.js"
it("TypeError: Cannot assign to read only property 'sum' of object '[object Module]'", () => {
jest.spyOn(util, "sum").mockImplementation(() => { return 2 })
expect(sum(1, 2)).toBe(2);
})
Run Code Online (Sandbox Code Playgroud)
尝试2
import { jest } from '@jest/globals'
import { sum } from './util.js'
it("TypeError: Cannot assign to read only property …Run Code Online (Sandbox Code Playgroud)