如何使用 Jest 模拟 ES6 模块导入? [英] How can I mock an ES6 module import using Jest?

查看:41
本文介绍了如何使用 Jest 模拟 ES6 模块导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试我的 ES6 模块之一以特定方式调用另一个 ES6 模块.使用 Jasmine 这非常简单 --

I want to test that one of my ES6 modules calls another ES6 module in a particular way. With Jasmine this is super easy --

应用代码:

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

export default (x) => {
  dependency.doSomething(x * 2);
}

和测试代码:

//myModule-test.js
import myModule from '../myModule';
import dependency from '../dependency';

describe('myModule', () => {
  it('calls the dependency with double the input', () => {
    spyOn(dependency, 'doSomething');

    myModule(2);

    expect(dependency.doSomething).toHaveBeenCalledWith(4);
  });
});

Jest 的等价物是什么?我觉得这是一件很简单的事情,但我一直在努力弄清楚.

What's the equivalent with Jest? I feel like this is such a simple thing to want to do, but I've been tearing my hair out trying to figure it out.

我最接近的是用 requires 替换 imports,并将它们移动到测试/函数中.这两件事我都不想做.

The closest I've come is by replacing the imports with requires, and moving them inside the tests/functions. Neither of which are things I want to do.

// myModule.js
export default (x) => {
  const dependency = require('./dependency'); // Yuck
  dependency.doSomething(x * 2);
}

//myModule-test.js
describe('myModule', () => {
  it('calls the dependency with double the input', () => {
    jest.mock('../dependency');

    myModule(2);

    const dependency = require('../dependency'); // Also yuck
    expect(dependency.doSomething).toBeCalledWith(4);
  });
});

对于奖励积分,当 dependency.js 中的函数是默认导出时,我很乐意让整个事情正常工作.但是,我知道监视默认导出在 Jasmine 中不起作用(或者至少我永远无法让它工作),所以我也不希望在 Jest 中也可以这样做.

For bonus points, I'd love to make the whole thing work when the function inside dependency.js is a default export. However, I know that spying on default exports doesn't work in Jasmine (or at least I could never get it to work), so I'm not holding out hope that it's possible in Jest either.

推荐答案

快进到 2020 年,我发现这篇博文是解决方案:Jest mock 默认和命名导出

Fast forwarding to 2020, I found this blog post to be the solution: Jest mock default and named export

仅使用 ES6 模块语法:

Using only ES6 module syntax:

// esModule.js
export default 'defaultExport';
export const namedExport = () => {};

// esModule.test.js
jest.mock('./esModule', () => ({
  __esModule: true, // this property makes it work
  default: 'mockedDefaultExport',
  namedExport: jest.fn(),
}));

import defaultExport, { namedExport } from './esModule';
defaultExport; // 'mockedDefaultExport'
namedExport; // mock function

还有一件事你需要知道(我花了一段时间才弄明白)是你不能在测试中调用 jest.mock() ;您必须在模块的顶层调用它.但是,如果您想为不同的测试设置不同的模拟,您可以在各个测试中调用 mockImplementation().

Also one thing you need to know (which took me a while to figure out) is that you can't call jest.mock() inside the test; you must call it at the top level of the module. However, you can call mockImplementation() inside individual tests if you want to set up different mocks for different tests.

这篇关于如何使用 Jest 模拟 ES6 模块导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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