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

Persist require.cache after globalSetup #6066

Closed
faustbrian opened this issue Apr 25, 2018 · 4 comments
Closed

Persist require.cache after globalSetup #6066

faustbrian opened this issue Apr 25, 2018 · 4 comments

Comments

@faustbrian
Copy link

faustbrian commented Apr 25, 2018

What is the current behaviour?

When requiring and bootstrapping a node module (singleton in my case) during the globalSetup like require('some-node-module') it will be gone if I try to get the same instance in a test via require('some-node-module').

Outside of jest this behaviour works perfectly fine since node.js caches any require call which makes it easy to provide the same instance of a module between multiple modules.

What is the expected behaviour?

After calling require('some-node-module') in the globalSetup all tests should have access to instance of require('some-node-module').

Please provide your exact Jest configuration

module.exports = {
  testEnvironment: 'node',
  moduleFileExtensions: ['js', 'json'],
  globalSetup: './__tests__/setup.js',
  setupTestFrameworkScriptFile: 'jest-extended'
}

Run npx envinfo --preset jest in your project directory and paste the
results here

  System:
    OS: macOS High Sierra 10.13.4
    CPU: x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
  Binaries:
    Node: 9.11.1
    Yarn: 1.6.0
    npm: 5.8.0
  npmPackages:
    jest:
      wanted: ^22.4.3
      installed: 22.4.3

Notes

I found that jest ignores the node.js require.cache but is there some way to enable the require.cache for tests?

@SimenB
Copy link
Member

SimenB commented Apr 25, 2018

Jest implements its own require, node's require is not run within the sandbox. Sorta related to #6058

@SimenB SimenB closed this as completed Apr 25, 2018
@faustbrian
Copy link
Author

So there is absolutely no way to achieve this with jest, not even any workarounds?

@SimenB
Copy link
Member

SimenB commented Apr 25, 2018

Correct. It's meant to be used to e.g. setup a database (not a database connection) or start a browser (but not connect to it). Those things should be done per test (e.g. setupFiles)

@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 12, 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

2 participants