From e27bb5b15183857806db80777811f3b4a724d772 Mon Sep 17 00:00:00 2001 From: Grant Cheadle Date: Thu, 29 Feb 2024 13:41:17 -0800 Subject: [PATCH 1/9] import.meta.resolve() intro --- e2e/native-esm/__tests__/native-esm.test.js | 7 +++ packages/jest-runtime/src/index.ts | 56 ++++++++++++++++++++- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/e2e/native-esm/__tests__/native-esm.test.js b/e2e/native-esm/__tests__/native-esm.test.js index 7e0574a33963..ff446c2503f9 100644 --- a/e2e/native-esm/__tests__/native-esm.test.js +++ b/e2e/native-esm/__tests__/native-esm.test.js @@ -37,6 +37,13 @@ test('should have correct import.meta', () => { expect( import.meta.url.endsWith('/e2e/native-esm/__tests__/native-esm.test.js'), ).toBe(true); + expect(import.meta.resolve('colors')) + .endsWith('jest/node_modules/colors/lib/index.js') + .toBe(true); + expect(import.meta.resolve('./native-esm.test')) + .endsWith('jest/e2e/native-esm/__tests__/native-esm.test.js') + .toBe(true); + if (process.platform === 'win32') { expect( import.meta.filename.endsWith( diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 72d247e81588..ebeeb1fee7c7 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -4,7 +4,6 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ - import nativeModule = require('module'); import * as path from 'path'; import {URL, fileURLToPath, pathToFileURL} from 'url'; @@ -21,6 +20,7 @@ import { import {parse as parseCjs} from 'cjs-module-lexer'; import {CoverageInstrumenter, type V8Coverage} from 'collect-v8-coverage'; import * as fs from 'graceful-fs'; +import type {ResolverConfig} from 'jest-resolve/src/types'; import slash = require('slash'); import stripBOM = require('strip-bom'); import type { @@ -47,7 +47,11 @@ import { shouldInstrument, } from '@jest/transform'; import type {Config, Global} from '@jest/types'; -import HasteMap, {type IHasteMap, type IModuleMap} from 'jest-haste-map'; +import HasteMap, { + type IHasteMap, + type IModuleMap, + ModuleMap, +} from 'jest-haste-map'; import {formatStackTrace, separateMessageFromStack} from 'jest-message-util'; import type {MockMetadata, ModuleMocker} from 'jest-mock'; import {escapePathForRegex} from 'jest-regex-util'; @@ -525,6 +529,54 @@ export default class Runtime { // @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, { + defaultPlatform: 'node', + extensions: ['.js', '.json', '.ts', '.node', '.mjs'], + hasCoreModules: false, + } as ResolverConfig); + + const resolvedPath = resolver.resolveModuleFromDirIfExists( + dirname, + specifier, + ); + + // Check resolution result and format output + if (resolvedPath) { + // Convert the resolved path back to a URL if sparent was originally a URL otherwise return the path + return typeof parent === 'string' && + parent.startsWith('file:///') + ? pathToFileURL(resolvedPath).href + : resolvedPath; + } else { + return null; + } + }; + let jest = this.jestObjectCaches.get(modulePath); if (!jest) { From 246cb82379dc5c81abb414bc648de7eafb6cad7d Mon Sep 17 00:00:00 2001 From: Grant Cheadle Date: Fri, 1 Mar 2024 11:23:31 -0800 Subject: [PATCH 2/9] resolve changes --- e2e/native-esm/__tests__/native-esm.test.js | 19 ++++++++++++------- packages/jest-runtime/src/index.ts | 6 +++--- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/e2e/native-esm/__tests__/native-esm.test.js b/e2e/native-esm/__tests__/native-esm.test.js index ff446c2503f9..0fc2c7d66f4f 100644 --- a/e2e/native-esm/__tests__/native-esm.test.js +++ b/e2e/native-esm/__tests__/native-esm.test.js @@ -37,13 +37,6 @@ test('should have correct import.meta', () => { expect( import.meta.url.endsWith('/e2e/native-esm/__tests__/native-esm.test.js'), ).toBe(true); - expect(import.meta.resolve('colors')) - .endsWith('jest/node_modules/colors/lib/index.js') - .toBe(true); - expect(import.meta.resolve('./native-esm.test')) - .endsWith('jest/e2e/native-esm/__tests__/native-esm.test.js') - .toBe(true); - if (process.platform === 'win32') { expect( import.meta.filename.endsWith( @@ -53,6 +46,12 @@ test('should have correct import.meta', () => { expect(import.meta.dirname.endsWith('\\e2e\\native-esm\\__tests__')).toBe( true, ); + expect(import.meta.resolve('colors')) + .endsWith('\\jest\\node_modules\\colors\\lib\\index.js') + .toBe(true); + expect(import.meta.resolve('./native-esm.test')) + .endsWith('\\jest\\e2e\\native-esm\\__tests__\\native-esm.test.js') + .toBe(true); } else { expect( import.meta.filename.endsWith( @@ -62,6 +61,12 @@ test('should have correct import.meta', () => { expect(import.meta.dirname.endsWith('/e2e/native-esm/__tests__')).toBe( true, ); + expect(import.meta.resolve('colors')) + .endsWith('jest/node_modules/colors/lib/index.js') + .toBe(true); + expect(import.meta.resolve('./native-esm.test')) + .endsWith('jest/e2e/native-esm/__tests__/native-esm.test.js') + .toBe(true); } }); diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index ebeeb1fee7c7..a7b3b7270820 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -529,10 +529,10 @@ export default class Runtime { // @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 = ( + meta.resolve = async ( specifier: string, parent?: string | URL, - ): string | null => { + ): Promise => { let filename; let dirname; @@ -573,7 +573,7 @@ export default class Runtime { ? pathToFileURL(resolvedPath).href : resolvedPath; } else { - return null; + throw new Error('Cannot Resolve Path'); } }; From d548e688c54cb92199ad1751c317434b674b7272 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 2 Mar 2024 12:20:23 +0100 Subject: [PATCH 3/9] maybe a bit better --- packages/jest-runtime/src/index.ts | 36 +++++++++++------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index a7b3b7270820..35795dbb750a 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -4,6 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ + import nativeModule = require('module'); import * as path from 'path'; import {URL, fileURLToPath, pathToFileURL} from 'url'; @@ -20,7 +21,6 @@ import { import {parse as parseCjs} from 'cjs-module-lexer'; import {CoverageInstrumenter, type V8Coverage} from 'collect-v8-coverage'; import * as fs from 'graceful-fs'; -import type {ResolverConfig} from 'jest-resolve/src/types'; import slash = require('slash'); import stripBOM = require('strip-bom'); import type { @@ -47,11 +47,7 @@ import { shouldInstrument, } from '@jest/transform'; import type {Config, Global} from '@jest/types'; -import HasteMap, { - type IHasteMap, - type IModuleMap, - ModuleMap, -} from 'jest-haste-map'; +import HasteMap, {type IHasteMap, type IModuleMap} from 'jest-haste-map'; import {formatStackTrace, separateMessageFromStack} from 'jest-message-util'; import type {MockMetadata, ModuleMocker} from 'jest-mock'; import {escapePathForRegex} from 'jest-regex-util'; @@ -533,8 +529,8 @@ export default class Runtime { specifier: string, parent?: string | URL, ): Promise => { - let filename; - let dirname; + let filename: string; + let dirname: string; if (typeof parent === 'string') { // Check if parent is a valid file URL @@ -552,29 +548,23 @@ export default class Runtime { dirname = path.dirname(filename); } - // Configure the module resolver - const moduleMap = ModuleMap.create('/'); - const resolver = new Resolver(moduleMap, { - defaultPlatform: 'node', - extensions: ['.js', '.json', '.ts', '.node', '.mjs'], - hasCoreModules: false, - } as ResolverConfig); - - const resolvedPath = resolver.resolveModuleFromDirIfExists( - dirname, - specifier, - ); + const resolvedPath = + this._resolver.resolveModuleFromDirIfExists( + dirname, + specifier, + {conditions: this.esmConditions}, + ); // Check resolution result and format output if (resolvedPath) { - // Convert the resolved path back to a URL if sparent was originally a URL otherwise return the path + // Convert the resolved path back to a URL if parent was originally a URL otherwise return the path return typeof parent === 'string' && parent.startsWith('file:///') ? pathToFileURL(resolvedPath).href : resolvedPath; - } else { - throw new Error('Cannot Resolve Path'); } + + throw new Error('Cannot Resolve Path'); }; let jest = this.jestObjectCaches.get(modulePath); From a60273380d1bad1644eae822530a7f560ac73bde Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sat, 2 Mar 2024 12:35:24 +0100 Subject: [PATCH 4/9] simplify --- e2e/native-esm/__tests__/native-esm.test.js | 36 +++++++---- e2e/native-esm/package.json | 1 + e2e/native-esm/yarn.lock | 8 +++ packages/jest-runtime/src/index.ts | 68 ++++++++------------- 4 files changed, 59 insertions(+), 54 deletions(-) diff --git a/e2e/native-esm/__tests__/native-esm.test.js b/e2e/native-esm/__tests__/native-esm.test.js index 0fc2c7d66f4f..1dcc800a4809 100644 --- a/e2e/native-esm/__tests__/native-esm.test.js +++ b/e2e/native-esm/__tests__/native-esm.test.js @@ -24,13 +24,14 @@ import staticImportedStatefulWithQuery from '../stateful.mjs?query=1'; import staticImportedStatefulWithAnotherQuery from '../stateful.mjs?query=2'; /* eslint-enable */ -test('should have correct import.meta', () => { +test('should have correct import.meta', async () => { expect(typeof require).toBe('undefined'); expect(typeof jest).toBe('undefined'); expect(import.meta).toEqual({ dirname: expect.any(String), filename: expect.any(String), jest: expect.anything(), + resolve: expect.any(Function), url: expect.any(String), }); expect(import.meta.jest).toBe(jestObject); @@ -46,12 +47,16 @@ test('should have correct import.meta', () => { expect(import.meta.dirname.endsWith('\\e2e\\native-esm\\__tests__')).toBe( true, ); - expect(import.meta.resolve('colors')) - .endsWith('\\jest\\node_modules\\colors\\lib\\index.js') - .toBe(true); - expect(import.meta.resolve('./native-esm.test')) - .endsWith('\\jest\\e2e\\native-esm\\__tests__\\native-esm.test.js') - .toBe(true); + expect( + (await import.meta.resolve('colors')).endsWith( + '\\jest\\e2e\\native-esm\\node_modules\\colors\\lib\\index.js', + ), + ).toBe(true); + expect( + (await import.meta.resolve('./native-esm.test')).endsWith( + '\\jest\\e2e\\native-esm\\__tests__\\native-esm.test.js', + ), + ).toBe(true); } else { expect( import.meta.filename.endsWith( @@ -61,12 +66,16 @@ test('should have correct import.meta', () => { expect(import.meta.dirname.endsWith('/e2e/native-esm/__tests__')).toBe( true, ); - expect(import.meta.resolve('colors')) - .endsWith('jest/node_modules/colors/lib/index.js') - .toBe(true); - expect(import.meta.resolve('./native-esm.test')) - .endsWith('jest/e2e/native-esm/__tests__/native-esm.test.js') - .toBe(true); + expect( + (await import.meta.resolve('colors')).endsWith( + 'jest/e2e/native-esm/node_modules/colors/lib/index.js', + ), + ).toBe(true); + expect( + (await import.meta.resolve('./native-esm.test')).endsWith( + 'jest/e2e/native-esm/__tests__/native-esm.test.js', + ), + ).toBe(true); } }); @@ -80,6 +89,7 @@ test('should support importing node core modules', () => { expect(JSON.parse(readFileSync(packageJsonPath, 'utf8'))).toEqual({ devDependencies: { + colors: '^1.4.0', 'discord.js': '14.3.0', 'iso-constants': '^0.1.2', yargs: '^17.5.1', diff --git a/e2e/native-esm/package.json b/e2e/native-esm/package.json index c113bde36bab..021655db00d0 100644 --- a/e2e/native-esm/package.json +++ b/e2e/native-esm/package.json @@ -4,6 +4,7 @@ "isolated-vm": "^4.6.0" }, "devDependencies": { + "colors": "^1.4.0", "discord.js": "14.3.0", "iso-constants": "^0.1.2", "yargs": "^17.5.1" diff --git a/e2e/native-esm/yarn.lock b/e2e/native-esm/yarn.lock index f12796649778..4b6d633f7356 100644 --- a/e2e/native-esm/yarn.lock +++ b/e2e/native-esm/yarn.lock @@ -333,6 +333,13 @@ __metadata: languageName: node linkType: hard +"colors@npm:^1.4.0": + version: 1.4.0 + resolution: "colors@npm:1.4.0" + checksum: 98aa2c2418ad87dedf25d781be69dc5fc5908e279d9d30c34d8b702e586a0474605b3a189511482b9d5ed0d20c867515d22749537f7bc546256c6014f3ebdcec + languageName: node + linkType: hard + "cross-spawn@npm:^7.0.0": version: 7.0.3 resolution: "cross-spawn@npm:7.0.3" @@ -1087,6 +1094,7 @@ __metadata: version: 0.0.0-use.local resolution: "root-workspace-0b6124@workspace:." dependencies: + colors: ^1.4.0 discord.js: 14.3.0 iso-constants: ^0.1.2 isolated-vm: ^4.6.0 diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 35795dbb750a..2e1f88dbb6f3 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -530,41 +530,30 @@ export default class Runtime { parent?: string | URL, ): Promise => { 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); + filename = modulePath; } - const resolvedPath = - this._resolver.resolveModuleFromDirIfExists( - dirname, - specifier, - {conditions: this.esmConditions}, - ); - - // Check resolution result and format output - if (resolvedPath) { - // Convert the resolved path back to a URL if parent was originally a URL otherwise return the path - return typeof parent === 'string' && - parent.startsWith('file:///') - ? pathToFileURL(resolvedPath).href - : resolvedPath; - } + const resolvedPath = await this._resolveModule( + filename, + specifier, + ); - throw new Error('Cannot Resolve Path'); + // Convert the resolved path back to a URL if parent was originally a URL otherwise return the path + return typeof parent === 'string' && + parent.startsWith('file:///') + ? pathToFileURL(resolvedPath).href + : resolvedPath; }; let jest = this.jestObjectCaches.get(modulePath); @@ -1508,28 +1497,25 @@ export default class Runtime { if (module) { return module; } - } else { - const {paths} = options; - if (paths) { - for (const p of paths) { - const absolutePath = path.resolve(from, '..', p); - const module = this._resolver.resolveModuleFromDirIfExists( - absolutePath, - moduleName, - // required to also resolve files without leading './' directly in the path - {conditions: this.cjsConditions, paths: [absolutePath]}, - ); - if (module) { - return module; - } - } - - throw new Resolver.ModuleNotFoundError( - `Cannot resolve module '${moduleName}' from paths ['${paths.join( - "', '", - )}'] from ${from}`, + } else if (options.paths) { + for (const p of options.paths) { + const absolutePath = path.resolve(from, '..', p); + const module = this._resolver.resolveModuleFromDirIfExists( + absolutePath, + moduleName, + // required to also resolve files without leading './' directly in the path + {conditions: this.cjsConditions, paths: [absolutePath]}, ); + if (module) { + return module; + } } + + throw new Resolver.ModuleNotFoundError( + `Cannot resolve module '${moduleName}' from paths ['${options.paths.join( + "', '", + )}'] from ${from}`, + ); } try { From ee68cd0e51538a599d24864ffa05c2597eb3b0b1 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 3 Mar 2024 02:04:33 +0100 Subject: [PATCH 5/9] no explicit types --- packages/jest-runtime/src/index.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 2e1f88dbb6f3..f42aa47662a3 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -525,10 +525,7 @@ export default class Runtime { // @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 => { + meta.resolve = async (specifier, parent) => { let filename: string; if (typeof parent === 'string') { From 4796be5a87fcfb1ac8e27a36f46e5845e68f8dbc Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 3 Mar 2024 02:05:10 +0100 Subject: [PATCH 6/9] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2dd36349cab5..13752c82a913 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - `[jest-mock]` Add support for the Explicit Resource Management proposal to use the `using` keyword with `jest.spyOn(object, methodName)` ([#14895](https://github.com/jestjs/jest/pull/14895)) - `[jest-runtime]` Exposing new modern timers function `jest.advanceTimersToFrame()` from `@jest/fake-timers` ([#14598](https://github.com/jestjs/jest/pull/14598)) - `[jest-runtime]` Support `import.meta.filename` and `import.meta.dirname` (available from [Node 20.11](https://nodejs.org/en/blog/release/v20.11.0)) ([#14854](https://github.com/jestjs/jest/pull/14854)) +- `[jest-runtime]` Support `import.meta.resolve` ([#14930](https://github.com/jestjs/jest/pull/14930)) - `[@jest/schemas]` Upgrade `@sinclair/typebox` to v0.31 ([#14072](https://github.com/jestjs/jest/pull/14072)) - `[@jest/types]` `test.each()`: Accept a readonly (`as const`) table properly ([#14565](https://github.com/jestjs/jest/pull/14565)) - `[jest-snapshot]` [**BREAKING**] Add support for [Error causes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) in snapshots ([#13965](https://github.com/facebook/jest/pull/13965)) From 4ce568ee8bc9856916a3810c7e9e16e3f22d22ce Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 3 Mar 2024 14:58:15 +0100 Subject: [PATCH 7/9] always return file url --- e2e/native-esm/__tests__/native-esm.test.js | 55 ++++++--------------- packages/jest-runtime/src/index.ts | 6 +-- 2 files changed, 17 insertions(+), 44 deletions(-) diff --git a/e2e/native-esm/__tests__/native-esm.test.js b/e2e/native-esm/__tests__/native-esm.test.js index 1dcc800a4809..71889c1291dc 100644 --- a/e2e/native-esm/__tests__/native-esm.test.js +++ b/e2e/native-esm/__tests__/native-esm.test.js @@ -38,45 +38,22 @@ test('should have correct import.meta', async () => { expect( import.meta.url.endsWith('/e2e/native-esm/__tests__/native-esm.test.js'), ).toBe(true); - if (process.platform === 'win32') { - expect( - import.meta.filename.endsWith( - '\\e2e\\native-esm\\__tests__\\native-esm.test.js', - ), - ).toBe(true); - expect(import.meta.dirname.endsWith('\\e2e\\native-esm\\__tests__')).toBe( - true, - ); - expect( - (await import.meta.resolve('colors')).endsWith( - '\\jest\\e2e\\native-esm\\node_modules\\colors\\lib\\index.js', - ), - ).toBe(true); - expect( - (await import.meta.resolve('./native-esm.test')).endsWith( - '\\jest\\e2e\\native-esm\\__tests__\\native-esm.test.js', - ), - ).toBe(true); - } else { - expect( - import.meta.filename.endsWith( - '/e2e/native-esm/__tests__/native-esm.test.js', - ), - ).toBe(true); - expect(import.meta.dirname.endsWith('/e2e/native-esm/__tests__')).toBe( - true, - ); - expect( - (await import.meta.resolve('colors')).endsWith( - 'jest/e2e/native-esm/node_modules/colors/lib/index.js', - ), - ).toBe(true); - expect( - (await import.meta.resolve('./native-esm.test')).endsWith( - 'jest/e2e/native-esm/__tests__/native-esm.test.js', - ), - ).toBe(true); - } + expect( + import.meta.filename.endsWith( + '/e2e/native-esm/__tests__/native-esm.test.js', + ), + ).toBe(true); + expect(import.meta.dirname.endsWith('/e2e/native-esm/__tests__')).toBe(true); + expect( + (await import.meta.resolve('colors')).endsWith( + 'jest/e2e/native-esm/node_modules/colors/lib/index.js', + ), + ).toBe(true); + expect( + (await import.meta.resolve('./native-esm.test')).endsWith( + 'jest/e2e/native-esm/__tests__/native-esm.test.js', + ), + ).toBe(true); }); test('should double stuff', () => { diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index f42aa47662a3..aacb56fff209 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -546,11 +546,7 @@ export default class Runtime { specifier, ); - // Convert the resolved path back to a URL if parent was originally a URL otherwise return the path - return typeof parent === 'string' && - parent.startsWith('file:///') - ? pathToFileURL(resolvedPath).href - : resolvedPath; + return pathToFileURL(resolvedPath).href; }; let jest = this.jestObjectCaches.get(modulePath); From 74f93e705a0458530c61a06161da916c90952d5d Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 3 Mar 2024 15:07:05 +0100 Subject: [PATCH 8/9] it should be sync --- e2e/native-esm/__tests__/native-esm.test.js | 14 ++++----- packages/jest-runtime/src/index.ts | 34 ++++++++------------- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/e2e/native-esm/__tests__/native-esm.test.js b/e2e/native-esm/__tests__/native-esm.test.js index 71889c1291dc..0aff2063ee5e 100644 --- a/e2e/native-esm/__tests__/native-esm.test.js +++ b/e2e/native-esm/__tests__/native-esm.test.js @@ -24,7 +24,7 @@ import staticImportedStatefulWithQuery from '../stateful.mjs?query=1'; import staticImportedStatefulWithAnotherQuery from '../stateful.mjs?query=2'; /* eslint-enable */ -test('should have correct import.meta', async () => { +test('should have correct import.meta', () => { expect(typeof require).toBe('undefined'); expect(typeof jest).toBe('undefined'); expect(import.meta).toEqual({ @@ -45,14 +45,14 @@ test('should have correct import.meta', async () => { ).toBe(true); expect(import.meta.dirname.endsWith('/e2e/native-esm/__tests__')).toBe(true); expect( - (await import.meta.resolve('colors')).endsWith( - 'jest/e2e/native-esm/node_modules/colors/lib/index.js', - ), + import.meta + .resolve('colors') + .endsWith('jest/e2e/native-esm/node_modules/colors/lib/index.js'), ).toBe(true); expect( - (await import.meta.resolve('./native-esm.test')).endsWith( - 'jest/e2e/native-esm/__tests__/native-esm.test.js', - ), + import.meta + .resolve('./native-esm.test') + .endsWith('jest/e2e/native-esm/__tests__/native-esm.test.js'), ).toBe(true); }); diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index aacb56fff209..1c004f26ca40 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -518,32 +518,22 @@ export default class Runtime { return this.linkAndEvaluateModule(module); }, initializeImportMeta: (meta: JestImportMeta) => { - meta.url = pathToFileURL(modulePath).href; + 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 = fileURLToPath(meta.url); + 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(meta.filename); - - meta.resolve = async (specifier, parent) => { - let filename: string; - - if (typeof parent === 'string') { - // Check if parent is a valid file URL - if (parent.startsWith('file:///')) { - filename = path.resolve(fileURLToPath(parent), specifier); - } else { - // Parent is a non-URL string; treat as a file path - filename = path.resolve(parent, specifier); - } - } else { - // No valid parent provided fallback to module's URL - filename = modulePath; - } - - const resolvedPath = await this._resolveModule( - filename, + 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; From 8178a6a1ad0778fe43dc5fb5faf383be039a8bd3 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 3 Mar 2024 15:11:22 +0100 Subject: [PATCH 9/9] oops --- e2e/native-esm/__tests__/native-esm.test.js | 25 ++++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/e2e/native-esm/__tests__/native-esm.test.js b/e2e/native-esm/__tests__/native-esm.test.js index 0aff2063ee5e..90de1bfa7e9d 100644 --- a/e2e/native-esm/__tests__/native-esm.test.js +++ b/e2e/native-esm/__tests__/native-esm.test.js @@ -38,12 +38,25 @@ test('should have correct import.meta', () => { expect( import.meta.url.endsWith('/e2e/native-esm/__tests__/native-esm.test.js'), ).toBe(true); - expect( - import.meta.filename.endsWith( - '/e2e/native-esm/__tests__/native-esm.test.js', - ), - ).toBe(true); - expect(import.meta.dirname.endsWith('/e2e/native-esm/__tests__')).toBe(true); + if (process.platform === 'win32') { + expect( + import.meta.filename.endsWith( + '\\e2e\\native-esm\\__tests__\\native-esm.test.js', + ), + ).toBe(true); + expect(import.meta.dirname.endsWith('\\e2e\\native-esm\\__tests__')).toBe( + true, + ); + } else { + expect( + import.meta.filename.endsWith( + '/e2e/native-esm/__tests__/native-esm.test.js', + ), + ).toBe(true); + expect(import.meta.dirname.endsWith('/e2e/native-esm/__tests__')).toBe( + true, + ); + } expect( import.meta .resolve('colors')