How can I mock an ES6 module import using Jest?

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

I'm beginning to think this isn't possible, but I want to ask anyway.

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

The app code:

// myModule.js import dependency from './dependency';  export default (x) => {   dependency.doSomething(x * 2); } 

And the test code:

//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);   }); }); 

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.

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);   }); }); 

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.

回答1:

I've been able to solve this by using a hack involving import *. It even works for both named and default exports!

For a named export:

// dependency.js export const doSomething = (y) => console.log(y)  // myModule.js import { doSomething } from './dependency';  export default (x) => {   doSomething(x * 2); }  // myModule-test.js import myModule from '../myModule'; import * as dependency from '../dependency';  describe('myModule', () => {   it('calls the dependency with double the input', () => {     dependency.doSomething = jest.fn(); // Mutate the named export      myModule(2);      expect(dependency.doSomething).toBeCalledWith(4);   }); }); 

Or for a default export:

// dependency.js export default (y) => console.log(y)  // myModule.js import dependency from './dependency'; // Note lack of curlies  export default (x) => {   dependency(x * 2); }  // myModule-test.js import myModule from '../myModule'; import * as dependency from '../dependency';  describe('myModule', () => {   it('calls the dependency with double the input', () => {     dependency.default = jest.fn(); // Mutate the default export      myModule(2);      expect(dependency.default).toBeCalledWith(4); // Assert against the default   }); }); 

As Mihai Damian quite rightly pointed out below, this is mutating the module object of dependency, and so it will 'leak' across to other tests. So if you use this approach you should probably store the original value and then set it back again after each test. This is why sinon has a restore method!



回答2:

You have to mock the module and set the spy by yourself:

import myModule from '../myModule'; import dependency from '../dependency'; jest.mock('../dependency', () => ({   doSomething: jest.fn() }))  describe('myModule', () => {   it('calls the dependency with double the input', () => {     myModule(2);     expect(dependency.doSomething).toBeCalledWith(4);   }); }); 


回答3:

Adding more to Andreas answer. I had the same problem with ES6 code but did not want to mutate the imports. That looked hacky. So I did this

import myModule from '../myModule'; import dependency from '../dependency'; jest.mock('../dependency');  describe('myModule', () => {   it('calls the dependency with double the input', () => {     myModule(2);   }); }); 

And added dependency.js in " __ mocks __" folder parallel to dependency.js. This worked for me. Also, this gave me option to return suitable data from mock implementation. Make sure you give the correct path to the module you want to mock.



回答4:

To mock an ES6 dependency module default export using jest:

import myModule from '../myModule'; import dependency from '../dependency';  jest.mock('../dependency');  // If necessary, you can place a mock implementation like this: dependency.mockImplementation(() => 42);  describe('myModule', () => {   it('calls the dependency once with double the input', () => {     myModule(2);      expect(dependency).toHaveBeenCalledTimes(1);     expect(dependency).toHaveBeenCalledWith(4);   }); }); 

The other options didn't work for my case.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!