Repository: facebook/jest
File: packages/jest-runtime/src/index.ts (TypeScript)

Code before:
    throw new Error('The jest-runtime CLI has been moved into jest-repl');
  }

  static getCLIOptions(): never {
    throw new Error('The jest-runtime CLI has been moved into jest-repl');
  }

  // unstable as it should be replaced by https://github.com/nodejs/modules/issues/393, and we don't want people to use it
  unstable_shouldLoadAsEsm(modulePath: string): boolean {
    return (
      isWasm(modulePath) ||
      Resolver.unstable_shouldLoadAsEsm(
        modulePath,
        this._config.extensionsToTreatAsEsm,
      )
    );
  }

  private async loadEsmModule(
    modulePath: string,
    query = '',
  ): Promise<VMModule> {
    const cacheKey = modulePath + query;
    const registry = this._isolatedESMModuleRegistry
      ? this._isolatedESMModuleRegistry
      : this._esmoduleRegistry;

    if (this._fileTransformsMutex.has(cacheKey)) {
      await this._fileTransformsMutex.get(cacheKey);
    }

    if (!registry.has(cacheKey)) {
      invariant(
        typeof this._environment.getVmContext === 'function',
        'ES Modules are only supported if your test environment has the `getVmContext` function',
      );

      const context = this._environment.getVmContext();

      invariant(context, 'Test environment has been torn down');

      let transformResolve: () => void;
      let transformReject: (error?: unknown) => void;

      this._fileTransformsMutex.set(
        cacheKey,
        new Promise((resolve, reject) => {
          transformResolve = resolve;
          transformReject = reject;
        }),
      );

Review comment (bug):
Hm.. Isn’t it enough to use the same `_isolatedMockRegistry`? Did you try that?

It was my idea about missing `_isolatedESMModuleRegistry`, but it can I was wrong. Here we can see that `_isolatedMockRegistry` was simply not used while loading ES modules. Sorry about slow thinking. Could you try to `_isolatedMockRegistry` everywhere?

Code after:

  static getCLIOptions(): never {
    throw new Error('The jest-runtime CLI has been moved into jest-repl');
  }

  // unstable as it should be replaced by https://github.com/nodejs/modules/issues/393, and we don't want people to use it
  unstable_shouldLoadAsEsm(modulePath: string): boolean {
    return (
      isWasm(modulePath) ||
      Resolver.unstable_shouldLoadAsEsm(
        modulePath,
        this._config.extensionsToTreatAsEsm,
      )
    );
  }

  private async loadEsmModule(
    modulePath: string,
    query = '',
  ): Promise<VMModule> {
    const cacheKey = modulePath + query;
    const registry = this._isolatedModuleRegistry
      ? this._isolatedModuleRegistry
      : this._esmoduleRegistry;

    if (this._fileTransformsMutex.has(cacheKey)) {
      await this._fileTransformsMutex.get(cacheKey);
    }

    if (!registry.has(cacheKey)) {
      invariant(
        typeof this._environment.getVmContext === 'function',
        'ES Modules are only supported if your test environment has the `getVmContext` function',
      );

      const context = this._environment.getVmContext();

      invariant(context, 'Test environment has been torn down');

      let transformResolve: () => void;
      let transformReject: (error?: unknown) => void;

      this._fileTransformsMutex.set(
        cacheKey,
        new Promise((resolve, reject) => {
          transformResolve = resolve;
          transformReject = reject;
        }),
      );

      invariant(