admin管理员组文章数量:1303404
Say I have the following module:
src/validators.js
export const isPositiveNumber = value => value > 0 && value < Number.MAX_SAFE_INTEGER;
I use it in another module:
src/calculators/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);
};
Then in my test:
tests/calculators/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 pute the volume', () => {
isPositiveNumber.mockReturnValue(true);
expect(volume(3)).toBeCloseTo(113,3);
});
});
});
This works, EXCEPT, I don't want to have mock isPositiveNumber
in the second test that actually putes the volume.
I want the isPositiveNumber
mock to be ONLY in the validation test.
I do not know how to do this given the ES6 module setup I'm using. It seems to require the mock interception happen outside of the scope of the test, which means I have to mock the return value in EVERY test in the suite.
This is just an example of a simple test, but there will be more plex tests later, and I want to know how to more precisely mock ES6 modules on a per-test basis.
Any help would be appreciated.
Say I have the following module:
src/validators.js
export const isPositiveNumber = value => value > 0 && value < Number.MAX_SAFE_INTEGER;
I use it in another module:
src/calculators/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);
};
Then in my test:
tests/calculators/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 pute the volume', () => {
isPositiveNumber.mockReturnValue(true);
expect(volume(3)).toBeCloseTo(113,3);
});
});
});
This works, EXCEPT, I don't want to have mock isPositiveNumber
in the second test that actually putes the volume.
I want the isPositiveNumber
mock to be ONLY in the validation test.
I do not know how to do this given the ES6 module setup I'm using. It seems to require the mock interception happen outside of the scope of the test, which means I have to mock the return value in EVERY test in the suite.
This is just an example of a simple test, but there will be more plex tests later, and I want to know how to more precisely mock ES6 modules on a per-test basis.
Any help would be appreciated.
Share Improve this question edited Apr 15, 2018 at 6:42 AgmLauncher asked Apr 15, 2018 at 6:37 AgmLauncherAgmLauncher 7,27010 gold badges49 silver badges71 bronze badges1 Answer
Reset to default 6That's probably because you're using babel-jest
, which according to the documentation, will hoist jest.mock()
to top-level regardless of where it's actually placed in your ES6 module code.
When using
babel-jest
, calls tomock
will automatically be hoisted to the top of the code block.
Instead, you can use the jest.doMock()
and jest.dontMock()
methods in your before()
and after()
hooks respectively for individual tests that require you to mock the functions in your module definition.
本文标签: javascriptHow do I mock an ES6 module in only a single test in JestStack Overflow
版权声明:本文标题:javascript - How do I mock an ES6 module in only a single test in Jest? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741754694a2396043.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论