Skip to content

Commit bdb86aa

Browse files
authored
feat: support import.meta.dirname and import.meta.filename (#14854)
1 parent 7239acf commit bdb86aa

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- `[@jest/fake-timers]` [**BREAKING**] Upgrade `@sinonjs/fake-timers` to v11 ([#14544](https://github.com/jestjs/jest/pull/14544))
1919
- `[@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))
2020
- `[jest-runtime]` Exposing new modern timers function `jest.advanceTimersToFrame()` from `@jest/fake-timers` ([#14598](https://github.com/jestjs/jest/pull/14598))
21+
- `[jest-runtime]` Support `import.meta.filename` and `import.meta.dirname` (available from [Node 20.11](https://nodejs.org/en/blog/release/v20.11.0))
2122
- `[@jest/schemas]` Upgrade `@sinclair/typebox` to v0.31 ([#14072](https://github.com/jestjs/jest/pull/14072))
2223
- `[@jest/types]` `test.each()`: Accept a readonly (`as const`) table properly ([#14565](https://github.com/jestjs/jest/pull/14565))
2324
- `[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))

e2e/native-esm/__tests__/native-esm.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,34 @@ test('should have correct import.meta', () => {
2828
expect(typeof require).toBe('undefined');
2929
expect(typeof jest).toBe('undefined');
3030
expect(import.meta).toEqual({
31+
dirname: expect.any(String),
32+
filename: expect.any(String),
3133
jest: expect.anything(),
3234
url: expect.any(String),
3335
});
3436
expect(import.meta.jest).toBe(jestObject);
3537
expect(
3638
import.meta.url.endsWith('/e2e/native-esm/__tests__/native-esm.test.js'),
3739
).toBe(true);
40+
if (process.platform === 'win32') {
41+
expect(
42+
import.meta.filename.endsWith(
43+
'\\e2e\\native-esm\\__tests__\\native-esm.test.js',
44+
),
45+
).toBe(true);
46+
expect(import.meta.dirname.endsWith('\\e2e\\native-esm\\__tests__')).toBe(
47+
true,
48+
);
49+
} else {
50+
expect(
51+
import.meta.filename.endsWith(
52+
'/e2e/native-esm/__tests__/native-esm.test.js',
53+
),
54+
).toBe(true);
55+
expect(import.meta.dirname.endsWith('/e2e/native-esm/__tests__')).toBe(
56+
true,
57+
);
58+
}
3859
});
3960

4061
test('should double stuff', () => {

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+
// @ts-expect-error Jest uses @types/node@16. Will be fixed when updated to @types/[email protected]
524+
meta.filename = fileURLToPath(meta.url);
525+
// @ts-expect-error Jest uses @types/node@16. Will be fixed when updated to @types/[email protected]
526+
meta.dirname = path.dirname(meta.filename);
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.dirname(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)