Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It's impossible to reset modules when using ES6 import syntax #3236

Closed
DzoQiEuoi opened this issue Mar 31, 2017 · 5 comments
Closed

It's impossible to reset modules when using ES6 import syntax #3236

DzoQiEuoi opened this issue Mar 31, 2017 · 5 comments

Comments

@DzoQiEuoi
Copy link

DzoQiEuoi commented Mar 31, 2017

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.

@jwbay
Copy link
Contributor

jwbay commented Mar 31, 2017

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.

@thymikee
Copy link
Collaborator

thymikee commented Apr 1, 2017

Just as @jwbay said (thanks ❤️). Use require(), it's definitely not "non-standard".

@xiaojingzhao
Copy link

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?

@luciomartinez
Copy link

luciomartinez commented Sep 24, 2020

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

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants