【发布时间】:2018-08-27 19:22:15
【问题描述】:
我正在为我的应用程序编写单元测试用例。有一个函数写在 Utils 部分并在所有文件中使用。我想在需要时模拟这个 Utils 函数,但我无法这样做。
这是我的代码设置:
Utils.js
> const getData = (name) => "Hello !!! " + name;
>
> const getContact = ()=> return Contacts.mobile;
>
> export {
> getData,
> getContact }
Login.js(使用 Utils.js)
const welcomeMessage = (name) => {
return getData(name);
}
我的测试文件(Login.spec.js)
import { getData } from '../../src/utils';
jest.mock('getData', () => jest.fn())
describe('User actions', () => {
it('should get username', () => {
const value = 'Hello !!! Jest';
expect(welcomeMessage('Jest')).toEqual(value);
});
});
当我运行我的测试用例时,我收到了这个错误:
Cannot find module 'getData' from 'Login.spec.js'
我试图在官方 Jest 文档和 SO 上找到解决方案,但找不到任何东西。我无法修复此错误并模拟此函数。
【问题讨论】:
标签: javascript reactjs unit-testing jestjs