小编Fei*_*291的帖子

使用 JEST 和 ESM 的快速测试给出错误“TypeError:无法分配给对象 '[object Module]' 的只读属性 'sum'”

我尝试使用 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)

javascript node.js express jestjs es6-modules

7
推荐指数
1
解决办法
1651
查看次数

标签 统计

es6-modules ×1

express ×1

javascript ×1

jestjs ×1

node.js ×1