title: NodeJs import issue with Jest
body: <p><a href="https://github.com/facebook/jest/issues/10944" rel="nofollow noreferrer">Issue on facebook/jest</a></p>
  <p>Currently working on a project where I need to dynamically load other components in the main application system, I'm facing an issue with some error that throws a Jest Invariant constraint violation when trying to import dynamically a module.</p>
  <p>An example is accessible at <a href="https://gitlab.com/matthieu88160/stryker-issue" rel="nofollow noreferrer">https://gitlab.com/matthieu88160/stryker-issue</a></p>
  <p>In this project, I have a class used to load a component and another that will use the ComponentLoader and add some check and post-processing (this part of the code is not present in the accessible project).
  In the GitLab job, two test suites are executed and one fails. My problem is that the two test suites are exactly the same, obviously, I copy-pasted the first one to create. The second one and by the way demonstrate the issue.</p>
  <p>I already tried some workaround found on the web without any success.</p>
  <p>Here is the code of the component I try to test :</p>
  <pre><code>import {existsSync} from 'fs';
  import {format} from 'util';
  
  export default class ComponentLoader
  {
      static load(file) {
          if(existsSync(file)) {
              return import(file);
          }
  
          throw new Error(
              format('Component file &quot;%s&quot; does not exist. Cannot load module', file)
          );
      }
  }
  </code></pre>
  <p>And the test itself:</p>
  <pre><code>import {describe, expect, test} from '@jest/globals';
  import {format} from 'util';
  import {fileURLToPath} from 'url';
  import {dirname} from 'path';
  import mock from 'jest-mock';
  
  describe(
      'ComponentLoader',
      () =&gt; {
          describe('load', () =&gt; {
              test('load method is able to load a component from a file', () =&gt; new Promise(
                  resolve =&gt; {
                      Promise.all(
                          [import('../src/ComponentLoader.mjs'), import('./fixture/component/A1/component.fixture.mjs')]
                      ).then(modules =&gt; {
                          const file = format(
                              '%s/./fixture/component/A1/component.fixture.mjs',
                              dirname(fileURLToPath(import.meta.url))
                          );
  
                          modules[0].default.load(file).then(obj =&gt; {
                              expect(obj).toBe(modules[1]);
                              resolve();
                          });
                      });
                  })
              );
          });
      }
  );
  </code></pre>
  <p>And here the error report:</p>
  <pre><code>PASS test/ComponentLoaderA.test.mjs
  FAIL test/ComponentLoaderB.test.mjs
    ● ComponentLoader › load › load method is able to load a component from a file
        15 |     static load(file) {
        16 |         if(existsSync(file)) {
      &gt; 17 |             return import(file);
           |             ^
        18 |         }
        19 | 
        20 |         throw new Error(
        at invariant (node_modules/jest-runtime/build/index.js:2004:11)
        at Function.load (src/ComponentLoader.mjs:17:13)
        at test/ComponentLoaderB.test.mjs:21:44
  </code></pre>
  <p>The interesting element from my point of view is the fact the <code>ComponentLoaderA.test.mjs</code> and the <code>ComponentLoaderB.test.mjs</code> are exactly the same.</p>
  <p>The full error trace I found is:</p>
  <pre><code>CComponentLoader load load method is able to load a component from a file
          Error:
      at invariant (importTest/.stryker-tmp/sandbox7042873/node_modules/jest-runtime/build/index.js:2004:11)
      at Runtime.loadEsmModule (importTest/.stryker-tmp/sandbox7042873/node_modules/jest-runtime/build/index.js:534:7)
      at Runtime.linkModules (importTest/.stryker-tmp/sandbox7042873/node_modules/jest-runtime/build/index.js:616:19)
      at importModuleDynamically (importTest/.stryker-tmp/sandbox7042873/node_modules/jest-runtime/build/index.js:555:16)
      at importModuleDynamicallyWrapper (internal/vm/module.js:443:21)
      at exports.importModuleDynamicallyCallback (internal/process/esm_loader.js:30:14)
      at Function.load (importTest/.stryker-tmp/sandbox7042873/src/ComponentLoader.mjs:89:11)
      at importTest/.stryker-tmp/sandbox7042873/test/ComponentLoaderB.test.mjs:22:37
      at new Promise (&lt;anonymous&gt;)
      at Object.&lt;anonymous&gt; (importTest/.stryker-tmp/sandbox7042873/test/ComponentLoaderB.test.mjs:14:79)
  </code></pre>
  <p>It seems the error does not have any message.</p>
  <h2>Further information from the jest-runtime investigation :</h2>
  <p>It seems that between the tests, the sandbox context is lost for a reason I cannot be able to manage to find at the moment.</p>
  <p>In node_modules/jest-runtime/build/index.js:2004:11 :
  The condition is NULL, the error is then thrown without a message.</p>
  <pre><code>function invariant(condition, message) {
    if (!condition) {
      throw new Error(message);
    }
  }
  </code></pre>
  <p>In node_modules/jest-runtime/build/index.js:534:7 :
  The context is NULL, and no message given, creating my empty error message.</p>
  <pre><code>const context = this._environment.getVmContext();
  
  invariant(context);
  </code></pre>
  <p>The toString() of this._environment.getVmContext method as follow:</p>
  <pre><code>getVmContext() {
      return this.context;
  }
  </code></pre>
  <p>The current _environment presents a null context :</p>
  <pre><code>NodeEnvironment {
    context: null,
    fakeTimers: null,
    [...]
  }
  </code></pre>
  <p>The deeper point I can reach is this code where the context appear to be null :</p>
  <pre><code>const module = new (_vm().SourceTextModule)(transformedCode, {
    context,
    identifier: modulePath,
    importModuleDynamically: (specifier, referencingModule) =&gt; {
      return this.linkModules(
        specifier,
        referencingModule.identifier,
        referencingModule.context
      )},
  
    initializeImportMeta(meta) {
      meta.url = (0, _url().pathToFileURL)(modulePath).href;
    }
  });
  </code></pre>
  <p>The context variable is not empty and <code>_vm().SourceTextModule</code> is a class extending Module.
  I can notice in the <code>importModuleDynamically</code> execution using <code>console.log(this._environment)</code> that the context is currently null.</p>