javascript 模拟另一个文件中的函数 - Jest

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49359476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:33:04  来源:igfitidea点击:

Mock a function from another file - Jest

javascriptreactjsunit-testingjestjs

提问by Dalvik

I am writing Unit test cases for my application. There is one function which is written in Utils section and Used in all files. I wanted to mock this Utils function whenever I need but I am unable to do so.

我正在为我的应用程序编写单元测试用例。有一个函数写在 Utils 部分并在所有文件中使用。我想在需要时模拟这个 Utils 函数,但我无法这样做。

Here is my code setup:

这是我的代码设置:

Utils.js

实用程序.js

> const getData = (name) => "Hello !!! " + name;
> 
> const getContact = ()=> return Contacts.mobile;
> 
> export {
>     getData,
>     getContact }

Login.js (Which uses Utils.js)

Login.js(使用Utils.js)

    const welcomeMessage = (name) => {

    return getData(name);
    }

My Test file (Login.spec.js)

我的测试文件 (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);
    });

});

When I run my test case then I am getting this error:

当我运行我的测试用例时,我收到此错误:

 Cannot find module 'getData' from 'Login.spec.js'

I tried to find the solution on official Jest Documentation and on SO as well but couldn't found anything. I am unable to fix this error and mock this function.

我试图在官方 Jest 文档和 SO 上找到解决方案,但找不到任何东西。我无法修复此错误并模拟此功能。

回答by Khoa

The first argument of jest.mock(...)must be a module path:

的第一个参数jest.mock(...)必须是模块路径:

jest.mock('../../src/utils');

because the utilsmodule is your code, not a 3rd lib, so you must learn manual mock of jest: https://facebook.github.io/jest/docs/en/manual-mocks.html

因为utils模块是你的代码,而不是第三个库,所以你必须学习手动模拟笑话:https: //facebook.github.io/jest/docs/en/manual-mocks.html

if you had this file: src/utils.js

如果你有这个文件: src/utils.js

you can mock it by creating a file: src/__mocks__/utils.js

你可以通过创建一个文件来模拟它: src/__mocks__/utils.js

content of this file is the replication of the original but replace the implementation by getData = jest.fn()

此文件的内容是原始文件的复制,但将实现替换为 getData = jest.fn()

on you test file, just call: jest.mock('../../src/utils');at begin of file.

在您的测试文件上,只需调用:jest.mock('../../src/utils');在文件开头。

then when you're familiar with, you can call that function inside beforeEach()and call its counter jest.unmock('../../src/utils');insider afterEach()

然后当你熟悉了,你可以在里面调用那个函数beforeEach()并调用它的counter jest.unmock('../../src/utils');insiderafterEach()

An easy way to think about it is that:

一个简单的思考方式是:

when you call jest.mock('../../src/utils');, it means you tell jestthat:

当你打电话时jest.mock('../../src/utils');,这意味着你告诉jest

hey if the running test meets the line require('../../src/utils'), don't load it, let load ../../src/__mocks__/utils.

嘿,如果运行测试符合行require('../../src/utils'),不要加载它,让加载../../src/__mocks__/utils

回答by Dan

another solution would fake this by doing something like:

另一种解决方案将通过执行以下操作来伪造:

window['getData'] = jest.fn();