我收到错误:
ReferenceError: Cannot access 'myMock' before initialization
我正在这样做:
import MyClass from './my_class';
import * as anotherClass from './another_class';
const mockMethod1 = jest.fn();
const mockMethod2 = jest.fn();
jest.mock('./my_class', () => {
return { …Run Code Online (Sandbox Code Playgroud) 我希望对当前正在测试的文件中使用的函数存根。像这样的解构需要此函数:
const { theFunctionIWant } = require('path/to/module')
Run Code Online (Sandbox Code Playgroud)
测试时,永远不会调用存根,而是继续调用实函数。但是当我“正常”地要求它时(即:不破坏结构)
const myModule = require('path/to/module')
Run Code Online (Sandbox Code Playgroud)
然后正确使用存根,一切正常
我认为这是由于解构的工作原理以及sinon直接对对象属性而不是对函数进行存根的事实。无论如何,如果您能为我提供一些见解,我将不胜感激!