【问题标题】:Will this mock be visibile outside of this test?这个模拟在这个测试之外是否可见?
【发布时间】:2017-09-19 00:04:35
【问题描述】:

我看到一个测试在 describe 块之外有一个 jest.mock 调用。

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

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

import target from '../';

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

describe('my component', () => {
    it('should foo', () => {
        //... 
    });
});

【问题讨论】:

    标签: javascript testing jestjs


    【解决方案1】:

    据我了解,您的示例按原样很好。

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

    ——https://facebook.github.io/jest/docs/en/jest-object.html#jestmockmodulename-factory-options

    【讨论】:

      猜你喜欢
      • 2020-05-31
      • 2012-04-22
      • 1970-01-01
      • 1970-01-01
      • 2020-02-01
      • 2020-06-10
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多