开玩笑嘲笑默认导出-需求与导入 [英] Jest mocking default exports - require vs import

查看:98
本文介绍了开玩笑嘲笑默认导出-需求与导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了一些有关在这里嘲笑默认出口的问题,但是我认为这还没有被问到:

I have seen questions referring to the mocking of default exports with jest around here, but I don't think this has already been asked:

当模拟正在测试的模块的依赖项的默认导出时,如果该模块使用ES6 import语句导入了依赖项,则测试套件将无法运行,说明TypeError: (0 , _dependency.default) is not a function成功,但是,如果该模块使用改为require().default调用.

When mocking the default export of a dependency of a module that is being tested, the tests suite fails to run if the module imports the dependency with the ES6 import statement, stating TypeError: (0 , _dependency.default) is not a function It succeeds, however, if the module uses a require().default call instead.

根据我的理解,import module from location直接转换为const module = require(location).default,所以我很困惑为什么会这样.我宁愿保持代码风格一致,也不在原始模块中使用require调用.

In my understanding, import module from location directly translates to const module = require(location).default, so I am very confused why this is happening. I'd rather keep my code style consistent and not use the require call in the original module.

有办法吗?

带有模拟文件的测试文件:

Test file with mock:

import './modules.js';
import dependency from './dependency';

jest.mock('./dependency', () => {
   return {
      default: jest.fn()
   };
});

// This is what I would eventually like to call
it('calls the mocked function', () => {
   expect(dependency).toHaveBeenCalled();
});

Dependency.js

Dependency.js

export default () => console.log('do something');

module.js(不起作用)

module.js (not working)

import dependency from './dependency.js';
dependency();

module.js(正在运行)

module.js (working)

const dependency = require('./dependency.js').default;
dependency();

推荐答案

您可以使用es6 importrequire js在玩笑测试中导入js文件.

You can use either es6 import or require js to import your js files in your jest tests.

使用es6 import时,您应该知道jest正在尝试解析所有依赖关系,并且还会为您要导入的类调用构造函数.在此步骤中,您无法模拟.必须成功解决依赖关系,然后您才能进行模拟.

When using es6 import you should know that jest is trying to resolve all the dependencies and also calls the constructor for the class that you are importing. During this step, you cannot mock it. The dependency has to be successfully resolved, and then you can proceed with mocks.

我还应该补充一点,如此处默认情况下,笑话会将所有jest.mocks提升到文件的顶部,因此导入的顺序并不重要.

I should also add that as can be seen here jest by default hoists any jest.mocks to the top of the file so the order in which you place your imports does not really matter.

您的问题虽然有所不同.您的模拟功能假定您已使用require js包含了js文件.

Your problem though is different. Your mock function assumes that you have included your js file using require js.

jest.mock('./dependecy', () => {
   return {
      default: jest.fn()
   };
});

使用require js导入文件时,其结构如下:

When you import a file using require js, this is the structure it has:

因此,假设我已经使用require js导入了名为"Test"的类,并且它具有名为"doSomething"的方法,则可以在测试中通过执行以下操作来调用它:

So assuming I have imported my class called "Test" using require js, and it has method called "doSomething" I could call it in my test by doing something like:

const test = require('../Test');
test.default.doSomething();

使用es6 import导入时,您应该以不同的方式进行操作.使用相同的示例:

When importing it using es6 import, you should do it differently though. Using the same example:

import Test from '../Test';
Test.doSomething();

如果要使用es6 import,请将模拟功能更改为:

If you want to use es6 import change your mock function to:

jest.mock('./dependecy', () => jest.fn());

这篇关于开玩笑嘲笑默认导出-需求与导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆