-
-
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 (#14854)
- Loading branch information
1 parent
7239acf
commit bdb86aa
Showing
3 changed files
with
41 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
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; | ||
|
||
// @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.dirname(meta.filename); | ||
|
||
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.dirname(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 | ||
|