该模拟在此测试之外是否可见?

Ben*_*Ben 5 javascript testing jestjs

我看到一个测试在块jest.mock之外进行了调用describe

我担心这会在测试之外造成可见的变化。我不希望这个测试影响其他测试。

应该jest.mock移动到描述块内还是按原样可以?

import target from '../';

jest.mock('../../../helpers/helpers.api', () => ({
    httpPost(...args) {
        return args;
    }
}));

describe('my component', () => {
    it('should foo', () => {
        //... 
    });
});
Run Code Online (Sandbox Code Playgroud)

Win*_*ing 4

据我了解,你的例子照原样就很好。

\n\n
\n

被模拟的模块jest.mock仅针对调用jest.mock. 即使在模拟该模块的测试文件之后运行,导入该模块的另一个文件也将获得原始实现。

\n
\n\n

\xe2\x80\x93 https://facebook.github.io/jest/docs/en/jest-object.html#jestmockmodulename-factory-options

\n