【发布时间】:2020-06-05 22:40:44
【问题描述】:
我试图弄清楚如何在我的测试文件中使用 Jest 模拟一个常量 (DEFAuLT_OPTIONS)
我的文件结构如下:
src/
Builder/
- Constants.js
- Builder.js
- Builder.test.js
文件
// Constants.js
// Define a set of defaults to be used in my production build
const DEFAULT_OPTIONS = {
enableTransparency: true,
threshold: 100
};
export {
DEFAULT_OPTIONS
}
// Builder.js
// Exports a `buildTree` function, which uses the constants
import { DEFAULT_OPTIONS } from './Constants';
function buildTree(options) {
return {
root: '/',
options: { ...options, ...DEFAULT_OPTIONS }
};
}
export { buildTree };
// Builder.test.js
import { DEFAULT_OPTIONS } from './Constants';
import { buildTree } from './Builder';
describe('Builder', () => {
it('some description', () => {
// How to mock value of `DEFAULT_OPTIONS` here so that
// the call to `buildGTree` uses my mocked version?
const options = { foo: 'bar' };
const result = buildTree(options);
});
});
我怎么能 -
- 为单个测试模拟
DEFAULT_OPTIONS的值? - 为一组测试模拟
DEFAULT_OPTIONS的值? (如果不同)
谢谢!
编辑:我尝试了以下方法,但该模块的值似乎为 undefined
const mockDefaultOptions = {
optionA: 'a',
optionB: 'b'
}
jest.mock('./Constants', () => ({
DEFAULT_OPTIONS: mockDefaultOptions,
}));
【问题讨论】:
标签: javascript module mocking jestjs