代码之家  ›  专栏  ›  技术社区  ›  Sraw

如何使用jest模拟原始const值并恢复模拟?

  •  0
  • Sraw  · 技术社区  · 5 年前

    假设我有一个名为“config.js”的文件:

    export const DELAY_SECONDS = 5;
    

    在进行测试时:

    // Ignore the delay when doing the tests.
    jest.mock("/path/to/config", () => ({ DELAY_SECONDS: 0 }))
    

    但我也想测试一下原始值是否有效:

    it('should work with delay', () => {
        // Use original value implicitly.
        a_function_uses_DELAY_SECONDS()
        expect(...).toBe(...)
    })
    

    我该如何恢复那个模拟?或者有更好的方法来实现模拟吗?

    我尝试了以下方法,但均无效:

    it('should work with delay', () => {
        jest.unmock() // Doesn't work at all, don't even know what does this method do.
        // Use original value implicitly.
        a_function_uses_DELAY_SECONDS()
        expect(...).toBe(...)
    })
    
    it('should work with delay', () => {
        jest.mock("/path/to/config", () => ({ DELAY_SECONDS: 5 })) // Call the mock again doesn't work
        // Use original value implicitly.
        a_function_uses_DELAY_SECONDS()
        expect(...).toBe(...)
    })
    
    it('should work with delay', () => {
        const config = require("/path/to/config").default;
        config.DELAY_SECONDS = 5; // Won't work, as it is a constant, cannot modify
        // Use original value implicitly.
        a_function_uses_DELAY_SECONDS()
        expect(...).toBe(...)
    })
    
    0 回复  |  直到 5 年前
        1
  •  3
  •   Lin Du Correcter    5 年前

    你可以使用 jest.doMock(moduleName, factory, options) .

    例如。

    config.js :

    export const DELAY_SECONDS = 5;
    

    main.js :

    import { DELAY_SECONDS } from './config';
    
    function main() {
      return DELAY_SECONDS;
    }
    
    export { main };
    

    main.test.js :

    describe('64473533', () => {
      beforeEach(() => {
        jest.resetModules();
      });
      it('should work with delay - original', () => {
        const { main } = require('./main');
        const actual = main();
        expect(actual).toBe(5);
      });
      it('should work with delay - mocked', () => {
        jest.doMock('./config', () => ({ DELAY_SECONDS: 0 }));
        const { main } = require('./main');
        const actual = main();
        expect(actual).toBe(0);
      });
    });
    

    单元测试结果:

     PASS  src/stackoverflow/64473533/main.test.js
      64473533
        ✓ should work with delay - original (445ms)
        ✓ should work with delay - mocked (2ms)
    
    -----------|----------|----------|----------|----------|-------------------|
    File       |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
    -----------|----------|----------|----------|----------|-------------------|
    All files  |      100 |      100 |      100 |      100 |                   |
     config.js |      100 |      100 |      100 |      100 |                   |
     main.js   |      100 |      100 |      100 |      100 |                   |
    -----------|----------|----------|----------|----------|-------------------|
    Test Suites: 1 passed, 1 total
    Tests:       2 passed, 2 total
    Snapshots:   0 total
    Time:        3.694s