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

Requiring modules with isolateModules does not work if module has already been required with a non-isolated method e.g: using require. #7863

Closed
zlatinejc opened this issue Feb 11, 2019 · 6 comments · Fixed by #10963
Labels

Comments

@zlatinejc
Copy link

🐛 Bug Report

Requiring modules with isolateModules does not work if module has already been required with a non-isolated method e.g: using require.

To Reproduce

describe('Isolated modules test', () => {
  test('should be able to return different isolated modules', () => {
    let m0 = require('../path/to/testing/module')
    // let m0
    // jest.isolateModules(() => {
    //   m0 = require('../path/to/testing/module')
    // })

    let m1
    jest.isolateModules(() => {
      m1 = require('../path/to/testing/module')
    })

    expect(m1).toStrictEqual(m0) // does not fail, while it should.
  })
})

Expected behavior

Requiring a module should not have an affect to an isolated module registry.

describe('Isolated modules test', () => {
  test('should be able to return different isolated modules', () => {
    let m0 = require('../path/to/testing/module')
    // let m0
    // jest.isolateModules(() => {
    //   m0 = require('../path/to/testing/module')
    // })

    let m1
    jest.isolateModules(() => {
      m1 = require('../path/to/testing/module')
    })

    expect(m1).toStrictEqual(m0) // should fail.
  })
})

Run npx envinfo --preset jest

npx: installed 1 in 2.159s

  System:
    OS: macOS 10.14.1
    CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
  Binaries:
    Node: 8.15.0 - ~/.nvm/versions/node/v8.15.0/bin/node
    Yarn: 1.9.4 - ~/.yarn/bin/yarn
    npm: 6.4.1 - ~/.nvm/versions/node/v8.15.0/bin/npm
  npmPackages:
    jest: ^24.1.0 => 24.1.0
@SimenB
Copy link
Member

SimenB commented Feb 11, 2019

@rogeliog mind taking a look at this?

@wincent
Copy link
Contributor

wincent commented Jun 12, 2019

I also ran into this. My use case is:

const thing = require('../thing');

describe('thing', () => {
   // many tests involving thing
   // ...

    test('some special case', () => {
      jest.isolateModules(() => {
        jest.mock('some-module-that-thing-interacts-with', ...);

        thing = require('../thing');
        // some assertion involving thing
      });
    });
});

@matthias-pichler
Copy link

I think this should at least be documented since the docs actually show a second import outside the isolatedModules:

let myModule;
jest.isolateModules(() => {
  myModule = require('myModule');
});

const otherCopyOfMyModule = require('myModule');

@Jazzmanpw
Copy link

Any progress on this issue?

@toomuchdesign
Copy link
Contributor

I'm working on patch for this issue, and the behaviour seems to be originated here, where requireModule decides which registry to use:

      if (
        this._moduleRegistry.get(modulePath) ||
        !this._isolatedModuleRegistry
      ) {
        moduleRegistry = this._moduleRegistry;
      } else {
        moduleRegistry = this._isolatedModuleRegistry;
      }
    }

The if condition seems quite a bit too explicit to look like a bug. I'm wandering whether this was the intended behaviour or not.

@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
Projects
None yet
6 participants