作者:qgone | 来源:互联网 | 2022-12-10 13:43
因此,在寻找问题的答案的过程中,我发现了以下信息:开玩笑:如何全局模拟node-uuid(或任何其他导入的模块)
我已经尝试了答案,但似乎无法正确使用它,因为它给了我未定义的错误。我是测试领域的新手,所以请原谅任何重大错误:
这是我的第一个方法
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);
});
小智..
6
使用来模拟它mockImplementation
。
import uuid from 'uuid/v4';
jest.mock('uuid/v4');
describe('mock uuid', () => {
it('should return testid }', () => {
uuid.mockImplementation(() => 'testid');
...
});
});
确保您import uuid
正确(使用正确的版本参考,例如v4,v3 ...)
1> 小智..:
使用来模拟它mockImplementation
。
import uuid from 'uuid/v4';
jest.mock('uuid/v4');
describe('mock uuid', () => {
it('should return testid }', () => {
uuid.mockImplementation(() => 'testid');
...
});
});
确保您import uuid
正确(使用正确的版本参考,例如v4,v3 ...)
我按照示例中提供的操作进行了操作,但出现此错误:类型'v4'上不存在属性'mockImplementation'。