【发布时间】:2018-12-25 06:16:51
【问题描述】:
所以在我寻找问题的答案时,我发现了这篇文章:Jest: How to globally mock node-uuid (or any other imported module)
我已经尝试了答案,但我似乎无法正确使用它,因为它给了我一个未定义的错误。我是测试领域的新手,所以请原谅任何重大错误:
这是我的第一个方法
const mockF = jest.mock('uuid');
mockF.mockReturnValue('12345789');
但它无法识别功能。
“mockF.mockReturnValue 不是函数”以及我尝试过的其他内容。
然后我尝试按照帖子的建议手动模拟,但似乎无法让它工作,你能帮我吗?谢谢
如果有帮助,这是整个测试:
const faker = require('faker');
const storageUtils = require('../../storage/utils');
const utils = require('../utils/generateFile');
const { generateFileName } = storageUtils;
const { file } = utils;
test('should return a valid file name when provided the correct information', () => {
// ARRANGE
// create a scope
const scope = {
type: 'RECRUITER',
_id: '987654321',
};
// establish what the expected name to be returned is
const expectedName = 'r_987654321_123456789.png';
jest.mock('uuid/v4', () => () => '123456789');
// ACTION
const received = generateFileName(file, scope);
// ASSERT
// expect the returned value to equal our expected one
expect(received).toBe(expectedName);
});
【问题讨论】:
-
jest.mock('uuid', () => jest.fn(() => '1'));适合你吗? -
很遗憾没有
-
我终于解决了!感谢所有的帮助,问题是我在做 jest.mock 和 require 在一个特定的测试而不是一个整体。很抱歉给您添麻烦,再次感谢
标签: javascript jestjs uuid