Skip to content

Commit

Permalink
feat: support import.meta.dirname and import.meta.filename (#14854)
Browse files Browse the repository at this point in the history
  • Loading branch information
alesmenzel authored Jan 16, 2024
1 parent 7239acf commit bdb86aa
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- `[@jest/fake-timers]` [**BREAKING**] Upgrade `@sinonjs/fake-timers` to v11 ([#14544](https://github.com/jestjs/jest/pull/14544))
- `[@jest/fake-timers]` Exposing new modern timers function `advanceTimersToFrame()` which advances all timers by the needed milliseconds to execute callbacks currently scheduled with `requestAnimationFrame` ([#14598](https://github.com/jestjs/jest/pull/14598))
- `[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))
- `[@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))
Expand Down
21 changes: 21 additions & 0 deletions e2e/native-esm/__tests__/native-esm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,34 @@ test('should have correct import.meta', () => {
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(),
url: expect.any(String),
});
expect(import.meta.jest).toBe(jestObject);
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,
);
} 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,
);
}
});

test('should double stuff', () => {
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;

// @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) {
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.dirname(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 bdb86aa

Please sign in to comment.