Skip to content

Persist require.cache after globalSetup #6066

Closed
@faustbrian

Description

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions