Skip to content

Commit 7abba69

Browse files
committed
feat: support import.meta.dirname and import.meta.filename
1 parent 50fe2ee commit 7abba69

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Features
44

5+
- `[jest-runtime]` Support `import.meta.filename` and `import.meta.dirname` (available from [Node 20.11](https://nodejs.org/en/blog/release/v20.11.0))
56
- `[jest-circus, jest-cli, jest-config]` Add `waitNextEventLoopTurnForUnhandledRejectionEvents` flag to minimise performance impact of correct detection of unhandled promise rejections introduced in [#14315](https://github.com/jestjs/jest/pull/14315) ([#14681](https://github.com/jestjs/jest/pull/14681))
67
- `[jest-circus]` Add a `waitBeforeRetry` option to `jest.retryTimes` ([#14738](https://github.com/jestjs/jest/pull/14738))
78
- `[jest-circus, jest-jasmine2]` Allow `setupFilesAfterEnv` to export an async function ([#10962](https://github.com/jestjs/jest/issues/10962))

packages/jest-runtime/src/index.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,11 @@ export default class Runtime {
520520
initializeImportMeta: (meta: JestImportMeta) => {
521521
meta.url = pathToFileURL(modulePath).href;
522522

523+
if (meta.url.startsWith('file://')) {
524+
meta.filename = fileURLToPath(meta.url);
525+
meta.dirname = path.resolve(meta.filename);
526+
}
527+
523528
let jest = this.jestObjectCaches.get(modulePath);
524529

525530
if (!jest) {
@@ -672,6 +677,13 @@ export default class Runtime {
672677
initializeImportMeta(meta: ImportMeta) {
673678
// no `jest` here as it's not loaded in a file
674679
meta.url = specifier;
680+
681+
if (meta.url.startsWith('file://')) {
682+
// @ts-expect-error Jest uses @types/node@16. Will be fixed when updated to @types/[email protected]
683+
meta.filename = fileURLToPath(meta.url);
684+
// @ts-expect-error Jest uses @types/node@16. Will be fixed when updated to @types/[email protected]
685+
meta.dirname = path.resolve(meta.filename);
686+
}
675687
},
676688
});
677689
}
@@ -685,19 +697,22 @@ export default class Runtime {
685697
specifier = fileURLToPath(specifier);
686698
}
687699

688-
const [path, query] = specifier.split('?');
700+
const [specifierPath, query] = specifier.split('?');
689701

690702
if (
691703
await this._shouldMockModule(
692704
referencingIdentifier,
693-
path,
705+
specifierPath,
694706
this._explicitShouldMockModule,
695707
)
696708
) {
697-
return this.importMock(referencingIdentifier, path, context);
709+
return this.importMock(referencingIdentifier, specifierPath, context);
698710
}
699711

700-
const resolved = await this._resolveModule(referencingIdentifier, path);
712+
const resolved = await this._resolveModule(
713+
referencingIdentifier,
714+
specifierPath,
715+
);
701716

702717
if (
703718
// json files are modules when imported in modules

0 commit comments

Comments
 (0)