-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support import.meta.dirname and import.meta.filename
- Loading branch information
Ales Menzel
committed
Jan 15, 2024
1 parent
50fe2ee
commit 4c44b56
Showing
2 changed files
with
20 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -520,6 +520,11 @@ export default class Runtime { | |
initializeImportMeta: (meta: JestImportMeta) => { | ||
meta.url = pathToFileURL(modulePath).href; | ||
|
||
if (meta.url.startsWith('file://')) { | ||
meta.filename = fileURLToPath(meta.url); | ||
Check failure on line 524 in packages/jest-runtime/src/index.ts GitHub Actions / Typecheck Examples and Tests
|
||
meta.dirname = path.resolve(meta.filename); | ||
Check failure on line 525 in packages/jest-runtime/src/index.ts GitHub Actions / Typecheck Examples and Tests
Check failure on line 525 in packages/jest-runtime/src/index.ts GitHub Actions / Typecheck Examples and Tests
Check failure on line 525 in packages/jest-runtime/src/index.ts GitHub Actions / TypeScript Compatibility
|
||
} | ||
|
||
let jest = this.jestObjectCaches.get(modulePath); | ||
|
||
if (!jest) { | ||
|
@@ -672,6 +677,13 @@ export default class Runtime { | |
initializeImportMeta(meta: ImportMeta) { | ||
// no `jest` here as it's not loaded in a file | ||
meta.url = specifier; | ||
|
||
if (meta.url.startsWith('file://')) { | ||
// @ts-expect-error Jest uses @types/node@16. Will be fixed when updated to @types/[email protected] | ||
meta.filename = fileURLToPath(meta.url); | ||
// @ts-expect-error Jest uses @types/node@16. Will be fixed when updated to @types/[email protected] | ||
meta.dirname = path.resolve(meta.filename); | ||
} | ||
}, | ||
}); | ||
} | ||
|
@@ -685,19 +697,22 @@ export default class Runtime { | |
specifier = fileURLToPath(specifier); | ||
} | ||
|
||
const [path, query] = specifier.split('?'); | ||
const [specifierPath, query] = specifier.split('?'); | ||
|
||
if ( | ||
await this._shouldMockModule( | ||
referencingIdentifier, | ||
path, | ||
specifierPath, | ||
this._explicitShouldMockModule, | ||
) | ||
) { | ||
return this.importMock(referencingIdentifier, path, context); | ||
return this.importMock(referencingIdentifier, specifierPath, context); | ||
} | ||
|
||
const resolved = await this._resolveModule(referencingIdentifier, path); | ||
const resolved = await this._resolveModule( | ||
referencingIdentifier, | ||
specifierPath, | ||
); | ||
|
||
if ( | ||
// json files are modules when imported in modules | ||
|