question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

🐛 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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:11
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
wincentcommented, 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
      });
    });
});
0reactions
github-actions[bot]commented, May 11, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is jest.isolateModules not working as expected?
I want to move away from using jest.resetModules since I want to reset two modules, but keep one through all the runs.
Read more >
[Amendment] SE-0306: Actors and `nonisolated let`
Hi everyone. We are starting review for an amendment to the accepted proposal SE-0306: Actors today, and running the review through May 28, ......
Read more >
Installation Manual of Solar Module2022
Do not operate when modules are wet unless you wear PPE(personal protective equipment). Please follow the cleaning requirements in this manual when cleaning ......
Read more >
AB_1794_Series_Datasheet.pdf - ArtisanTG
If desired, connect individual power supplies to each base to isolate modules. Plug the I/O module into the terminal base to connect the...
Read more >
LT0351 4100ES Fire Indicator Panel Operator's Manual
not require entry of a passcode. ZONE ISOLATIONS - The zone isolate status is not stored in non-volatile memory, so if the panel...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found