"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Mock ES6 Module Imports in Jest?

How to Mock ES6 Module Imports in Jest?

Posted on 2025-03-24
Browse:226

How to Mock ES6 Module Imports in Jest?

Mocking ES6 Module Imports in Jest

Introduction

Jest provides a comprehensive suite of tools for unit testing, including mocking modules that are imported in your code. This article explains how to mock ES6 module imports in Jest, addressing the challenge of testing module dependencies.

Problem Statement

Mocking module imports allows developers to isolate the behavior of a specific module while executing tests for its dependent modules. However, the approach used in Jasmine, where imports are replaced with spies, is not directly applicable in Jest due to its different testing environment.

Solution

Jest provides the import * syntax to import all exports from a module as a single object. This technique can be leveraged to mock ES6 module imports.

Mocking Named Exports

For named exports, simply import the module using import * and then mutate the exported object to mock the desired function:

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

Mocking Default Exports

For default exports, you can import them using import moduleName from 'modulePath' and then mutate the default value of the imported object:

// dependency.js
export default (y) => console.log(y);
// myModule.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
  });
});

Conclusion

Using the import * syntax and mutating the exported object, it is possible to mock ES6 module imports in Jest, enabling you to test the functionality of your modules while isolating their dependencies.

Release Statement This article is reproduced on: 1729676733 If there is any infringement, please contact [email protected] to delete it.
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3