Skip to content

Add the ability to specify modules to reset in jest.resetModules #6638

Closed
@benmj

Description

@benmj

🚀 Feature Proposal

Allow passing variable arguments to jest.resetModules to specify which modules should be reset.

Motivation

I have several files that contain local state which I would like to reset between tests. However, I do not want to reset and re-import all of the dependencies in the test file because this seems 1) unnecessary and 2) error prone and easy to forget.

Example

Current

Here is a somewhat simplified example:

jest.mock('logger');

let logger;
let myModule;

beforeEach(() => {
    jest.clearAllMocks()
        .resetModules();

    logger = require('logger');
    myModule = require('../myModule');
});

Desired

jest.mock('logger');
const logger = require('logger');

let myModule;

beforeEach(() => {
    jest.clearAllMocks()
        .resetModules('../myModule'); // pass in the desired file to be reset

    /** only reload the module that I want to reset **/
    myModule = require('../myModule');
});

Pitch

It would remove cruft, add specificity, and give finer-grained control.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions