如何仅在 Jest 的单个测试中模拟 ES6 模块?

Agm*_*her 5 javascript unit-testing jestjs es6-modules

假设我有以下模块:

源代码/验证器.js

export const isPositiveNumber = value => value > 0 && value < Number.MAX_SAFE_INTEGER;
Run Code Online (Sandbox Code Playgroud)

我在另一个模块中使用它:

src/计算器/volume.js

import { isPositiveNumber } from '../validators';
import { InvalidArgumentException } from '../exceptions';

export const sphere = radius => {
    if (!isPositiveNumber(radius)) {
        throw new InvalidArgumentException('radius must be a positive number');
    }

    return (4/3) * Math.PI * Math.pow(radius, 3);
};
Run Code Online (Sandbox Code Playgroud)

然后在我的测试中:

测试/计算器/volume.test.js

import { volume } from '../../src/calculators/volume';
import { InvalidArgumentException } from '../../src/exceptions';
import { isPositiveNumber } from '../../src/validators';

jest.mock('../../src/validators');

describe('Volume calculations', () => {
    describe('sphere()', () => {
        it('should throw an exception if the radius is invalid', () => {
            isPositiveNumber.mockReturnValue(false);
            expect(() => volume()).toThrow(InvalidArgumentException);
        });

        it('should compute the volume', () => {
            isPositiveNumber.mockReturnValue(true);
            expect(volume(3)).toBeCloseTo(113,3);
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

这有效,除了,我不想isPositiveNumber在实际计算体积的第二个测试中模拟。

我希望isPositiveNumber模拟仅在验证测试中。

鉴于我正在使用的 ES6 模块设置,我不知道如何执行此操作。似乎需要模拟拦截发生在测试范围之外,这意味着我必须模拟套件中每个测试中的返回值。

这只是一个简单测试的例子,但稍后会有更复杂的测试,我想知道如何在每个测试的基础上更精确地模拟 ES6 模块。

任何帮助,将不胜感激。

Pat*_*rts 7

这可能是因为您正在使用babel-jest根据文档,它将提升jest.mock()到顶层,无论它实际放置在 ES6 模块代码中的位置。

使用 时babel-jest,对 的调用mock将自动提升到代码块的顶部。

相反,您可以分别在和挂钩中使用jest.doMock()jest.dontMock()方法来进行需要您模拟模块定义中的函数的单独测试。before()after()