From 27ea27963a372f9d3875c61313aa0ed0a30490dc Mon Sep 17 00:00:00 2001 From: Wing Chau Date: Mon, 29 Aug 2022 17:40:53 +0100 Subject: [PATCH 01/14] Add `jest.mock` ESM example --- docs/ECMAScriptModules.md | 31 +++++++++++++++++++ .../version-25.x/ECMAScriptModules.md | 31 +++++++++++++++++++ .../version-26.x/ECMAScriptModules.md | 31 +++++++++++++++++++ .../version-27.x/ECMAScriptModules.md | 31 +++++++++++++++++++ .../version-28.x/ECMAScriptModules.md | 31 +++++++++++++++++++ .../version-29.0/ECMAScriptModules.md | 31 +++++++++++++++++++ 6 files changed, 186 insertions(+) diff --git a/docs/ECMAScriptModules.md b/docs/ECMAScriptModules.md index 199b7ff61b8c..318639adf34c 100644 --- a/docs/ECMAScriptModules.md +++ b/docs/ECMAScriptModules.md @@ -38,4 +38,35 @@ import.meta.jest.useFakeTimers(); // jest === import.meta.jest => true ``` +Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. + +```js +// - main.cjs +const { BrowserWindow, app } = require('electron'); + +// etc. + +module.exports = { example }; +``` + +```js +// - main.test.js +import { jest } from '@jest/globals'; + +jest.mock('electron', () => ({ + app: { + on: jest.fn(), + whenReady: jest.fn(() => Promise.resolve()), + }, + BrowserWindow: jest.fn().mockImplementation(() => ({ + // partial mocks. + })) +})); + +const { BrowserWindow } = (await import('electron')).default; +const exported = await import('main.cjs'); + +// etc. +``` + 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. diff --git a/website/versioned_docs/version-25.x/ECMAScriptModules.md b/website/versioned_docs/version-25.x/ECMAScriptModules.md index 7c01cb606358..b38f92504392 100644 --- a/website/versioned_docs/version-25.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-25.x/ECMAScriptModules.md @@ -32,4 +32,35 @@ jest.useFakeTimers(); // etc. ``` +Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. + +```js +// - main.cjs +const { BrowserWindow, app } = require('electron'); + +// etc. + +module.exports = { example }; +``` + +```js +// - main.test.js +import { jest } from '@jest/globals'; + +jest.mock('electron', () => ({ + app: { + on: jest.fn(), + whenReady: jest.fn(() => Promise.resolve()), + }, + BrowserWindow: jest.fn().mockImplementation(() => ({ + // partial mocks. + })) +})); + +const { BrowserWindow } = (await import('electron')).default; +const exported = await import('main.cjs'); + +// etc. +``` + 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. diff --git a/website/versioned_docs/version-26.x/ECMAScriptModules.md b/website/versioned_docs/version-26.x/ECMAScriptModules.md index 7c01cb606358..b38f92504392 100644 --- a/website/versioned_docs/version-26.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-26.x/ECMAScriptModules.md @@ -32,4 +32,35 @@ jest.useFakeTimers(); // etc. ``` +Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. + +```js +// - main.cjs +const { BrowserWindow, app } = require('electron'); + +// etc. + +module.exports = { example }; +``` + +```js +// - main.test.js +import { jest } from '@jest/globals'; + +jest.mock('electron', () => ({ + app: { + on: jest.fn(), + whenReady: jest.fn(() => Promise.resolve()), + }, + BrowserWindow: jest.fn().mockImplementation(() => ({ + // partial mocks. + })) +})); + +const { BrowserWindow } = (await import('electron')).default; +const exported = await import('main.cjs'); + +// etc. +``` + 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. diff --git a/website/versioned_docs/version-27.x/ECMAScriptModules.md b/website/versioned_docs/version-27.x/ECMAScriptModules.md index b4ad17d411b4..ec2cc4540d50 100644 --- a/website/versioned_docs/version-27.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-27.x/ECMAScriptModules.md @@ -33,4 +33,35 @@ jest.useFakeTimers(); // etc. ``` +Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. + +```js +// - main.cjs +const { BrowserWindow, app } = require('electron'); + +// etc. + +module.exports = { example }; +``` + +```js +// - main.test.js +import { jest } from '@jest/globals'; + +jest.mock('electron', () => ({ + app: { + on: jest.fn(), + whenReady: jest.fn(() => Promise.resolve()), + }, + BrowserWindow: jest.fn().mockImplementation(() => ({ + // partial mocks. + })) +})); + +const { BrowserWindow } = (await import('electron')).default; +const exported = await import('main.cjs'); + +// etc. +``` + 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. diff --git a/website/versioned_docs/version-28.x/ECMAScriptModules.md b/website/versioned_docs/version-28.x/ECMAScriptModules.md index 199b7ff61b8c..318639adf34c 100644 --- a/website/versioned_docs/version-28.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-28.x/ECMAScriptModules.md @@ -38,4 +38,35 @@ import.meta.jest.useFakeTimers(); // jest === import.meta.jest => true ``` +Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. + +```js +// - main.cjs +const { BrowserWindow, app } = require('electron'); + +// etc. + +module.exports = { example }; +``` + +```js +// - main.test.js +import { jest } from '@jest/globals'; + +jest.mock('electron', () => ({ + app: { + on: jest.fn(), + whenReady: jest.fn(() => Promise.resolve()), + }, + BrowserWindow: jest.fn().mockImplementation(() => ({ + // partial mocks. + })) +})); + +const { BrowserWindow } = (await import('electron')).default; +const exported = await import('main.cjs'); + +// etc. +``` + 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. diff --git a/website/versioned_docs/version-29.0/ECMAScriptModules.md b/website/versioned_docs/version-29.0/ECMAScriptModules.md index 199b7ff61b8c..318639adf34c 100644 --- a/website/versioned_docs/version-29.0/ECMAScriptModules.md +++ b/website/versioned_docs/version-29.0/ECMAScriptModules.md @@ -38,4 +38,35 @@ import.meta.jest.useFakeTimers(); // jest === import.meta.jest => true ``` +Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. + +```js +// - main.cjs +const { BrowserWindow, app } = require('electron'); + +// etc. + +module.exports = { example }; +``` + +```js +// - main.test.js +import { jest } from '@jest/globals'; + +jest.mock('electron', () => ({ + app: { + on: jest.fn(), + whenReady: jest.fn(() => Promise.resolve()), + }, + BrowserWindow: jest.fn().mockImplementation(() => ({ + // partial mocks. + })) +})); + +const { BrowserWindow } = (await import('electron')).default; +const exported = await import('main.cjs'); + +// etc. +``` + 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. From 658b12a49b84c5b1a5281b90f0bfea9beb5c1662 Mon Sep 17 00:00:00 2001 From: Wing Chau Date: Mon, 29 Aug 2022 18:03:53 +0100 Subject: [PATCH 02/14] Update `ManualMocks` doc for ESM - Added link to `ECMAScriptModules` in section `Using with ES module imports`. --- docs/ManualMocks.md | 2 ++ website/versioned_docs/version-25.x/ManualMocks.md | 2 ++ website/versioned_docs/version-26.x/ManualMocks.md | 2 ++ website/versioned_docs/version-27.x/ManualMocks.md | 2 ++ website/versioned_docs/version-28.x/ManualMocks.md | 2 ++ website/versioned_docs/version-29.0/ManualMocks.md | 2 ++ 6 files changed, 12 insertions(+) diff --git a/docs/ManualMocks.md b/docs/ManualMocks.md index 178226d727f9..d13c3798a160 100644 --- a/docs/ManualMocks.md +++ b/docs/ManualMocks.md @@ -130,6 +130,8 @@ The code for this example is available at [examples/manual-mocks](https://github If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) then you'll normally be inclined to put your `import` statements at the top of the test file. But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist `jest.mock` calls to the top of the module (before any imports). To learn more about this and see it in action, see [this repo](https://github.com/kentcdodds/how-jest-mocking-works). +*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](https://jestjs.io/docs/ecmascript-modules) for details.* + ## Mocking methods which are not implemented in JSDOM If some code uses a method which JSDOM (the DOM implementation used by Jest) hasn't implemented yet, testing it is not easily possible. This is e.g. the case with `window.matchMedia()`. Jest returns `TypeError: window.matchMedia is not a function` and doesn't properly execute the test. diff --git a/website/versioned_docs/version-25.x/ManualMocks.md b/website/versioned_docs/version-25.x/ManualMocks.md index 9387ca82fc5a..81fbd7c62942 100644 --- a/website/versioned_docs/version-25.x/ManualMocks.md +++ b/website/versioned_docs/version-25.x/ManualMocks.md @@ -130,6 +130,8 @@ The code for this example is available at [examples/manual-mocks](https://github If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) then you'll normally be inclined to put your `import` statements at the top of the test file. But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist `jest.mock` calls to the top of the module (before any imports). To learn more about this and see it in action, see [this repo](https://github.com/kentcdodds/how-jest-mocking-works). +*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](https://jestjs.io/docs/ecmascript-modules) for details.* + ## Mocking methods which are not implemented in JSDOM If some code uses a method which JSDOM (the DOM implementation used by Jest) hasn't implemented yet, testing it is not easily possible. This is e.g. the case with `window.matchMedia()`. Jest returns `TypeError: window.matchMedia is not a function` and doesn't properly execute the test. diff --git a/website/versioned_docs/version-26.x/ManualMocks.md b/website/versioned_docs/version-26.x/ManualMocks.md index 178226d727f9..d13c3798a160 100644 --- a/website/versioned_docs/version-26.x/ManualMocks.md +++ b/website/versioned_docs/version-26.x/ManualMocks.md @@ -130,6 +130,8 @@ The code for this example is available at [examples/manual-mocks](https://github If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) then you'll normally be inclined to put your `import` statements at the top of the test file. But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist `jest.mock` calls to the top of the module (before any imports). To learn more about this and see it in action, see [this repo](https://github.com/kentcdodds/how-jest-mocking-works). +*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](https://jestjs.io/docs/ecmascript-modules) for details.* + ## Mocking methods which are not implemented in JSDOM If some code uses a method which JSDOM (the DOM implementation used by Jest) hasn't implemented yet, testing it is not easily possible. This is e.g. the case with `window.matchMedia()`. Jest returns `TypeError: window.matchMedia is not a function` and doesn't properly execute the test. diff --git a/website/versioned_docs/version-27.x/ManualMocks.md b/website/versioned_docs/version-27.x/ManualMocks.md index 178226d727f9..d13c3798a160 100644 --- a/website/versioned_docs/version-27.x/ManualMocks.md +++ b/website/versioned_docs/version-27.x/ManualMocks.md @@ -130,6 +130,8 @@ The code for this example is available at [examples/manual-mocks](https://github If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) then you'll normally be inclined to put your `import` statements at the top of the test file. But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist `jest.mock` calls to the top of the module (before any imports). To learn more about this and see it in action, see [this repo](https://github.com/kentcdodds/how-jest-mocking-works). +*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](https://jestjs.io/docs/ecmascript-modules) for details.* + ## Mocking methods which are not implemented in JSDOM If some code uses a method which JSDOM (the DOM implementation used by Jest) hasn't implemented yet, testing it is not easily possible. This is e.g. the case with `window.matchMedia()`. Jest returns `TypeError: window.matchMedia is not a function` and doesn't properly execute the test. diff --git a/website/versioned_docs/version-28.x/ManualMocks.md b/website/versioned_docs/version-28.x/ManualMocks.md index 178226d727f9..d13c3798a160 100644 --- a/website/versioned_docs/version-28.x/ManualMocks.md +++ b/website/versioned_docs/version-28.x/ManualMocks.md @@ -130,6 +130,8 @@ The code for this example is available at [examples/manual-mocks](https://github If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) then you'll normally be inclined to put your `import` statements at the top of the test file. But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist `jest.mock` calls to the top of the module (before any imports). To learn more about this and see it in action, see [this repo](https://github.com/kentcdodds/how-jest-mocking-works). +*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](https://jestjs.io/docs/ecmascript-modules) for details.* + ## Mocking methods which are not implemented in JSDOM If some code uses a method which JSDOM (the DOM implementation used by Jest) hasn't implemented yet, testing it is not easily possible. This is e.g. the case with `window.matchMedia()`. Jest returns `TypeError: window.matchMedia is not a function` and doesn't properly execute the test. diff --git a/website/versioned_docs/version-29.0/ManualMocks.md b/website/versioned_docs/version-29.0/ManualMocks.md index 178226d727f9..d13c3798a160 100644 --- a/website/versioned_docs/version-29.0/ManualMocks.md +++ b/website/versioned_docs/version-29.0/ManualMocks.md @@ -130,6 +130,8 @@ The code for this example is available at [examples/manual-mocks](https://github If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) then you'll normally be inclined to put your `import` statements at the top of the test file. But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist `jest.mock` calls to the top of the module (before any imports). To learn more about this and see it in action, see [this repo](https://github.com/kentcdodds/how-jest-mocking-works). +*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](https://jestjs.io/docs/ecmascript-modules) for details.* + ## Mocking methods which are not implemented in JSDOM If some code uses a method which JSDOM (the DOM implementation used by Jest) hasn't implemented yet, testing it is not easily possible. This is e.g. the case with `window.matchMedia()`. Jest returns `TypeError: window.matchMedia is not a function` and doesn't properly execute the test. From 3a8816dd1a2558ca0827c40c18fa192211ba8a12 Mon Sep 17 00:00:00 2001 From: Wing Chau Date: Mon, 29 Aug 2022 18:21:45 +0100 Subject: [PATCH 03/14] Update Node version for experimental ESM support - To reflect the most updated status. --- docs/ECMAScriptModules.md | 2 +- website/versioned_docs/version-25.x/ECMAScriptModules.md | 2 +- website/versioned_docs/version-26.x/ECMAScriptModules.md | 2 +- website/versioned_docs/version-27.x/ECMAScriptModules.md | 2 +- website/versioned_docs/version-28.x/ECMAScriptModules.md | 2 +- website/versioned_docs/version-29.0/ECMAScriptModules.md | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/ECMAScriptModules.md b/docs/ECMAScriptModules.md index 318639adf34c..9b8602795d11 100644 --- a/docs/ECMAScriptModules.md +++ b/docs/ECMAScriptModules.md @@ -7,7 +7,7 @@ Jest ships with _experimental_ support for ECMAScript Modules (ESM). > Note that due to its experimental nature there are many bugs and missing features in Jest's implementation, both known and unknown. You should check out the [tracking issue](https://github.com/facebook/jest/issues/9430) and the [label](https://github.com/facebook/jest/labels/ES%20Modules) on the issue tracker for the latest status. -> Also note that the APIs Jest uses to implement ESM support is still [considered experimental by Node](https://nodejs.org/api/vm.html#vm_class_vm_module) (as of version `14.13.1`). +> Also note that the APIs Jest uses to implement ESM support is still [considered experimental by Node](https://nodejs.org/api/vm.html#vm_class_vm_module) (as of version `18.8.0`). With the warnings out of the way, this is how you activate ESM support in your tests. diff --git a/website/versioned_docs/version-25.x/ECMAScriptModules.md b/website/versioned_docs/version-25.x/ECMAScriptModules.md index b38f92504392..87ad8ce23317 100644 --- a/website/versioned_docs/version-25.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-25.x/ECMAScriptModules.md @@ -7,7 +7,7 @@ Jest ships with _experimental_ support for ECMAScript Modules (ESM). > Note that due to its experimental nature there are many bugs and missing features in Jest's implementation, both known and unknown. You should check out the [tracking issue](https://github.com/facebook/jest/issues/9430) and the [label](https://github.com/facebook/jest/labels/ES%20Modules) on the issue tracker for the latest status. -> Also note that the APIs Jest uses to implement ESM support is still [considered experimental by Node](https://nodejs.org/api/vm.html#vm_class_vm_module) (as of version `14.13.1`). +> Also note that the APIs Jest uses to implement ESM support is still [considered experimental by Node](https://nodejs.org/api/vm.html#vm_class_vm_module) (as of version `18.8.0`). With the warnings out of the way, this is how you activate ESM support in your tests. diff --git a/website/versioned_docs/version-26.x/ECMAScriptModules.md b/website/versioned_docs/version-26.x/ECMAScriptModules.md index b38f92504392..87ad8ce23317 100644 --- a/website/versioned_docs/version-26.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-26.x/ECMAScriptModules.md @@ -7,7 +7,7 @@ Jest ships with _experimental_ support for ECMAScript Modules (ESM). > Note that due to its experimental nature there are many bugs and missing features in Jest's implementation, both known and unknown. You should check out the [tracking issue](https://github.com/facebook/jest/issues/9430) and the [label](https://github.com/facebook/jest/labels/ES%20Modules) on the issue tracker for the latest status. -> Also note that the APIs Jest uses to implement ESM support is still [considered experimental by Node](https://nodejs.org/api/vm.html#vm_class_vm_module) (as of version `14.13.1`). +> Also note that the APIs Jest uses to implement ESM support is still [considered experimental by Node](https://nodejs.org/api/vm.html#vm_class_vm_module) (as of version `18.8.0`). With the warnings out of the way, this is how you activate ESM support in your tests. diff --git a/website/versioned_docs/version-27.x/ECMAScriptModules.md b/website/versioned_docs/version-27.x/ECMAScriptModules.md index ec2cc4540d50..090ca1548e3e 100644 --- a/website/versioned_docs/version-27.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-27.x/ECMAScriptModules.md @@ -7,7 +7,7 @@ Jest ships with _experimental_ support for ECMAScript Modules (ESM). > Note that due to its experimental nature there are many bugs and missing features in Jest's implementation, both known and unknown. You should check out the [tracking issue](https://github.com/facebook/jest/issues/9430) and the [label](https://github.com/facebook/jest/labels/ES%20Modules) on the issue tracker for the latest status. -> Also note that the APIs Jest uses to implement ESM support is still [considered experimental by Node](https://nodejs.org/api/vm.html#vm_class_vm_module) (as of version `14.13.1`). +> Also note that the APIs Jest uses to implement ESM support is still [considered experimental by Node](https://nodejs.org/api/vm.html#vm_class_vm_module) (as of version `18.8.0`). With the warnings out of the way, this is how you activate ESM support in your tests. diff --git a/website/versioned_docs/version-28.x/ECMAScriptModules.md b/website/versioned_docs/version-28.x/ECMAScriptModules.md index 318639adf34c..9b8602795d11 100644 --- a/website/versioned_docs/version-28.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-28.x/ECMAScriptModules.md @@ -7,7 +7,7 @@ Jest ships with _experimental_ support for ECMAScript Modules (ESM). > Note that due to its experimental nature there are many bugs and missing features in Jest's implementation, both known and unknown. You should check out the [tracking issue](https://github.com/facebook/jest/issues/9430) and the [label](https://github.com/facebook/jest/labels/ES%20Modules) on the issue tracker for the latest status. -> Also note that the APIs Jest uses to implement ESM support is still [considered experimental by Node](https://nodejs.org/api/vm.html#vm_class_vm_module) (as of version `14.13.1`). +> Also note that the APIs Jest uses to implement ESM support is still [considered experimental by Node](https://nodejs.org/api/vm.html#vm_class_vm_module) (as of version `18.8.0`). With the warnings out of the way, this is how you activate ESM support in your tests. diff --git a/website/versioned_docs/version-29.0/ECMAScriptModules.md b/website/versioned_docs/version-29.0/ECMAScriptModules.md index 318639adf34c..9b8602795d11 100644 --- a/website/versioned_docs/version-29.0/ECMAScriptModules.md +++ b/website/versioned_docs/version-29.0/ECMAScriptModules.md @@ -7,7 +7,7 @@ Jest ships with _experimental_ support for ECMAScript Modules (ESM). > Note that due to its experimental nature there are many bugs and missing features in Jest's implementation, both known and unknown. You should check out the [tracking issue](https://github.com/facebook/jest/issues/9430) and the [label](https://github.com/facebook/jest/labels/ES%20Modules) on the issue tracker for the latest status. -> Also note that the APIs Jest uses to implement ESM support is still [considered experimental by Node](https://nodejs.org/api/vm.html#vm_class_vm_module) (as of version `14.13.1`). +> Also note that the APIs Jest uses to implement ESM support is still [considered experimental by Node](https://nodejs.org/api/vm.html#vm_class_vm_module) (as of version `18.8.0`). With the warnings out of the way, this is how you activate ESM support in your tests. From 4fdcbb22fab57d7b3eb8130999ddbd8bc12b8f4e Mon Sep 17 00:00:00 2001 From: Wing Chau Date: Tue, 30 Aug 2022 07:06:59 +0100 Subject: [PATCH 04/14] Fix link - Replace hard coded link to `ECMAScriptModules` documentation in `ManualMocks.md` with a relative link to `ECMAScriptModules.md`. --- docs/ManualMocks.md | 2 +- website/versioned_docs/version-25.x/ManualMocks.md | 2 +- website/versioned_docs/version-26.x/ManualMocks.md | 2 +- website/versioned_docs/version-27.x/ManualMocks.md | 2 +- website/versioned_docs/version-28.x/ManualMocks.md | 2 +- website/versioned_docs/version-29.0/ManualMocks.md | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/ManualMocks.md b/docs/ManualMocks.md index d13c3798a160..855b2bd7978b 100644 --- a/docs/ManualMocks.md +++ b/docs/ManualMocks.md @@ -130,7 +130,7 @@ The code for this example is available at [examples/manual-mocks](https://github If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) then you'll normally be inclined to put your `import` statements at the top of the test file. But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist `jest.mock` calls to the top of the module (before any imports). To learn more about this and see it in action, see [this repo](https://github.com/kentcdodds/how-jest-mocking-works). -*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](https://jestjs.io/docs/ecmascript-modules) for details.* +*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details.* ## Mocking methods which are not implemented in JSDOM diff --git a/website/versioned_docs/version-25.x/ManualMocks.md b/website/versioned_docs/version-25.x/ManualMocks.md index 81fbd7c62942..c8b879dc5aa4 100644 --- a/website/versioned_docs/version-25.x/ManualMocks.md +++ b/website/versioned_docs/version-25.x/ManualMocks.md @@ -130,7 +130,7 @@ The code for this example is available at [examples/manual-mocks](https://github If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) then you'll normally be inclined to put your `import` statements at the top of the test file. But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist `jest.mock` calls to the top of the module (before any imports). To learn more about this and see it in action, see [this repo](https://github.com/kentcdodds/how-jest-mocking-works). -*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](https://jestjs.io/docs/ecmascript-modules) for details.* +*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details.* ## Mocking methods which are not implemented in JSDOM diff --git a/website/versioned_docs/version-26.x/ManualMocks.md b/website/versioned_docs/version-26.x/ManualMocks.md index d13c3798a160..855b2bd7978b 100644 --- a/website/versioned_docs/version-26.x/ManualMocks.md +++ b/website/versioned_docs/version-26.x/ManualMocks.md @@ -130,7 +130,7 @@ The code for this example is available at [examples/manual-mocks](https://github If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) then you'll normally be inclined to put your `import` statements at the top of the test file. But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist `jest.mock` calls to the top of the module (before any imports). To learn more about this and see it in action, see [this repo](https://github.com/kentcdodds/how-jest-mocking-works). -*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](https://jestjs.io/docs/ecmascript-modules) for details.* +*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details.* ## Mocking methods which are not implemented in JSDOM diff --git a/website/versioned_docs/version-27.x/ManualMocks.md b/website/versioned_docs/version-27.x/ManualMocks.md index d13c3798a160..855b2bd7978b 100644 --- a/website/versioned_docs/version-27.x/ManualMocks.md +++ b/website/versioned_docs/version-27.x/ManualMocks.md @@ -130,7 +130,7 @@ The code for this example is available at [examples/manual-mocks](https://github If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) then you'll normally be inclined to put your `import` statements at the top of the test file. But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist `jest.mock` calls to the top of the module (before any imports). To learn more about this and see it in action, see [this repo](https://github.com/kentcdodds/how-jest-mocking-works). -*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](https://jestjs.io/docs/ecmascript-modules) for details.* +*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details.* ## Mocking methods which are not implemented in JSDOM diff --git a/website/versioned_docs/version-28.x/ManualMocks.md b/website/versioned_docs/version-28.x/ManualMocks.md index d13c3798a160..855b2bd7978b 100644 --- a/website/versioned_docs/version-28.x/ManualMocks.md +++ b/website/versioned_docs/version-28.x/ManualMocks.md @@ -130,7 +130,7 @@ The code for this example is available at [examples/manual-mocks](https://github If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) then you'll normally be inclined to put your `import` statements at the top of the test file. But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist `jest.mock` calls to the top of the module (before any imports). To learn more about this and see it in action, see [this repo](https://github.com/kentcdodds/how-jest-mocking-works). -*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](https://jestjs.io/docs/ecmascript-modules) for details.* +*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details.* ## Mocking methods which are not implemented in JSDOM diff --git a/website/versioned_docs/version-29.0/ManualMocks.md b/website/versioned_docs/version-29.0/ManualMocks.md index d13c3798a160..855b2bd7978b 100644 --- a/website/versioned_docs/version-29.0/ManualMocks.md +++ b/website/versioned_docs/version-29.0/ManualMocks.md @@ -130,7 +130,7 @@ The code for this example is available at [examples/manual-mocks](https://github If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) then you'll normally be inclined to put your `import` statements at the top of the test file. But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist `jest.mock` calls to the top of the module (before any imports). To learn more about this and see it in action, see [this repo](https://github.com/kentcdodds/how-jest-mocking-works). -*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](https://jestjs.io/docs/ecmascript-modules) for details.* +*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details.* ## Mocking methods which are not implemented in JSDOM From 7e562dfe07d82ebe9ad218f20117af6329dd8d52 Mon Sep 17 00:00:00 2001 From: Wing Chau Date: Tue, 30 Aug 2022 12:48:53 +0100 Subject: [PATCH 05/14] Update modified docs for format suggested --- docs/ECMAScriptModules.md | 6 ++---- docs/ManualMocks.md | 4 +++- website/versioned_docs/version-25.x/ECMAScriptModules.md | 6 ++---- website/versioned_docs/version-25.x/ManualMocks.md | 4 +++- website/versioned_docs/version-26.x/ECMAScriptModules.md | 6 ++---- website/versioned_docs/version-26.x/ManualMocks.md | 4 +++- website/versioned_docs/version-27.x/ECMAScriptModules.md | 6 ++---- website/versioned_docs/version-27.x/ManualMocks.md | 4 +++- website/versioned_docs/version-28.x/ECMAScriptModules.md | 6 ++---- website/versioned_docs/version-28.x/ManualMocks.md | 4 +++- website/versioned_docs/version-29.0/ECMAScriptModules.md | 6 ++---- website/versioned_docs/version-29.0/ManualMocks.md | 4 +++- 12 files changed, 30 insertions(+), 30 deletions(-) diff --git a/docs/ECMAScriptModules.md b/docs/ECMAScriptModules.md index 9b8602795d11..dde86ca25ab4 100644 --- a/docs/ECMAScriptModules.md +++ b/docs/ECMAScriptModules.md @@ -40,8 +40,7 @@ import.meta.jest.useFakeTimers(); Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. -```js -// - main.cjs +```js title="main.cjs" const { BrowserWindow, app } = require('electron'); // etc. @@ -49,8 +48,7 @@ const { BrowserWindow, app } = require('electron'); module.exports = { example }; ``` -```js -// - main.test.js +```js title="main.test.cjs" import { jest } from '@jest/globals'; jest.mock('electron', () => ({ diff --git a/docs/ManualMocks.md b/docs/ManualMocks.md index 855b2bd7978b..ee41427ea2b6 100644 --- a/docs/ManualMocks.md +++ b/docs/ManualMocks.md @@ -130,7 +130,9 @@ The code for this example is available at [examples/manual-mocks](https://github If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) then you'll normally be inclined to put your `import` statements at the top of the test file. But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist `jest.mock` calls to the top of the module (before any imports). To learn more about this and see it in action, see [this repo](https://github.com/kentcdodds/how-jest-mocking-works). -*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details.* +:::caution + +Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details. ## Mocking methods which are not implemented in JSDOM diff --git a/website/versioned_docs/version-25.x/ECMAScriptModules.md b/website/versioned_docs/version-25.x/ECMAScriptModules.md index 87ad8ce23317..1b35bf8b1c02 100644 --- a/website/versioned_docs/version-25.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-25.x/ECMAScriptModules.md @@ -34,8 +34,7 @@ jest.useFakeTimers(); Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. -```js -// - main.cjs +```js title="main.cjs" const { BrowserWindow, app } = require('electron'); // etc. @@ -43,8 +42,7 @@ const { BrowserWindow, app } = require('electron'); module.exports = { example }; ``` -```js -// - main.test.js +```js title="main.test.cjs" import { jest } from '@jest/globals'; jest.mock('electron', () => ({ diff --git a/website/versioned_docs/version-25.x/ManualMocks.md b/website/versioned_docs/version-25.x/ManualMocks.md index c8b879dc5aa4..9e7001c891ff 100644 --- a/website/versioned_docs/version-25.x/ManualMocks.md +++ b/website/versioned_docs/version-25.x/ManualMocks.md @@ -130,7 +130,9 @@ The code for this example is available at [examples/manual-mocks](https://github If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) then you'll normally be inclined to put your `import` statements at the top of the test file. But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist `jest.mock` calls to the top of the module (before any imports). To learn more about this and see it in action, see [this repo](https://github.com/kentcdodds/how-jest-mocking-works). -*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details.* +:::caution + +Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details. ## Mocking methods which are not implemented in JSDOM diff --git a/website/versioned_docs/version-26.x/ECMAScriptModules.md b/website/versioned_docs/version-26.x/ECMAScriptModules.md index 87ad8ce23317..1b35bf8b1c02 100644 --- a/website/versioned_docs/version-26.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-26.x/ECMAScriptModules.md @@ -34,8 +34,7 @@ jest.useFakeTimers(); Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. -```js -// - main.cjs +```js title="main.cjs" const { BrowserWindow, app } = require('electron'); // etc. @@ -43,8 +42,7 @@ const { BrowserWindow, app } = require('electron'); module.exports = { example }; ``` -```js -// - main.test.js +```js title="main.test.cjs" import { jest } from '@jest/globals'; jest.mock('electron', () => ({ diff --git a/website/versioned_docs/version-26.x/ManualMocks.md b/website/versioned_docs/version-26.x/ManualMocks.md index 855b2bd7978b..ee41427ea2b6 100644 --- a/website/versioned_docs/version-26.x/ManualMocks.md +++ b/website/versioned_docs/version-26.x/ManualMocks.md @@ -130,7 +130,9 @@ The code for this example is available at [examples/manual-mocks](https://github If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) then you'll normally be inclined to put your `import` statements at the top of the test file. But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist `jest.mock` calls to the top of the module (before any imports). To learn more about this and see it in action, see [this repo](https://github.com/kentcdodds/how-jest-mocking-works). -*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details.* +:::caution + +Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details. ## Mocking methods which are not implemented in JSDOM diff --git a/website/versioned_docs/version-27.x/ECMAScriptModules.md b/website/versioned_docs/version-27.x/ECMAScriptModules.md index 090ca1548e3e..1528371beccd 100644 --- a/website/versioned_docs/version-27.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-27.x/ECMAScriptModules.md @@ -35,8 +35,7 @@ jest.useFakeTimers(); Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. -```js -// - main.cjs +```js title="main.cjs" const { BrowserWindow, app } = require('electron'); // etc. @@ -44,8 +43,7 @@ const { BrowserWindow, app } = require('electron'); module.exports = { example }; ``` -```js -// - main.test.js +```js title="main.test.cjs" import { jest } from '@jest/globals'; jest.mock('electron', () => ({ diff --git a/website/versioned_docs/version-27.x/ManualMocks.md b/website/versioned_docs/version-27.x/ManualMocks.md index 855b2bd7978b..ee41427ea2b6 100644 --- a/website/versioned_docs/version-27.x/ManualMocks.md +++ b/website/versioned_docs/version-27.x/ManualMocks.md @@ -130,7 +130,9 @@ The code for this example is available at [examples/manual-mocks](https://github If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) then you'll normally be inclined to put your `import` statements at the top of the test file. But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist `jest.mock` calls to the top of the module (before any imports). To learn more about this and see it in action, see [this repo](https://github.com/kentcdodds/how-jest-mocking-works). -*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details.* +:::caution + +Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details. ## Mocking methods which are not implemented in JSDOM diff --git a/website/versioned_docs/version-28.x/ECMAScriptModules.md b/website/versioned_docs/version-28.x/ECMAScriptModules.md index 9b8602795d11..dde86ca25ab4 100644 --- a/website/versioned_docs/version-28.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-28.x/ECMAScriptModules.md @@ -40,8 +40,7 @@ import.meta.jest.useFakeTimers(); Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. -```js -// - main.cjs +```js title="main.cjs" const { BrowserWindow, app } = require('electron'); // etc. @@ -49,8 +48,7 @@ const { BrowserWindow, app } = require('electron'); module.exports = { example }; ``` -```js -// - main.test.js +```js title="main.test.cjs" import { jest } from '@jest/globals'; jest.mock('electron', () => ({ diff --git a/website/versioned_docs/version-28.x/ManualMocks.md b/website/versioned_docs/version-28.x/ManualMocks.md index 855b2bd7978b..ee41427ea2b6 100644 --- a/website/versioned_docs/version-28.x/ManualMocks.md +++ b/website/versioned_docs/version-28.x/ManualMocks.md @@ -130,7 +130,9 @@ The code for this example is available at [examples/manual-mocks](https://github If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) then you'll normally be inclined to put your `import` statements at the top of the test file. But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist `jest.mock` calls to the top of the module (before any imports). To learn more about this and see it in action, see [this repo](https://github.com/kentcdodds/how-jest-mocking-works). -*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details.* +:::caution + +Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details. ## Mocking methods which are not implemented in JSDOM diff --git a/website/versioned_docs/version-29.0/ECMAScriptModules.md b/website/versioned_docs/version-29.0/ECMAScriptModules.md index 9b8602795d11..dde86ca25ab4 100644 --- a/website/versioned_docs/version-29.0/ECMAScriptModules.md +++ b/website/versioned_docs/version-29.0/ECMAScriptModules.md @@ -40,8 +40,7 @@ import.meta.jest.useFakeTimers(); Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. -```js -// - main.cjs +```js title="main.cjs" const { BrowserWindow, app } = require('electron'); // etc. @@ -49,8 +48,7 @@ const { BrowserWindow, app } = require('electron'); module.exports = { example }; ``` -```js -// - main.test.js +```js title="main.test.cjs" import { jest } from '@jest/globals'; jest.mock('electron', () => ({ diff --git a/website/versioned_docs/version-29.0/ManualMocks.md b/website/versioned_docs/version-29.0/ManualMocks.md index 855b2bd7978b..ee41427ea2b6 100644 --- a/website/versioned_docs/version-29.0/ManualMocks.md +++ b/website/versioned_docs/version-29.0/ManualMocks.md @@ -130,7 +130,9 @@ The code for this example is available at [examples/manual-mocks](https://github If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) then you'll normally be inclined to put your `import` statements at the top of the test file. But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist `jest.mock` calls to the top of the module (before any imports). To learn more about this and see it in action, see [this repo](https://github.com/kentcdodds/how-jest-mocking-works). -*__Caution__: Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details.* +:::caution + +Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details. ## Mocking methods which are not implemented in JSDOM From e21e65760e4992754b9fc022d9154be2e1c4cf19 Mon Sep 17 00:00:00 2001 From: Wing Chau Date: Wed, 31 Aug 2022 07:36:28 +0100 Subject: [PATCH 06/14] Fix `caution` section in `ManualMocks` - Also clarified that the limitation comes from ESM, not Jest. --- docs/ManualMocks.md | 4 +++- website/versioned_docs/version-25.x/ManualMocks.md | 4 +++- website/versioned_docs/version-26.x/ManualMocks.md | 4 +++- website/versioned_docs/version-27.x/ManualMocks.md | 4 +++- website/versioned_docs/version-28.x/ManualMocks.md | 4 +++- website/versioned_docs/version-29.0/ManualMocks.md | 4 +++- 6 files changed, 18 insertions(+), 6 deletions(-) diff --git a/docs/ManualMocks.md b/docs/ManualMocks.md index ee41427ea2b6..212eb0d55642 100644 --- a/docs/ManualMocks.md +++ b/docs/ManualMocks.md @@ -132,7 +132,9 @@ If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web :::caution -Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details. +`jest.mock` calls cannot be hoisted to the top of the module if you enabled ECMAScript modules support. The ESM module loader always evaluates the imports before executing code. See [ECMAScriptModules](ECMAScriptModules.md) for details. + +::: ## Mocking methods which are not implemented in JSDOM diff --git a/website/versioned_docs/version-25.x/ManualMocks.md b/website/versioned_docs/version-25.x/ManualMocks.md index 9e7001c891ff..3a4e4104539a 100644 --- a/website/versioned_docs/version-25.x/ManualMocks.md +++ b/website/versioned_docs/version-25.x/ManualMocks.md @@ -132,7 +132,9 @@ If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web :::caution -Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details. +`jest.mock` calls cannot be hoisted to the top of the module if you enabled ECMAScript modules support. The ESM module loader always evaluates the imports before executing code. See [ECMAScriptModules](ECMAScriptModules.md) for details. + +::: ## Mocking methods which are not implemented in JSDOM diff --git a/website/versioned_docs/version-26.x/ManualMocks.md b/website/versioned_docs/version-26.x/ManualMocks.md index ee41427ea2b6..212eb0d55642 100644 --- a/website/versioned_docs/version-26.x/ManualMocks.md +++ b/website/versioned_docs/version-26.x/ManualMocks.md @@ -132,7 +132,9 @@ If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web :::caution -Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details. +`jest.mock` calls cannot be hoisted to the top of the module if you enabled ECMAScript modules support. The ESM module loader always evaluates the imports before executing code. See [ECMAScriptModules](ECMAScriptModules.md) for details. + +::: ## Mocking methods which are not implemented in JSDOM diff --git a/website/versioned_docs/version-27.x/ManualMocks.md b/website/versioned_docs/version-27.x/ManualMocks.md index ee41427ea2b6..212eb0d55642 100644 --- a/website/versioned_docs/version-27.x/ManualMocks.md +++ b/website/versioned_docs/version-27.x/ManualMocks.md @@ -132,7 +132,9 @@ If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web :::caution -Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details. +`jest.mock` calls cannot be hoisted to the top of the module if you enabled ECMAScript modules support. The ESM module loader always evaluates the imports before executing code. See [ECMAScriptModules](ECMAScriptModules.md) for details. + +::: ## Mocking methods which are not implemented in JSDOM diff --git a/website/versioned_docs/version-28.x/ManualMocks.md b/website/versioned_docs/version-28.x/ManualMocks.md index ee41427ea2b6..212eb0d55642 100644 --- a/website/versioned_docs/version-28.x/ManualMocks.md +++ b/website/versioned_docs/version-28.x/ManualMocks.md @@ -132,7 +132,9 @@ If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web :::caution -Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details. +`jest.mock` calls cannot be hoisted to the top of the module if you enabled ECMAScript modules support. The ESM module loader always evaluates the imports before executing code. See [ECMAScriptModules](ECMAScriptModules.md) for details. + +::: ## Mocking methods which are not implemented in JSDOM diff --git a/website/versioned_docs/version-29.0/ManualMocks.md b/website/versioned_docs/version-29.0/ManualMocks.md index ee41427ea2b6..212eb0d55642 100644 --- a/website/versioned_docs/version-29.0/ManualMocks.md +++ b/website/versioned_docs/version-29.0/ManualMocks.md @@ -132,7 +132,9 @@ If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web :::caution -Jest does NOT hoist `jest.mock` calls to the top of the module if you have ESM support activated. See [ECMAScriptModules](ECMAScriptModules.md) for details. +`jest.mock` calls cannot be hoisted to the top of the module if you enabled ECMAScript modules support. The ESM module loader always evaluates the imports before executing code. See [ECMAScriptModules](ECMAScriptModules.md) for details. + +::: ## Mocking methods which are not implemented in JSDOM From 6945cfc3d0a8da8d3f9e7515ed1ad2f0e568a9af Mon Sep 17 00:00:00 2001 From: Wing Chau Date: Wed, 31 Aug 2022 16:26:58 +0100 Subject: [PATCH 07/14] Prefer `require` for CJS module imports --- docs/ECMAScriptModules.md | 7 +++++-- website/versioned_docs/version-25.x/ECMAScriptModules.md | 7 +++++-- website/versioned_docs/version-26.x/ECMAScriptModules.md | 7 +++++-- website/versioned_docs/version-27.x/ECMAScriptModules.md | 7 +++++-- website/versioned_docs/version-28.x/ECMAScriptModules.md | 7 +++++-- website/versioned_docs/version-29.0/ECMAScriptModules.md | 7 +++++-- 6 files changed, 30 insertions(+), 12 deletions(-) diff --git a/docs/ECMAScriptModules.md b/docs/ECMAScriptModules.md index dde86ca25ab4..b46d870d30bb 100644 --- a/docs/ECMAScriptModules.md +++ b/docs/ECMAScriptModules.md @@ -49,8 +49,11 @@ module.exports = { example }; ``` ```js title="main.test.cjs" +import { createRequire } from 'node:module'; import { jest } from '@jest/globals'; +const require = createRequire(import.meta.url); + jest.mock('electron', () => ({ app: { on: jest.fn(), @@ -61,8 +64,8 @@ jest.mock('electron', () => ({ })) })); -const { BrowserWindow } = (await import('electron')).default; -const exported = await import('main.cjs'); +const { BrowserWindow } = require('electron'); +const exported = require('main.cjs'); // etc. ``` diff --git a/website/versioned_docs/version-25.x/ECMAScriptModules.md b/website/versioned_docs/version-25.x/ECMAScriptModules.md index 1b35bf8b1c02..e11eb61291ff 100644 --- a/website/versioned_docs/version-25.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-25.x/ECMAScriptModules.md @@ -43,8 +43,11 @@ module.exports = { example }; ``` ```js title="main.test.cjs" +import { createRequire } from 'node:module'; import { jest } from '@jest/globals'; +const require = createRequire(import.meta.url); + jest.mock('electron', () => ({ app: { on: jest.fn(), @@ -55,8 +58,8 @@ jest.mock('electron', () => ({ })) })); -const { BrowserWindow } = (await import('electron')).default; -const exported = await import('main.cjs'); +const { BrowserWindow } = require('electron'); +const exported = require('main.cjs'); // etc. ``` diff --git a/website/versioned_docs/version-26.x/ECMAScriptModules.md b/website/versioned_docs/version-26.x/ECMAScriptModules.md index 1b35bf8b1c02..e11eb61291ff 100644 --- a/website/versioned_docs/version-26.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-26.x/ECMAScriptModules.md @@ -43,8 +43,11 @@ module.exports = { example }; ``` ```js title="main.test.cjs" +import { createRequire } from 'node:module'; import { jest } from '@jest/globals'; +const require = createRequire(import.meta.url); + jest.mock('electron', () => ({ app: { on: jest.fn(), @@ -55,8 +58,8 @@ jest.mock('electron', () => ({ })) })); -const { BrowserWindow } = (await import('electron')).default; -const exported = await import('main.cjs'); +const { BrowserWindow } = require('electron'); +const exported = require('main.cjs'); // etc. ``` diff --git a/website/versioned_docs/version-27.x/ECMAScriptModules.md b/website/versioned_docs/version-27.x/ECMAScriptModules.md index 1528371beccd..72c7b2dcf448 100644 --- a/website/versioned_docs/version-27.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-27.x/ECMAScriptModules.md @@ -44,8 +44,11 @@ module.exports = { example }; ``` ```js title="main.test.cjs" +import { createRequire } from 'node:module'; import { jest } from '@jest/globals'; +const require = createRequire(import.meta.url); + jest.mock('electron', () => ({ app: { on: jest.fn(), @@ -56,8 +59,8 @@ jest.mock('electron', () => ({ })) })); -const { BrowserWindow } = (await import('electron')).default; -const exported = await import('main.cjs'); +const { BrowserWindow } = require('electron'); +const exported = require('main.cjs'); // etc. ``` diff --git a/website/versioned_docs/version-28.x/ECMAScriptModules.md b/website/versioned_docs/version-28.x/ECMAScriptModules.md index dde86ca25ab4..b46d870d30bb 100644 --- a/website/versioned_docs/version-28.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-28.x/ECMAScriptModules.md @@ -49,8 +49,11 @@ module.exports = { example }; ``` ```js title="main.test.cjs" +import { createRequire } from 'node:module'; import { jest } from '@jest/globals'; +const require = createRequire(import.meta.url); + jest.mock('electron', () => ({ app: { on: jest.fn(), @@ -61,8 +64,8 @@ jest.mock('electron', () => ({ })) })); -const { BrowserWindow } = (await import('electron')).default; -const exported = await import('main.cjs'); +const { BrowserWindow } = require('electron'); +const exported = require('main.cjs'); // etc. ``` diff --git a/website/versioned_docs/version-29.0/ECMAScriptModules.md b/website/versioned_docs/version-29.0/ECMAScriptModules.md index dde86ca25ab4..b46d870d30bb 100644 --- a/website/versioned_docs/version-29.0/ECMAScriptModules.md +++ b/website/versioned_docs/version-29.0/ECMAScriptModules.md @@ -49,8 +49,11 @@ module.exports = { example }; ``` ```js title="main.test.cjs" +import { createRequire } from 'node:module'; import { jest } from '@jest/globals'; +const require = createRequire(import.meta.url); + jest.mock('electron', () => ({ app: { on: jest.fn(), @@ -61,8 +64,8 @@ jest.mock('electron', () => ({ })) })); -const { BrowserWindow } = (await import('electron')).default; -const exported = await import('main.cjs'); +const { BrowserWindow } = require('electron'); +const exported = require('main.cjs'); // etc. ``` From 3d06b0ab2009737596d2bf122ad57ac04baa0c1a Mon Sep 17 00:00:00 2001 From: Wing Chau Date: Wed, 31 Aug 2022 18:06:01 +0100 Subject: [PATCH 08/14] Document `jest.unstable_mockModule` --- docs/ECMAScriptModules.md | 15 +++++++++++++++ .../version-27.x/ECMAScriptModules.md | 15 +++++++++++++++ .../version-28.x/ECMAScriptModules.md | 15 +++++++++++++++ .../version-29.0/ECMAScriptModules.md | 15 +++++++++++++++ 4 files changed, 60 insertions(+) diff --git a/docs/ECMAScriptModules.md b/docs/ECMAScriptModules.md index b46d870d30bb..3b583c1c00d0 100644 --- a/docs/ECMAScriptModules.md +++ b/docs/ECMAScriptModules.md @@ -71,3 +71,18 @@ const exported = require('main.cjs'); ``` 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. + +In short, `jest.unstable_mockModule` is needed to mock ESM for now. Its usage is essentially the same as `jest.mock`. See the example below: + +```js +import { jest } from '@jest/globals'; + +jest.unstable_mockModule('node:child_process', () => ({ + execSync: jest.fn(), + // etc. +})); + +const { execSync } = await import('node:child_process'); + +// etc. +``` diff --git a/website/versioned_docs/version-27.x/ECMAScriptModules.md b/website/versioned_docs/version-27.x/ECMAScriptModules.md index 72c7b2dcf448..85398db20056 100644 --- a/website/versioned_docs/version-27.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-27.x/ECMAScriptModules.md @@ -66,3 +66,18 @@ const exported = require('main.cjs'); ``` 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. + +In short, `jest.unstable_mockModule` is needed to mock ESM for now. Its usage is essentially the same as `jest.mock`. See the example below: + +```js +import { jest } from '@jest/globals'; + +jest.unstable_mockModule('node:child_process', () => ({ + execSync: jest.fn(), + // etc. +})); + +const { execSync } = await import('node:child_process'); + +// etc. +``` diff --git a/website/versioned_docs/version-28.x/ECMAScriptModules.md b/website/versioned_docs/version-28.x/ECMAScriptModules.md index b46d870d30bb..3b583c1c00d0 100644 --- a/website/versioned_docs/version-28.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-28.x/ECMAScriptModules.md @@ -71,3 +71,18 @@ const exported = require('main.cjs'); ``` 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. + +In short, `jest.unstable_mockModule` is needed to mock ESM for now. Its usage is essentially the same as `jest.mock`. See the example below: + +```js +import { jest } from '@jest/globals'; + +jest.unstable_mockModule('node:child_process', () => ({ + execSync: jest.fn(), + // etc. +})); + +const { execSync } = await import('node:child_process'); + +// etc. +``` diff --git a/website/versioned_docs/version-29.0/ECMAScriptModules.md b/website/versioned_docs/version-29.0/ECMAScriptModules.md index b46d870d30bb..3b583c1c00d0 100644 --- a/website/versioned_docs/version-29.0/ECMAScriptModules.md +++ b/website/versioned_docs/version-29.0/ECMAScriptModules.md @@ -71,3 +71,18 @@ const exported = require('main.cjs'); ``` 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. + +In short, `jest.unstable_mockModule` is needed to mock ESM for now. Its usage is essentially the same as `jest.mock`. See the example below: + +```js +import { jest } from '@jest/globals'; + +jest.unstable_mockModule('node:child_process', () => ({ + execSync: jest.fn(), + // etc. +})); + +const { execSync } = await import('node:child_process'); + +// etc. +``` From 87f81ba670b484389ea87dd034427a5f34ebabd4 Mon Sep 17 00:00:00 2001 From: Wing Chau Date: Fri, 2 Sep 2022 14:08:18 +0100 Subject: [PATCH 09/14] Reorder examples --- docs/ECMAScriptModules.md | 54 ++++++++++--------- .../version-25.x/ECMAScriptModules.md | 21 +++++--- .../version-26.x/ECMAScriptModules.md | 21 +++++--- .../version-27.x/ECMAScriptModules.md | 54 ++++++++++--------- .../version-28.x/ECMAScriptModules.md | 54 ++++++++++--------- .../version-29.0/ECMAScriptModules.md | 54 ++++++++++--------- 6 files changed, 142 insertions(+), 116 deletions(-) diff --git a/docs/ECMAScriptModules.md b/docs/ECMAScriptModules.md index 3b583c1c00d0..c16a7e09e374 100644 --- a/docs/ECMAScriptModules.md +++ b/docs/ECMAScriptModules.md @@ -38,19 +38,37 @@ import.meta.jest.useFakeTimers(); // jest === import.meta.jest => true ``` -Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. +Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. + +Please also 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. `jest.unstable_mockModule` is needed to mock ESM for now. Its usage is essentially the same as `jest.mock`. See the example below: + +```js +import {jest} from '@jest/globals'; + +jest.unstable_mockModule('node:child_process', () => ({ + execSync: jest.fn(), + // etc. +})); + +const {execSync} = await import('node:child_process'); + +// etc. +``` + +For mocking CJS modules, you should continue to use `jest.mock`. See the example below: ```js title="main.cjs" -const { BrowserWindow, app } = require('electron'); +const {BrowserWindow, app} = require('electron'); // etc. -module.exports = { example }; +module.exports = {example}; ``` + ```js title="main.test.cjs" -import { createRequire } from 'node:module'; -import { jest } from '@jest/globals'; +import {createRequire} from 'node:module'; +import {jest} from '@jest/globals'; const require = createRequire(import.meta.url); @@ -61,28 +79,14 @@ jest.mock('electron', () => ({ }, BrowserWindow: jest.fn().mockImplementation(() => ({ // partial mocks. - })) + })), })); -const { BrowserWindow } = require('electron'); -const exported = require('main.cjs'); - -// etc. -``` - -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. - -In short, `jest.unstable_mockModule` is needed to mock ESM for now. Its usage is essentially the same as `jest.mock`. See the example below: - -```js -import { jest } from '@jest/globals'; - -jest.unstable_mockModule('node:child_process', () => ({ - execSync: jest.fn(), - // etc. -})); - -const { execSync } = await import('node:child_process'); +const {BrowserWindow} = require('electron'); +const exported = require('./main.cjs'); +// alternatively +const {BrowserWindow} = (await import('electron')).default; +const exported = await import('./main.cjs'); // etc. ``` diff --git a/website/versioned_docs/version-25.x/ECMAScriptModules.md b/website/versioned_docs/version-25.x/ECMAScriptModules.md index e11eb61291ff..db425c47eab8 100644 --- a/website/versioned_docs/version-25.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-25.x/ECMAScriptModules.md @@ -32,19 +32,20 @@ jest.useFakeTimers(); // etc. ``` -Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. +Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. ```js title="main.cjs" -const { BrowserWindow, app } = require('electron'); +const {BrowserWindow, app} = require('electron'); // etc. -module.exports = { example }; +module.exports = {example}; ``` + ```js title="main.test.cjs" -import { createRequire } from 'node:module'; -import { jest } from '@jest/globals'; +import {createRequire} from 'node:module'; +import {jest} from '@jest/globals'; const require = createRequire(import.meta.url); @@ -55,11 +56,15 @@ jest.mock('electron', () => ({ }, BrowserWindow: jest.fn().mockImplementation(() => ({ // partial mocks. - })) + })), })); -const { BrowserWindow } = require('electron'); -const exported = require('main.cjs'); +const {BrowserWindow} = require('electron'); +const exported = require('./main.cjs'); + +// alternatively +const {BrowserWindow} = (await import('electron')).default; +const exported = await import('./main.cjs'); // etc. ``` diff --git a/website/versioned_docs/version-26.x/ECMAScriptModules.md b/website/versioned_docs/version-26.x/ECMAScriptModules.md index e11eb61291ff..db425c47eab8 100644 --- a/website/versioned_docs/version-26.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-26.x/ECMAScriptModules.md @@ -32,19 +32,20 @@ jest.useFakeTimers(); // etc. ``` -Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. +Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. ```js title="main.cjs" -const { BrowserWindow, app } = require('electron'); +const {BrowserWindow, app} = require('electron'); // etc. -module.exports = { example }; +module.exports = {example}; ``` + ```js title="main.test.cjs" -import { createRequire } from 'node:module'; -import { jest } from '@jest/globals'; +import {createRequire} from 'node:module'; +import {jest} from '@jest/globals'; const require = createRequire(import.meta.url); @@ -55,11 +56,15 @@ jest.mock('electron', () => ({ }, BrowserWindow: jest.fn().mockImplementation(() => ({ // partial mocks. - })) + })), })); -const { BrowserWindow } = require('electron'); -const exported = require('main.cjs'); +const {BrowserWindow} = require('electron'); +const exported = require('./main.cjs'); + +// alternatively +const {BrowserWindow} = (await import('electron')).default; +const exported = await import('./main.cjs'); // etc. ``` diff --git a/website/versioned_docs/version-27.x/ECMAScriptModules.md b/website/versioned_docs/version-27.x/ECMAScriptModules.md index 85398db20056..3a084406888d 100644 --- a/website/versioned_docs/version-27.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-27.x/ECMAScriptModules.md @@ -33,19 +33,37 @@ jest.useFakeTimers(); // etc. ``` -Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. +Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. + +Please also 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. `jest.unstable_mockModule` is needed to mock ESM for now. Its usage is essentially the same as `jest.mock`. See the example below: + +```js +import {jest} from '@jest/globals'; + +jest.unstable_mockModule('node:child_process', () => ({ + execSync: jest.fn(), + // etc. +})); + +const {execSync} = await import('node:child_process'); + +// etc. +``` + +For mocking CJS modules, you should continue to use `jest.mock`. See the example below: ```js title="main.cjs" -const { BrowserWindow, app } = require('electron'); +const {BrowserWindow, app} = require('electron'); // etc. -module.exports = { example }; +module.exports = {example}; ``` + ```js title="main.test.cjs" -import { createRequire } from 'node:module'; -import { jest } from '@jest/globals'; +import {createRequire} from 'node:module'; +import {jest} from '@jest/globals'; const require = createRequire(import.meta.url); @@ -56,28 +74,14 @@ jest.mock('electron', () => ({ }, BrowserWindow: jest.fn().mockImplementation(() => ({ // partial mocks. - })) -})); - -const { BrowserWindow } = require('electron'); -const exported = require('main.cjs'); - -// etc. -``` - -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. - -In short, `jest.unstable_mockModule` is needed to mock ESM for now. Its usage is essentially the same as `jest.mock`. See the example below: - -```js -import { jest } from '@jest/globals'; - -jest.unstable_mockModule('node:child_process', () => ({ - execSync: jest.fn(), - // etc. + })), })); -const { execSync } = await import('node:child_process'); +const {BrowserWindow} = require('electron'); +const exported = require('./main.cjs'); +// alternatively +const {BrowserWindow} = (await import('electron')).default; +const exported = await import('./main.cjs'); // etc. ``` diff --git a/website/versioned_docs/version-28.x/ECMAScriptModules.md b/website/versioned_docs/version-28.x/ECMAScriptModules.md index 3b583c1c00d0..c16a7e09e374 100644 --- a/website/versioned_docs/version-28.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-28.x/ECMAScriptModules.md @@ -38,19 +38,37 @@ import.meta.jest.useFakeTimers(); // jest === import.meta.jest => true ``` -Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. +Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. + +Please also 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. `jest.unstable_mockModule` is needed to mock ESM for now. Its usage is essentially the same as `jest.mock`. See the example below: + +```js +import {jest} from '@jest/globals'; + +jest.unstable_mockModule('node:child_process', () => ({ + execSync: jest.fn(), + // etc. +})); + +const {execSync} = await import('node:child_process'); + +// etc. +``` + +For mocking CJS modules, you should continue to use `jest.mock`. See the example below: ```js title="main.cjs" -const { BrowserWindow, app } = require('electron'); +const {BrowserWindow, app} = require('electron'); // etc. -module.exports = { example }; +module.exports = {example}; ``` + ```js title="main.test.cjs" -import { createRequire } from 'node:module'; -import { jest } from '@jest/globals'; +import {createRequire} from 'node:module'; +import {jest} from '@jest/globals'; const require = createRequire(import.meta.url); @@ -61,28 +79,14 @@ jest.mock('electron', () => ({ }, BrowserWindow: jest.fn().mockImplementation(() => ({ // partial mocks. - })) + })), })); -const { BrowserWindow } = require('electron'); -const exported = require('main.cjs'); - -// etc. -``` - -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. - -In short, `jest.unstable_mockModule` is needed to mock ESM for now. Its usage is essentially the same as `jest.mock`. See the example below: - -```js -import { jest } from '@jest/globals'; - -jest.unstable_mockModule('node:child_process', () => ({ - execSync: jest.fn(), - // etc. -})); - -const { execSync } = await import('node:child_process'); +const {BrowserWindow} = require('electron'); +const exported = require('./main.cjs'); +// alternatively +const {BrowserWindow} = (await import('electron')).default; +const exported = await import('./main.cjs'); // etc. ``` diff --git a/website/versioned_docs/version-29.0/ECMAScriptModules.md b/website/versioned_docs/version-29.0/ECMAScriptModules.md index 3b583c1c00d0..c16a7e09e374 100644 --- a/website/versioned_docs/version-29.0/ECMAScriptModules.md +++ b/website/versioned_docs/version-29.0/ECMAScriptModules.md @@ -38,19 +38,37 @@ import.meta.jest.useFakeTimers(); // jest === import.meta.jest => true ``` -Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. +Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. + +Please also 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. `jest.unstable_mockModule` is needed to mock ESM for now. Its usage is essentially the same as `jest.mock`. See the example below: + +```js +import {jest} from '@jest/globals'; + +jest.unstable_mockModule('node:child_process', () => ({ + execSync: jest.fn(), + // etc. +})); + +const {execSync} = await import('node:child_process'); + +// etc. +``` + +For mocking CJS modules, you should continue to use `jest.mock`. See the example below: ```js title="main.cjs" -const { BrowserWindow, app } = require('electron'); +const {BrowserWindow, app} = require('electron'); // etc. -module.exports = { example }; +module.exports = {example}; ``` + ```js title="main.test.cjs" -import { createRequire } from 'node:module'; -import { jest } from '@jest/globals'; +import {createRequire} from 'node:module'; +import {jest} from '@jest/globals'; const require = createRequire(import.meta.url); @@ -61,28 +79,14 @@ jest.mock('electron', () => ({ }, BrowserWindow: jest.fn().mockImplementation(() => ({ // partial mocks. - })) + })), })); -const { BrowserWindow } = require('electron'); -const exported = require('main.cjs'); - -// etc. -``` - -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. - -In short, `jest.unstable_mockModule` is needed to mock ESM for now. Its usage is essentially the same as `jest.mock`. See the example below: - -```js -import { jest } from '@jest/globals'; - -jest.unstable_mockModule('node:child_process', () => ({ - execSync: jest.fn(), - // etc. -})); - -const { execSync } = await import('node:child_process'); +const {BrowserWindow} = require('electron'); +const exported = require('./main.cjs'); +// alternatively +const {BrowserWindow} = (await import('electron')).default; +const exported = await import('./main.cjs'); // etc. ``` From 7c58f12e89d54c973c037ed1f89c760ead2d1d74 Mon Sep 17 00:00:00 2001 From: Wing Chau Date: Fri, 2 Sep 2022 14:31:27 +0100 Subject: [PATCH 10/14] Override ESLint rule for docs - Diabled `no-redeclare`. --- .eslintrc.cjs | 1 + docs/ECMAScriptModules.md | 2 +- website/versioned_docs/version-25.x/ECMAScriptModules.md | 1 - website/versioned_docs/version-26.x/ECMAScriptModules.md | 1 - website/versioned_docs/version-27.x/ECMAScriptModules.md | 2 +- website/versioned_docs/version-28.x/ECMAScriptModules.md | 2 +- website/versioned_docs/version-29.0/ECMAScriptModules.md | 2 +- 7 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index c7a6e781bc9b..44dce82fda45 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -182,6 +182,7 @@ module.exports = { { files: ['docs/**/*', 'website/**/*'], rules: { + 'no-redeclare': 'off', 'import/order': 'off', 'import/sort-keys': 'off', 'no-restricted-globals': ['off'], diff --git a/docs/ECMAScriptModules.md b/docs/ECMAScriptModules.md index c16a7e09e374..d503305da793 100644 --- a/docs/ECMAScriptModules.md +++ b/docs/ECMAScriptModules.md @@ -65,7 +65,6 @@ const {BrowserWindow, app} = require('electron'); module.exports = {example}; ``` - ```js title="main.test.cjs" import {createRequire} from 'node:module'; import {jest} from '@jest/globals'; @@ -88,5 +87,6 @@ const exported = require('./main.cjs'); // alternatively const {BrowserWindow} = (await import('electron')).default; const exported = await import('./main.cjs'); + // etc. ``` diff --git a/website/versioned_docs/version-25.x/ECMAScriptModules.md b/website/versioned_docs/version-25.x/ECMAScriptModules.md index db425c47eab8..d3e92c4bda92 100644 --- a/website/versioned_docs/version-25.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-25.x/ECMAScriptModules.md @@ -42,7 +42,6 @@ const {BrowserWindow, app} = require('electron'); module.exports = {example}; ``` - ```js title="main.test.cjs" import {createRequire} from 'node:module'; import {jest} from '@jest/globals'; diff --git a/website/versioned_docs/version-26.x/ECMAScriptModules.md b/website/versioned_docs/version-26.x/ECMAScriptModules.md index db425c47eab8..d3e92c4bda92 100644 --- a/website/versioned_docs/version-26.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-26.x/ECMAScriptModules.md @@ -42,7 +42,6 @@ const {BrowserWindow, app} = require('electron'); module.exports = {example}; ``` - ```js title="main.test.cjs" import {createRequire} from 'node:module'; import {jest} from '@jest/globals'; diff --git a/website/versioned_docs/version-27.x/ECMAScriptModules.md b/website/versioned_docs/version-27.x/ECMAScriptModules.md index 3a084406888d..68c6f7403810 100644 --- a/website/versioned_docs/version-27.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-27.x/ECMAScriptModules.md @@ -60,7 +60,6 @@ const {BrowserWindow, app} = require('electron'); module.exports = {example}; ``` - ```js title="main.test.cjs" import {createRequire} from 'node:module'; import {jest} from '@jest/globals'; @@ -83,5 +82,6 @@ const exported = require('./main.cjs'); // alternatively const {BrowserWindow} = (await import('electron')).default; const exported = await import('./main.cjs'); + // etc. ``` diff --git a/website/versioned_docs/version-28.x/ECMAScriptModules.md b/website/versioned_docs/version-28.x/ECMAScriptModules.md index c16a7e09e374..d503305da793 100644 --- a/website/versioned_docs/version-28.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-28.x/ECMAScriptModules.md @@ -65,7 +65,6 @@ const {BrowserWindow, app} = require('electron'); module.exports = {example}; ``` - ```js title="main.test.cjs" import {createRequire} from 'node:module'; import {jest} from '@jest/globals'; @@ -88,5 +87,6 @@ const exported = require('./main.cjs'); // alternatively const {BrowserWindow} = (await import('electron')).default; const exported = await import('./main.cjs'); + // etc. ``` diff --git a/website/versioned_docs/version-29.0/ECMAScriptModules.md b/website/versioned_docs/version-29.0/ECMAScriptModules.md index c16a7e09e374..d503305da793 100644 --- a/website/versioned_docs/version-29.0/ECMAScriptModules.md +++ b/website/versioned_docs/version-29.0/ECMAScriptModules.md @@ -65,7 +65,6 @@ const {BrowserWindow, app} = require('electron'); module.exports = {example}; ``` - ```js title="main.test.cjs" import {createRequire} from 'node:module'; import {jest} from '@jest/globals'; @@ -88,5 +87,6 @@ const exported = require('./main.cjs'); // alternatively const {BrowserWindow} = (await import('electron')).default; const exported = await import('./main.cjs'); + // etc. ``` From a0f927139139a632582f95a24af2a421b9ddb7ec Mon Sep 17 00:00:00 2001 From: Wing Chau Date: Fri, 2 Sep 2022 14:38:40 +0100 Subject: [PATCH 11/14] Improve `unstable_mockModule` doc - Chagned to the suggested version. --- docs/ECMAScriptModules.md | 4 +++- website/versioned_docs/version-27.x/ECMAScriptModules.md | 4 +++- website/versioned_docs/version-28.x/ECMAScriptModules.md | 4 +++- website/versioned_docs/version-29.0/ECMAScriptModules.md | 4 +++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/docs/ECMAScriptModules.md b/docs/ECMAScriptModules.md index d503305da793..5a4e5d6828f0 100644 --- a/docs/ECMAScriptModules.md +++ b/docs/ECMAScriptModules.md @@ -40,7 +40,9 @@ import.meta.jest.useFakeTimers(); Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. -Please also 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. `jest.unstable_mockModule` is needed to mock ESM for now. Its usage is essentially the same as `jest.mock`. See the example below: +ESM module mocking is supported through `jest.unstable_mockModule`. As the name suggest works are in progress, please follow [this issue](https://github.com/facebook/jest/issues/10025) for updates. + +The usage of `jest.unstable_mockModule` is essentially the same as `jest.mock` with two differences: the factory function is required and it can be sync or async: ```js import {jest} from '@jest/globals'; diff --git a/website/versioned_docs/version-27.x/ECMAScriptModules.md b/website/versioned_docs/version-27.x/ECMAScriptModules.md index 68c6f7403810..e5a95d398a30 100644 --- a/website/versioned_docs/version-27.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-27.x/ECMAScriptModules.md @@ -35,7 +35,9 @@ jest.useFakeTimers(); Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. -Please also 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. `jest.unstable_mockModule` is needed to mock ESM for now. Its usage is essentially the same as `jest.mock`. See the example below: +ESM module mocking is supported through `jest.unstable_mockModule`. As the name suggest works are in progress, please follow [this issue](https://github.com/facebook/jest/issues/10025) for updates. + +The usage of `jest.unstable_mockModule` is essentially the same as `jest.mock` with two differences: the factory function is required and it can be sync or async: ```js import {jest} from '@jest/globals'; diff --git a/website/versioned_docs/version-28.x/ECMAScriptModules.md b/website/versioned_docs/version-28.x/ECMAScriptModules.md index d503305da793..5a4e5d6828f0 100644 --- a/website/versioned_docs/version-28.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-28.x/ECMAScriptModules.md @@ -40,7 +40,9 @@ import.meta.jest.useFakeTimers(); Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. -Please also 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. `jest.unstable_mockModule` is needed to mock ESM for now. Its usage is essentially the same as `jest.mock`. See the example below: +ESM module mocking is supported through `jest.unstable_mockModule`. As the name suggest works are in progress, please follow [this issue](https://github.com/facebook/jest/issues/10025) for updates. + +The usage of `jest.unstable_mockModule` is essentially the same as `jest.mock` with two differences: the factory function is required and it can be sync or async: ```js import {jest} from '@jest/globals'; diff --git a/website/versioned_docs/version-29.0/ECMAScriptModules.md b/website/versioned_docs/version-29.0/ECMAScriptModules.md index d503305da793..5a4e5d6828f0 100644 --- a/website/versioned_docs/version-29.0/ECMAScriptModules.md +++ b/website/versioned_docs/version-29.0/ECMAScriptModules.md @@ -40,7 +40,9 @@ import.meta.jest.useFakeTimers(); Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. -Please also 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. `jest.unstable_mockModule` is needed to mock ESM for now. Its usage is essentially the same as `jest.mock`. See the example below: +ESM module mocking is supported through `jest.unstable_mockModule`. As the name suggest works are in progress, please follow [this issue](https://github.com/facebook/jest/issues/10025) for updates. + +The usage of `jest.unstable_mockModule` is essentially the same as `jest.mock` with two differences: the factory function is required and it can be sync or async: ```js import {jest} from '@jest/globals'; From 3c0755f6a509112dcd666df4407cefeabca4d3dd Mon Sep 17 00:00:00 2001 From: Wing Chau Date: Fri, 2 Sep 2022 16:04:27 +0100 Subject: [PATCH 12/14] Add doc section in `ECMAScriptModules` - Added section title `MOdule mocking in ESM` on `v27+` only. --- docs/ECMAScriptModules.md | 4 +++- website/versioned_docs/version-27.x/ECMAScriptModules.md | 4 +++- website/versioned_docs/version-28.x/ECMAScriptModules.md | 4 +++- website/versioned_docs/version-29.0/ECMAScriptModules.md | 4 +++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/docs/ECMAScriptModules.md b/docs/ECMAScriptModules.md index 5a4e5d6828f0..58975a253c01 100644 --- a/docs/ECMAScriptModules.md +++ b/docs/ECMAScriptModules.md @@ -38,7 +38,9 @@ import.meta.jest.useFakeTimers(); // jest === import.meta.jest => true ``` -Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. +## Module mocking in ESM + +Since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. ESM module mocking is supported through `jest.unstable_mockModule`. As the name suggest works are in progress, please follow [this issue](https://github.com/facebook/jest/issues/10025) for updates. diff --git a/website/versioned_docs/version-27.x/ECMAScriptModules.md b/website/versioned_docs/version-27.x/ECMAScriptModules.md index e5a95d398a30..f6ef871d9645 100644 --- a/website/versioned_docs/version-27.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-27.x/ECMAScriptModules.md @@ -33,7 +33,9 @@ jest.useFakeTimers(); // etc. ``` -Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. +## Module mocking in ESM + +Since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. ESM module mocking is supported through `jest.unstable_mockModule`. As the name suggest works are in progress, please follow [this issue](https://github.com/facebook/jest/issues/10025) for updates. diff --git a/website/versioned_docs/version-28.x/ECMAScriptModules.md b/website/versioned_docs/version-28.x/ECMAScriptModules.md index 5a4e5d6828f0..58975a253c01 100644 --- a/website/versioned_docs/version-28.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-28.x/ECMAScriptModules.md @@ -38,7 +38,9 @@ import.meta.jest.useFakeTimers(); // jest === import.meta.jest => true ``` -Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. +## Module mocking in ESM + +Since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. ESM module mocking is supported through `jest.unstable_mockModule`. As the name suggest works are in progress, please follow [this issue](https://github.com/facebook/jest/issues/10025) for updates. diff --git a/website/versioned_docs/version-29.0/ECMAScriptModules.md b/website/versioned_docs/version-29.0/ECMAScriptModules.md index 5a4e5d6828f0..58975a253c01 100644 --- a/website/versioned_docs/version-29.0/ECMAScriptModules.md +++ b/website/versioned_docs/version-29.0/ECMAScriptModules.md @@ -38,7 +38,9 @@ import.meta.jest.useFakeTimers(); // jest === import.meta.jest => true ``` -Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. +## Module mocking in ESM + +Since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. ESM module mocking is supported through `jest.unstable_mockModule`. As the name suggest works are in progress, please follow [this issue](https://github.com/facebook/jest/issues/10025) for updates. From d8f155ea895d3cab8450b3162f07ebfd6787a61d Mon Sep 17 00:00:00 2001 From: Wing Chau Date: Fri, 2 Sep 2022 16:19:57 +0100 Subject: [PATCH 13/14] Fix v25 sidebar --- website/versioned_sidebars/version-25.x-sidebars.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/website/versioned_sidebars/version-25.x-sidebars.json b/website/versioned_sidebars/version-25.x-sidebars.json index b390bdf1c162..c086e4e89978 100644 --- a/website/versioned_sidebars/version-25.x-sidebars.json +++ b/website/versioned_sidebars/version-25.x-sidebars.json @@ -66,6 +66,10 @@ "type": "doc", "id": "version-25.x/bypassing-module-mocks" }, + { + "type": "doc", + "id": "version-25.x/ecmascript-modules" + }, { "type": "doc", "id": "version-25.x/webpack" From f6ed55eedcaf5853329331caf4006dab8cbcd5f0 Mon Sep 17 00:00:00 2001 From: Wing Chau Date: Sat, 3 Sep 2022 13:37:40 +0100 Subject: [PATCH 14/14] Refine wordings --- docs/ECMAScriptModules.md | 4 ++-- docs/ManualMocks.md | 2 +- website/versioned_docs/version-25.x/ECMAScriptModules.md | 2 +- website/versioned_docs/version-25.x/ManualMocks.md | 2 +- website/versioned_docs/version-26.x/ECMAScriptModules.md | 2 +- website/versioned_docs/version-26.x/ManualMocks.md | 2 +- website/versioned_docs/version-27.x/ECMAScriptModules.md | 4 ++-- website/versioned_docs/version-27.x/ManualMocks.md | 2 +- website/versioned_docs/version-28.x/ECMAScriptModules.md | 4 ++-- website/versioned_docs/version-28.x/ManualMocks.md | 2 +- website/versioned_docs/version-29.0/ECMAScriptModules.md | 4 ++-- website/versioned_docs/version-29.0/ManualMocks.md | 2 +- 12 files changed, 16 insertions(+), 16 deletions(-) diff --git a/docs/ECMAScriptModules.md b/docs/ECMAScriptModules.md index 58975a253c01..d838c707248f 100644 --- a/docs/ECMAScriptModules.md +++ b/docs/ECMAScriptModules.md @@ -40,9 +40,9 @@ import.meta.jest.useFakeTimers(); ## Module mocking in ESM -Since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. +Since ESM evaluates static `import` statements before looking at the code, the hoisting of `jest.mock` calls that happens in CJS won't work for ESM. To mock modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules - the same applies to modules which load the mocked modules. -ESM module mocking is supported through `jest.unstable_mockModule`. As the name suggest works are in progress, please follow [this issue](https://github.com/facebook/jest/issues/10025) for updates. +ESM mocking is supported through `jest.unstable_mockModule`. As the name suggests, this API is still work in progress, please follow [this issue](https://github.com/facebook/jest/issues/10025) for updates. The usage of `jest.unstable_mockModule` is essentially the same as `jest.mock` with two differences: the factory function is required and it can be sync or async: diff --git a/docs/ManualMocks.md b/docs/ManualMocks.md index 212eb0d55642..c0d7ea2243ee 100644 --- a/docs/ManualMocks.md +++ b/docs/ManualMocks.md @@ -132,7 +132,7 @@ If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web :::caution -`jest.mock` calls cannot be hoisted to the top of the module if you enabled ECMAScript modules support. The ESM module loader always evaluates the imports before executing code. See [ECMAScriptModules](ECMAScriptModules.md) for details. +`jest.mock` calls cannot be hoisted to the top of the module if you enabled ECMAScript modules support. The ESM module loader always evaluates the static imports before executing code. See [ECMAScriptModules](ECMAScriptModules.md) for details. ::: diff --git a/website/versioned_docs/version-25.x/ECMAScriptModules.md b/website/versioned_docs/version-25.x/ECMAScriptModules.md index d3e92c4bda92..28cf24d550b0 100644 --- a/website/versioned_docs/version-25.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-25.x/ECMAScriptModules.md @@ -32,7 +32,7 @@ jest.useFakeTimers(); // etc. ``` -Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. +Additionally, since ESM evaluates static `import` statements before looking at the code, the hoisting of `jest.mock` calls that happens in CJS won't work in ESM. To mock modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules - the same applies to modules which load the mocked modules. ```js title="main.cjs" const {BrowserWindow, app} = require('electron'); diff --git a/website/versioned_docs/version-25.x/ManualMocks.md b/website/versioned_docs/version-25.x/ManualMocks.md index 3a4e4104539a..ae34120c8ec8 100644 --- a/website/versioned_docs/version-25.x/ManualMocks.md +++ b/website/versioned_docs/version-25.x/ManualMocks.md @@ -132,7 +132,7 @@ If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web :::caution -`jest.mock` calls cannot be hoisted to the top of the module if you enabled ECMAScript modules support. The ESM module loader always evaluates the imports before executing code. See [ECMAScriptModules](ECMAScriptModules.md) for details. +`jest.mock` calls cannot be hoisted to the top of the module if you enabled ECMAScript modules support. The ESM module loader always evaluates the static imports before executing code. See [ECMAScriptModules](ECMAScriptModules.md) for details. ::: diff --git a/website/versioned_docs/version-26.x/ECMAScriptModules.md b/website/versioned_docs/version-26.x/ECMAScriptModules.md index d3e92c4bda92..28cf24d550b0 100644 --- a/website/versioned_docs/version-26.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-26.x/ECMAScriptModules.md @@ -32,7 +32,7 @@ jest.useFakeTimers(); // etc. ``` -Additionally, since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. +Additionally, since ESM evaluates static `import` statements before looking at the code, the hoisting of `jest.mock` calls that happens in CJS won't work in ESM. To mock modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules - the same applies to modules which load the mocked modules. ```js title="main.cjs" const {BrowserWindow, app} = require('electron'); diff --git a/website/versioned_docs/version-26.x/ManualMocks.md b/website/versioned_docs/version-26.x/ManualMocks.md index 212eb0d55642..c0d7ea2243ee 100644 --- a/website/versioned_docs/version-26.x/ManualMocks.md +++ b/website/versioned_docs/version-26.x/ManualMocks.md @@ -132,7 +132,7 @@ If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web :::caution -`jest.mock` calls cannot be hoisted to the top of the module if you enabled ECMAScript modules support. The ESM module loader always evaluates the imports before executing code. See [ECMAScriptModules](ECMAScriptModules.md) for details. +`jest.mock` calls cannot be hoisted to the top of the module if you enabled ECMAScript modules support. The ESM module loader always evaluates the static imports before executing code. See [ECMAScriptModules](ECMAScriptModules.md) for details. ::: diff --git a/website/versioned_docs/version-27.x/ECMAScriptModules.md b/website/versioned_docs/version-27.x/ECMAScriptModules.md index f6ef871d9645..576b645bfe11 100644 --- a/website/versioned_docs/version-27.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-27.x/ECMAScriptModules.md @@ -35,9 +35,9 @@ jest.useFakeTimers(); ## Module mocking in ESM -Since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. +Since ESM evaluates static `import` statements before looking at the code, the hoisting of `jest.mock` calls that happens in CJS won't work for ESM. To mock modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules - the same applies to modules which load the mocked modules. -ESM module mocking is supported through `jest.unstable_mockModule`. As the name suggest works are in progress, please follow [this issue](https://github.com/facebook/jest/issues/10025) for updates. +ESM mocking is supported through `jest.unstable_mockModule`. As the name suggests, this API is still work in progress, please follow [this issue](https://github.com/facebook/jest/issues/10025) for updates. The usage of `jest.unstable_mockModule` is essentially the same as `jest.mock` with two differences: the factory function is required and it can be sync or async: diff --git a/website/versioned_docs/version-27.x/ManualMocks.md b/website/versioned_docs/version-27.x/ManualMocks.md index 212eb0d55642..c0d7ea2243ee 100644 --- a/website/versioned_docs/version-27.x/ManualMocks.md +++ b/website/versioned_docs/version-27.x/ManualMocks.md @@ -132,7 +132,7 @@ If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web :::caution -`jest.mock` calls cannot be hoisted to the top of the module if you enabled ECMAScript modules support. The ESM module loader always evaluates the imports before executing code. See [ECMAScriptModules](ECMAScriptModules.md) for details. +`jest.mock` calls cannot be hoisted to the top of the module if you enabled ECMAScript modules support. The ESM module loader always evaluates the static imports before executing code. See [ECMAScriptModules](ECMAScriptModules.md) for details. ::: diff --git a/website/versioned_docs/version-28.x/ECMAScriptModules.md b/website/versioned_docs/version-28.x/ECMAScriptModules.md index 58975a253c01..d838c707248f 100644 --- a/website/versioned_docs/version-28.x/ECMAScriptModules.md +++ b/website/versioned_docs/version-28.x/ECMAScriptModules.md @@ -40,9 +40,9 @@ import.meta.jest.useFakeTimers(); ## Module mocking in ESM -Since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. +Since ESM evaluates static `import` statements before looking at the code, the hoisting of `jest.mock` calls that happens in CJS won't work for ESM. To mock modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules - the same applies to modules which load the mocked modules. -ESM module mocking is supported through `jest.unstable_mockModule`. As the name suggest works are in progress, please follow [this issue](https://github.com/facebook/jest/issues/10025) for updates. +ESM mocking is supported through `jest.unstable_mockModule`. As the name suggests, this API is still work in progress, please follow [this issue](https://github.com/facebook/jest/issues/10025) for updates. The usage of `jest.unstable_mockModule` is essentially the same as `jest.mock` with two differences: the factory function is required and it can be sync or async: diff --git a/website/versioned_docs/version-28.x/ManualMocks.md b/website/versioned_docs/version-28.x/ManualMocks.md index 212eb0d55642..c0d7ea2243ee 100644 --- a/website/versioned_docs/version-28.x/ManualMocks.md +++ b/website/versioned_docs/version-28.x/ManualMocks.md @@ -132,7 +132,7 @@ If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web :::caution -`jest.mock` calls cannot be hoisted to the top of the module if you enabled ECMAScript modules support. The ESM module loader always evaluates the imports before executing code. See [ECMAScriptModules](ECMAScriptModules.md) for details. +`jest.mock` calls cannot be hoisted to the top of the module if you enabled ECMAScript modules support. The ESM module loader always evaluates the static imports before executing code. See [ECMAScriptModules](ECMAScriptModules.md) for details. ::: diff --git a/website/versioned_docs/version-29.0/ECMAScriptModules.md b/website/versioned_docs/version-29.0/ECMAScriptModules.md index 58975a253c01..d838c707248f 100644 --- a/website/versioned_docs/version-29.0/ECMAScriptModules.md +++ b/website/versioned_docs/version-29.0/ECMAScriptModules.md @@ -40,9 +40,9 @@ import.meta.jest.useFakeTimers(); ## Module mocking in ESM -Since ESM evaluates static `import` statements before looking at the code, hoisting on `jest.mock` calls that happens in CJS modules won't work in ESM. To `mock` modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules, same applies to modules which have to load the mocked modules. +Since ESM evaluates static `import` statements before looking at the code, the hoisting of `jest.mock` calls that happens in CJS won't work for ESM. To mock modules in ESM, you need to use `require` or dynamic `import()` after `jest.mock` calls to load the mocked modules - the same applies to modules which load the mocked modules. -ESM module mocking is supported through `jest.unstable_mockModule`. As the name suggest works are in progress, please follow [this issue](https://github.com/facebook/jest/issues/10025) for updates. +ESM mocking is supported through `jest.unstable_mockModule`. As the name suggests, this API is still work in progress, please follow [this issue](https://github.com/facebook/jest/issues/10025) for updates. The usage of `jest.unstable_mockModule` is essentially the same as `jest.mock` with two differences: the factory function is required and it can be sync or async: diff --git a/website/versioned_docs/version-29.0/ManualMocks.md b/website/versioned_docs/version-29.0/ManualMocks.md index 212eb0d55642..c0d7ea2243ee 100644 --- a/website/versioned_docs/version-29.0/ManualMocks.md +++ b/website/versioned_docs/version-29.0/ManualMocks.md @@ -132,7 +132,7 @@ If you're using [ES module imports](https://developer.mozilla.org/en-US/docs/Web :::caution -`jest.mock` calls cannot be hoisted to the top of the module if you enabled ECMAScript modules support. The ESM module loader always evaluates the imports before executing code. See [ECMAScriptModules](ECMAScriptModules.md) for details. +`jest.mock` calls cannot be hoisted to the top of the module if you enabled ECMAScript modules support. The ESM module loader always evaluates the static imports before executing code. See [ECMAScriptModules](ECMAScriptModules.md) for details. :::