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

Code before:
            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);

              meta.resolve = (
                specifier: string,
                parent?: string | URL,
              ): string | null => {
                let filename;
                let dirname;

                if (typeof parent === 'string') {
                  // Check if parent is a valid file URL
                  if (parent.startsWith('file:///')) {
                    filename = path.resolve(fileURLToPath(parent), specifier);
                    dirname = `./${parent.replace('file:///', '')}`; // Convert file URL to relative path
                  } else {
                    // Parent is a non-URL string; treat as a file path
                    filename = path.resolve(parent, specifier);
                    dirname = parent;
                  }
                } else {
                  // No valid parent provided fallback to module's URL
                  filename = fileURLToPath(meta.url);
                  dirname = path.dirname(filename);
                }

                // Configure the module resolver
                const moduleMap = ModuleMap.create('/');
                const resolver = new Resolver(moduleMap, {

Review comment (suggestion):
```suggestion
              meta.resolve = async (
```

Code after:
            ) => {
              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) => {
              const metaUrl = pathToFileURL(modulePath).href;
              meta.url = metaUrl;

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

              // @ts-expect-error It should not be async. Will be fixed when updated to @types/node@20.11.0
              meta.resolve = (specifier, parent = metaUrl) => {
                const parentPath = fileURLToPath(parent);

                const resolvedPath = this._resolver.resolveModule(
                  parentPath,
                  specifier,
                  {conditions: this.esmConditions},
                );

                return pathToFileURL(resolvedPath).href;
              };

              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!`,