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 = async (
                specifier: string,
                parent?: string | URL,
              ): Promise<string> => {
                let filename: string;
                let dirname: string;

                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);
                }

                const resolvedPath =
                  this._resolver.resolveModuleFromDirIfExists(
                    dirname,

Review comment (suggestion):
can this use `this._resolveModule`? then it should be enough to just handle the URL bit, and not have to care about `this._resolver` at all

Code after:
            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) => {
              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;
            },
          });
        }