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.

Scoped module initialization / cleanup

See original GitHub issue

🚀 Feature Proposal

Like jest.resetModules(), but only forgets whatever was initialized inside an explicit scope.

jest.withResetModules(() => {
  // Jest will snapshot the module registry when we enter,
  // and reset any newly loaded (!) modules when we exit.
});

Open to better naming.

Motivation

We often use jest.resetModules() in the React codebase to isolate individual tests.

However, sometimes we also use jest.resetModules() for another purpose. We have some code that’s shared between different packages, but ends up being bundled (i.e. copied) into all of them after the build time. But we run Jest on the source (building would’ve been too slow).

As a result, with our setup there might be some accidentally shared state when running the tests that doesn’t end up being shared in practice after the build. This can lead to weird bugs where tests tell us everything is okay, but it breaks in production. We’ve reduced this somewhat by running a subset of tests on compiled bundles, but this is still complicating some further work we need to do.

Usually we work around it like this:

React = require('react');
ReactDOM = require('react-dom');

jest.resetModules();
ReactDOMServer = require('react-dom/server');

This ensures ReactDOM and ReactDOMServer are properly isolated even if they happen to share some internal modules. The problem is that this wipes out all the modules, including React. In some cases it matters for us that React stays the same, but modules that were loaded when initializing ReactDOM or ReactDOMServer are discarded.

Example

var React;
var ReactDOM;
var ReactTestUtils;
var ReactDOMServer;

// React (and whatever it loaded) should be reused
React = require('react');

// Load some modules and then clear them from the cache
jest.withResetModules(() => {
  ReactDOM = require('react-dom');
  ReactTestUtils = require('react-dom/test-utils');
  // When we exit the scope, not only react-dom and react-dom/test-utils
  // are cleared from the cache, but also any their transitive dependencies
  // that have been loaded for the first time inside the current scope.
});

// Load other modules and then clear them from the cache
jest.withResetModules(() => {
  ReactDOMServer = require('react-dom/server');
});

Pitch

I tried my best above. Maybe if there was a way to clear individual modules that would work for me although I need to clear all recursive dependencies that haven’t already been loaded earlier.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
SimenBcommented, Jul 13, 2018

That it has to happen within a single test case (it/test). To achieve reuse the user can extract it into a function, but I don’t think it’s possible to make this persist between/across tests

0reactions
github-actions[bot]commented, May 12, 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

Developers - Scoped module initialization / cleanup - - Bountysource
Scoped module initialization / cleanup. ... resetModules() , but only forgets whatever was initialized inside an explicit scope.
Read more >
Dagger 2: Scoping and handling release/cleanup of provided ...
A Module just helps creating objects—especially if there is no injectable constructor or it needs further initialization—and is not aware of further ...
Read more >
Isolating Extension Modules — Python 3.11.1 documentation
Per-module state provides an easy way to think about lifetime and resource ownership: the extension module will initialize when a module object is...
Read more >
pytest fixtures: explicit, modular, scalable
Software test fixtures initialize test functions. ... Extending the previous example, we can add a scope="module" parameter to the @pytest.fixture ...
Read more >
Lifecycle events | NestJS - A progressive Node.js framework
onApplicationBootstrap(), Called once all modules have been initialized, ... Request-scoped classes are not tied to the application lifecycle and their ...
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