Skip to content

Commit

Permalink
feat: support import.meta.dirname and import.meta.filename
Browse files Browse the repository at this point in the history
  • Loading branch information
Ales Menzel committed Jan 15, 2024
1 parent 50fe2ee commit 4c44b56
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features

- `[jest-runtime]` Support `import.meta.filename` and `import.meta.dirname` (available from [Node 20.11](https://nodejs.org/en/blog/release/v20.11.0))
- `[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))
- `[jest-circus]` Add a `waitBeforeRetry` option to `jest.retryTimes` ([#14738](https://github.com/jestjs/jest/pull/14738))
- `[jest-circus, jest-jasmine2]` Allow `setupFilesAfterEnv` to export an async function ([#10962](https://github.com/jestjs/jest/issues/10962))
Expand Down
23 changes: 19 additions & 4 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

View workflow job for this annotation

GitHub Actions / Typecheck Examples and Tests

Property 'filename' does not exist on type 'JestImportMeta'.

Check failure on line 524 in packages/jest-runtime/src/index.ts

View workflow job for this annotation

GitHub Actions / TypeScript Compatibility

Property 'filename' does not exist on type 'JestImportMeta'.
meta.dirname = path.resolve(meta.filename);

Check failure on line 525 in packages/jest-runtime/src/index.ts

View workflow job for this annotation

GitHub Actions / Typecheck Examples and Tests

Property 'dirname' does not exist on type 'JestImportMeta'.

Check failure on line 525 in packages/jest-runtime/src/index.ts

View workflow job for this annotation

GitHub Actions / Typecheck Examples and Tests

Property 'filename' does not exist on type 'JestImportMeta'.

Check failure on line 525 in packages/jest-runtime/src/index.ts

View workflow job for this annotation

GitHub Actions / TypeScript Compatibility

Property 'dirname' does not exist on type 'JestImportMeta'.

Check failure on line 525 in packages/jest-runtime/src/index.ts

View workflow job for this annotation

GitHub Actions / TypeScript Compatibility

Property 'filename' does not exist on type 'JestImportMeta'.
}

let jest = this.jestObjectCaches.get(modulePath);

if (!jest) {
Expand Down Expand Up @@ -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);
}
},
});
}
Expand All @@ -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
Expand Down

0 comments on commit 4c44b56

Please sign in to comment.