Jest: It's impossible to reset modules when using ES6 import syntax

Created on 31 Mar 2017  路  4Comments  路  Source: facebook/jest

If you are using ES6 import syntax, there is no way to reset modules between tests. The only workaround is to put tests in different files, or switch to non-standard require syntax.

For example, the following test suite will fail due to shared state:

import { five } from 'aModule';

beforeEach() {
  jest.resetModules();
}

describe('testSuite', () => {
  test('firstTest', () => {
    five++;
    expect(five).toBe(6);
  });

  test('secondTest', () => {
    five + 2;
    expect(five).toBe(7);
  });
});

Event using the resetModules option will not reset five because the reference has already been created.

There should be an option to run each test in complete isolation.

Most helpful comment

Resetting the module registry doesn't do any good if you don't re-import or re-require the module afterward. See examples at https://facebook.github.io/jest/docs/jest-object.html#jestresetmodules. I think your options are to use require or the fancy new import() syntax.

All 4 comments

Resetting the module registry doesn't do any good if you don't re-import or re-require the module afterward. See examples at https://facebook.github.io/jest/docs/jest-object.html#jestresetmodules. I think your options are to use require or the fancy new import() syntax.

Just as @jwbay said (thanks 鉂わ笍). Use require(), it's definitely not _"non-standard"_.

Just as @jwbay said (thanks 鉂わ笍). Use require(), it's definitely not _"non-standard"_.

Hi, I tried require(). But it not acted as what I expected.
I have a module A

module.exports = {
  a: 3
};

And I add test file

const jestTestCase = require('utils/jest');

describe('sandbox', () => {
  beforeEach(() => jest.resetModules());

  it('the first value should be 3 ', () => {
    expect(jestTestCase.a).toEqual(3);
    jestTestCase.a = 5;
  });

  it('the second value should be 3 ', () => {
    expect(jestTestCase.a).toEqual(3);
  });
});

The second test case was failed. But what I expected is all tests should be passed.

And I tried inline require. It seems only inline require works.

describe('sandbox', () => {
  beforeEach(() => jest.resetModules());
  it('the first value should be 3 ', () => {
    const jestTestCase = require('utils/jest');
    expect(jestTestCase.a).toEqual(3);
    jestTestCase.a = 5;
  });

  it('the second value should be 3 ', () => {
    const jestTestCase = require('utils/jest');
    expect(jestTestCase.a).toEqual(3);
  });
});

But jest said they run all tests in sandbox. How to understand sandbox here?

Just in case someone else is also lost after trying above's solution but using import. Here's a fix for it:

describe('MyTests', () => {
  let MyModule;

  beforeEach(() => {
    return import('../module/path').then(module => {
      MyModule = module;
      jest.resetModules();
    });
  });

  test('should test my module', () => {
    expect(MyModule.aMethod).not.toBeUndefined();
  });
});

Requisites: configure the babel-plugin-dynamic-import-node babel plugin.

Source: https://jestjs.io/docs/en/jest-object#jestdomockmodulename-factory-options

Was this page helpful?
0 / 5 - 0 ratings