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

Code before:
            {context, identifier: modulePath},
          );
        } else {
          module = new SourceTextModule(transformedCode, {
            context,
            identifier: modulePath,
            importModuleDynamically: async (
              specifier: string,
              referencingModule: VMModule,
            ) => {
              invariant(
                runtimeSupportsVmModules,
                'You need to run with a version of node that supports ES Modules in the VM API. See https://jestjs.io/docs/ecmascript-modules',
              );
              const module = await this.resolveModule(
                specifier,
                referencingModule.identifier,
                referencingModule.context,
              );

              return this.linkAndEvaluateModule(module);
            },
            initializeImportMeta: (meta: JestImportMeta) => {
              meta.url = pathToFileURL(modulePath).href;

              if (meta.url.startsWith('file://')) {
                meta.filename = fileURLToPath(meta.url);
                meta.dirname = path.resolve(meta.filename);
              }

              let jest = this.jestObjectCaches.get(modulePath);

              if (!jest) {
                jest = this._createJestObjectFor(modulePath);

                this.jestObjectCaches.set(modulePath, jest);
              }

              meta.jest = jest;
            },
          });
        }

        invariant(
          !registry.has(cacheKey),
          `Module cache already has entry ${cacheKey}. This is a bug in Jest, please report it!`,
        );

        registry.set(cacheKey, module);

        transformResolve();

Review comment (suggestion):
NOTE: The check here is because of the CAVEAT mention in nodejs docs (see https://nodejs.org/dist/latest-v20.x/docs/api/esm.html#importmetadirname)

Code after:
            {context, identifier: modulePath},
          );
        } else {
          module = new SourceTextModule(transformedCode, {
            context,
            identifier: modulePath,
            importModuleDynamically: async (
              specifier: string,
              referencingModule: VMModule,
            ) => {
              invariant(
                runtimeSupportsVmModules,
                'You need to run with a version of node that supports ES Modules in the VM API. See https://jestjs.io/docs/ecmascript-modules',
              );
              const module = await this.resolveModule(
                specifier,
                referencingModule.identifier,
                referencingModule.context,
              );

              return this.linkAndEvaluateModule(module);
            },
            initializeImportMeta: (meta: JestImportMeta) => {
              meta.url = pathToFileURL(modulePath).href;

              // @ts-expect-error Jest uses @types/node@16. Will be fixed when updated to @types/node@20.11.0
              meta.filename = fileURLToPath(meta.url);
              // @ts-expect-error Jest uses @types/node@16. Will be fixed when updated to @types/node@20.11.0
              meta.dirname = path.dirname(meta.filename);

              let jest = this.jestObjectCaches.get(modulePath);

              if (!jest) {
                jest = this._createJestObjectFor(modulePath);

                this.jestObjectCaches.set(modulePath, jest);
              }

              meta.jest = jest;
            },
          });
        }

        invariant(
          !registry.has(cacheKey),
          `Module cache already has entry ${cacheKey}. This is a bug in Jest, please report it!`,
        );

        registry.set(cacheKey, module);

        transformResolve();