Skip to content

Commit d67f783

Browse files
authored
feat: add import.meta.jest (#12698)
1 parent 6c068ff commit d67f783

5 files changed

Lines changed: 26 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
- `[jest-runtime]` [**BREAKING**] `Runtime.createHasteMap` now returns a promise ([#12008](https://github.com/facebook/jest/pull/12008))
5151
- `[jest-runtime]` Calling `jest.resetModules` function will clear FS and transform cache ([#12531](https://github.com/facebook/jest/pull/12531))
5252
- `[jest-runtime]` [**BREAKING**] Remove `Context` type export, it must be imported from `@jest/test-result` ([#12685](https://github.com/facebook/jest/pull/12685))
53+
- `[jest-runtime]` Add `import.meta.jest` ([#12698](https://github.com/facebook/jest/pull/12698))
5354
- `[@jest/schemas]` New module for JSON schemas for Jest's config ([#12384](https://github.com/facebook/jest/pull/12384))
5455
- `[@jest/source-map]` Migrate from `source-map` to `@jridgewell/trace-mapping` ([#12692](https://github.com/facebook/jest/pull/12692))
5556
- `[jest-transform]` [**BREAKING**] Make it required for `process()` and `processAsync()` methods to always return structured data ([#12638](https://github.com/facebook/jest/pull/12638))

docs/ECMAScriptModules.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,19 @@ With the warnings out of the way, this is how you activate ESM support in your t
2323

2424
## Differences between ESM and CommonJS
2525

26-
Most of the differences are explained in [Node's documentation](https://nodejs.org/api/esm.html#esm_differences_between_es_modules_and_commonjs), but in addition to the things mentioned there, Jest injects a special variable into all executed files - the [`jest` object](JestObjectAPI.md). To access this object in ESM, you need to import it from the `@jest/globals` module.
26+
Most of the differences are explained in [Node's documentation](https://nodejs.org/api/esm.html#esm_differences_between_es_modules_and_commonjs), but in addition to the things mentioned there, Jest injects a special variable into all executed files - the [`jest` object](JestObjectAPI.md). To access this object in ESM, you need to import it from the `@jest/globals` module or use `import.meta`.
2727

2828
```js
2929
import {jest} from '@jest/globals';
3030

3131
jest.useFakeTimers();
3232

3333
// etc.
34+
35+
// alternatively
36+
import.meta.jest.useFakeTimers();
37+
38+
// jest === import.meta.jest => true
3439
```
3540

3641
Please note that we currently don't support `jest.mock` in a clean way in ESM, but that is something we intend to add proper support for in the future. Follow [this issue](https://github.com/facebook/jest/issues/10025) for updates.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ test('should have correct import.meta', () => {
2828
expect(typeof require).toBe('undefined');
2929
expect(typeof jest).toBe('undefined');
3030
expect(import.meta).toEqual({
31+
jest: expect.anything(),
3132
url: expect.any(String),
3233
});
34+
expect(import.meta.jest).toBe(jestObject);
3335
expect(
3436
import.meta.url.endsWith('/e2e/native-esm/__tests__/native-esm.test.js'),
3537
).toBe(true);

packages/jest-environment/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export type ModuleWrapper = (
2828
...sandboxInjectedGlobals: Array<Global.Global[keyof Global.Global]>
2929
) => unknown;
3030

31+
export interface JestImportMeta extends ImportMeta {
32+
jest: Jest;
33+
}
34+
3135
export interface JestEnvironmentConfig {
3236
projectConfig: Config.ProjectConfig;
3337
globalConfig: Config.GlobalConfig;

packages/jest-runtime/src/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import stripBOM = require('strip-bom');
2727
import type {
2828
Jest,
2929
JestEnvironment,
30+
JestImportMeta,
3031
Module,
3132
ModuleWrapper,
3233
} from '@jest/environment';
@@ -499,8 +500,18 @@ export default class Runtime {
499500

500501
return this.linkAndEvaluateModule(module);
501502
},
502-
initializeImportMeta(meta: ImportMeta) {
503+
initializeImportMeta: (meta: JestImportMeta) => {
503504
meta.url = pathToFileURL(modulePath).href;
505+
506+
let jest = this.jestObjectCaches.get(modulePath);
507+
508+
if (!jest) {
509+
jest = this._createJestObjectFor(modulePath);
510+
511+
this.jestObjectCaches.set(modulePath, jest);
512+
}
513+
514+
meta.jest = jest;
504515
},
505516
});
506517

@@ -625,6 +636,7 @@ export default class Runtime {
625636
return this.linkAndEvaluateModule(module);
626637
},
627638
initializeImportMeta(meta: ImportMeta) {
639+
// no `jest` here as it's not loaded in a file
628640
meta.url = specifier;
629641
},
630642
});

0 commit comments

Comments
 (0)