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

jest-resolve not honoring require cache #5120

Closed
OzairP opened this issue Dec 18, 2017 · 11 comments
Closed

jest-resolve not honoring require cache #5120

OzairP opened this issue Dec 18, 2017 · 11 comments

Comments

@OzairP
Copy link

OzairP commented Dec 18, 2017

Do you want to request a feature or report a bug? Bug

What is the current behavior? jest-resolve or some other module does not honor require.cache

If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
repository on GitHub that we can yarn install and yarn test.

https://github.com/OzairP/jest-require-cache-no-honor

What is the expected behavior?
Tests should pass

Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.

node: v8.6.0
npm: 5.3.0
yarn: 1.3.2
jest: 22.0.0

My current project hosts a tiny dependency injection container that is a singleton. When the app is setup it registers dependencies but in my tests I'm unable to mock my dependencies through my own container since the singleton is not shared between the test and the app.

Console screenshot: Note "index.js has someDep" line, this is from the setup
console

@cpojer
Copy link
Member

cpojer commented Dec 18, 2017

Jest does not implement require.cache. This is by design. If you want to wipe all modules, run jest.resetModules() in your test code.

@cpojer cpojer closed this as completed Dec 18, 2017
@OzairP
Copy link
Author

OzairP commented Dec 18, 2017

@cpojer when/where should jest.resetModules() be called. Is there any way to run jest without the jest-resolve or the patched require?

@SimenB
Copy link
Member

SimenB commented Dec 18, 2017

when/where should jest.resetModules() be called.

You can do it in an afterEach for instance, and have the requires in your tests.

Is there any way to run jest without the jest-resolve or the patched require

No. resetModules should be enough for your use case though, wherever you have your delete require.cache today

@OzairP
Copy link
Author

OzairP commented Dec 20, 2017

@SimenB I read through what jest.resetModules does and it is not the correct solution to my issue.

My issue is that any singletons like my DI Container I have in my project, which works based on require.cache. However, in my testing environment, any require'd singletons are reset without the cache.

My wish is to require any singletons through require.cache so I receive the same instance as I do from the project itself.

@OzairP
Copy link
Author

OzairP commented Dec 21, 2017

@SimenB please do check the sample project. The singleton class is the same pattern I'm using in my projects. Let me know if I can clarify anything.

https://github.com/OzairP/jest-require-cache-no-honor

@Bradcomp
Copy link

@OzairP did you ever find a solution to this?

@OzairP
Copy link
Author

OzairP commented Jun 19, 2018

@Bradcomp

I made sure to register any singletons before running any tests, they should not register in any beforeEach/Any hooks.

@ekeric13
Copy link

@OzairP What do you mean by "register"? And are you "registering" them in the globalSetup file?

@dcalhoun
Copy link

@ekeric13 I spent several hours trying to understand several, seemingly cryptic, comments on how to get singletons working in Jest. I was finally able to get this working with the below configuration that I gleaned from this comment. For reference, the singleton I am mocking is a module for setting global configuration values for a Next.js app.

// jest.setup.js
import mockEnvConfig from '~/env-config.js';

jest.mock('next/config', () => () => ({ publicRuntimeConfig: mockEnvConfig }));

// jest.config.js
module.exports = {
  setupFiles: ['<rootDir>/jest.setup.js'],
};

@jaythomas
Copy link

jaythomas commented Jul 27, 2019

I was specifically looking in the documentation for the jest way of testing a singleton, what is normally accomplished with simply modifying the require.cache. I couldn't find what I was looking for and ended up on this old issue instead. So for anyone else that does I used jest.isolateModules(fn):

// Spec for testing event-bus.js
let eventBus

// Tear down stateful services
beforeEach(() => {
  jest.isolateModules(() => {
    eventBus = require('./event-bus')
  })  
})

Note that jest is the globally provided jest variable. Don't require/import jest because you'll just shadow it and the function will be undefined.

@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

7 participants