From 005cafe06a7c9f34d82ca2cf39cf79f76784e55f Mon Sep 17 00:00:00 2001 From: Spyros Argalias Date: Mon, 17 Jun 2019 20:12:27 +0100 Subject: [PATCH 1/5] docs(jest.doMock): Add information for ES6 modules --- docs/JestObjectAPI.md | 40 +++++++++++++++++++ .../version-22.x/JestObjectAPI.md | 40 +++++++++++++++++++ .../version-23.x/JestObjectAPI.md | 40 +++++++++++++++++++ .../version-24.0/JestObjectAPI.md | 40 +++++++++++++++++++ 4 files changed, 160 insertions(+) diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index bf73f02b69cd..251f011e4c10 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -314,6 +314,46 @@ test('moduleName 2', () => { }); ``` +Using `jest.doMock()` with ES6 modules requires additional steps: + +- We have to specify the `__esModule: true` property (see the [`jest.mock()`](#jestmockmodulename-factory-options) API for more information). +- Static ES6 module imports can only appear at the top of the file, so instead we have to import them dynamically using `import()`. +- Finally, we need an environment which supports dynamic importing. Please see [Using Babel](GettingStarted.md#using-babel) for the initial setup. Then add the plugin [babel-plugin-dynamic-import-node](https://www.npmjs.com/package/babel-plugin-dynamic-import-node), or an equivalent, to your Babel config to enable dynamic importing in Node. + +```js +beforeEach(() => { + jest.resetModules(); +}); + +test('moduleName 1', () => { + jest.doMock('../moduleName', () => { + return { + __esModule: true, + default: 'default1', + foo: 'foo1', + }; + }); + import('../moduleName').then(moduleName => { + expect(moduleName.default).toEqual('default1'); + expect(moduleName.foo).toEqual('foo1'); + }); +}); + +test('moduleName 2', () => { + jest.doMock('../moduleName', () => { + return { + __esModule: true, + default: 'default2', + foo: 'foo2', + }; + }); + import('../moduleName').then(moduleName => { + expect(moduleName.default).toEqual('default2'); + expect(moduleName.foo).toEqual('foo2'); + }); +}); +``` + Returns the `jest` object for chaining. ### `jest.dontMock(moduleName)` diff --git a/website/versioned_docs/version-22.x/JestObjectAPI.md b/website/versioned_docs/version-22.x/JestObjectAPI.md index 6f1c9e765882..5bf6cd1ab04d 100644 --- a/website/versioned_docs/version-22.x/JestObjectAPI.md +++ b/website/versioned_docs/version-22.x/JestObjectAPI.md @@ -368,6 +368,46 @@ test('moduleName 2', () => { }); ``` +Using `jest.doMock()` with ES6 modules requires additional steps: + +- We have to specify the `__esModule: true` property (see the [`jest.mock()`](#jestmockmodulename-factory-options) API for more information). +- Static ES6 module imports can only appear at the top of the file, so instead we have to import them dynamically using `import()`. +- Finally, we need an environment which supports dynamic importing. Please see [Using Babel](GettingStarted.md#using-babel) for the initial setup. Then add the plugin [babel-plugin-dynamic-import-node](https://www.npmjs.com/package/babel-plugin-dynamic-import-node), or an equivalent, to your Babel config to enable dynamic importing in Node. + +```js +beforeEach(() => { + jest.resetModules(); +}); + +test('moduleName 1', () => { + jest.doMock('../moduleName', () => { + return { + __esModule: true, + default: 'default1', + foo: 'foo1', + }; + }); + import('../moduleName').then(moduleName => { + expect(moduleName.default).toEqual('default1'); + expect(moduleName.foo).toEqual('foo1'); + }); +}); + +test('moduleName 2', () => { + jest.doMock('../moduleName', () => { + return { + __esModule: true, + default: 'default2', + foo: 'foo2', + }; + }); + import('../moduleName').then(moduleName => { + expect(moduleName.default).toEqual('default2'); + expect(moduleName.foo).toEqual('foo2'); + }); +}); +``` + Returns the `jest` object for chaining. ### `jest.dontMock(moduleName)` diff --git a/website/versioned_docs/version-23.x/JestObjectAPI.md b/website/versioned_docs/version-23.x/JestObjectAPI.md index 62ec5e649334..ecf76d36fd85 100644 --- a/website/versioned_docs/version-23.x/JestObjectAPI.md +++ b/website/versioned_docs/version-23.x/JestObjectAPI.md @@ -369,6 +369,46 @@ test('moduleName 2', () => { }); ``` +Using `jest.doMock()` with ES6 modules requires additional steps: + +- We have to specify the `__esModule: true` property (see the [`jest.mock()`](#jestmockmodulename-factory-options) API for more information). +- Static ES6 module imports can only appear at the top of the file, so instead we have to import them dynamically using `import()`. +- Finally, we need an environment which supports dynamic importing. Please see [Using Babel](GettingStarted.md#using-babel) for the initial setup. Then add the plugin [babel-plugin-dynamic-import-node](https://www.npmjs.com/package/babel-plugin-dynamic-import-node), or an equivalent, to your Babel config to enable dynamic importing in Node. + +```js +beforeEach(() => { + jest.resetModules(); +}); + +test('moduleName 1', () => { + jest.doMock('../moduleName', () => { + return { + __esModule: true, + default: 'default1', + foo: 'foo1', + }; + }); + import('../moduleName').then(moduleName => { + expect(moduleName.default).toEqual('default1'); + expect(moduleName.foo).toEqual('foo1'); + }); +}); + +test('moduleName 2', () => { + jest.doMock('../moduleName', () => { + return { + __esModule: true, + default: 'default2', + foo: 'foo2', + }; + }); + import('../moduleName').then(moduleName => { + expect(moduleName.default).toEqual('default2'); + expect(moduleName.foo).toEqual('foo2'); + }); +}); +``` + Returns the `jest` object for chaining. ### `jest.dontMock(moduleName)` diff --git a/website/versioned_docs/version-24.0/JestObjectAPI.md b/website/versioned_docs/version-24.0/JestObjectAPI.md index 5ddf386f97b3..d8bdd8764e48 100644 --- a/website/versioned_docs/version-24.0/JestObjectAPI.md +++ b/website/versioned_docs/version-24.0/JestObjectAPI.md @@ -315,6 +315,46 @@ test('moduleName 2', () => { }); ``` +Using `jest.doMock()` with ES6 modules requires additional steps: + +- We have to specify the `__esModule: true` property (see the [`jest.mock()`](#jestmockmodulename-factory-options) API for more information). +- Static ES6 module imports can only appear at the top of the file, so instead we have to import them dynamically using `import()`. +- Finally, we need an environment which supports dynamic importing. Please see [Using Babel](GettingStarted.md#using-babel) for the initial setup. Then add the plugin [babel-plugin-dynamic-import-node](https://www.npmjs.com/package/babel-plugin-dynamic-import-node), or an equivalent, to your Babel config to enable dynamic importing in Node. + +```js +beforeEach(() => { + jest.resetModules(); +}); + +test('moduleName 1', () => { + jest.doMock('../moduleName', () => { + return { + __esModule: true, + default: 'default1', + foo: 'foo1', + }; + }); + import('../moduleName').then(moduleName => { + expect(moduleName.default).toEqual('default1'); + expect(moduleName.foo).toEqual('foo1'); + }); +}); + +test('moduleName 2', () => { + jest.doMock('../moduleName', () => { + return { + __esModule: true, + default: 'default2', + foo: 'foo2', + }; + }); + import('../moduleName').then(moduleName => { + expect(moduleName.default).toEqual('default2'); + expect(moduleName.foo).toEqual('foo2'); + }); +}); +``` + Returns the `jest` object for chaining. ### `jest.dontMock(moduleName)` From 024868d8b6e92f8aaa660f84cdc460c8f2dd9e23 Mon Sep 17 00:00:00 2001 From: Spyros Argalias Date: Mon, 17 Jun 2019 20:36:48 +0100 Subject: [PATCH 2/5] chore(CHANGELOG): Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fabc33277c85..a28e0540d4de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ - `[jest-leak-detector]` remove code repeat ([#8438](https://github.com/facebook/jest/pull/8438) - `[docs]` Add example to `jest.requireActual` ([#8482](https://github.com/facebook/jest/pull/8482) - `[docs]` Add example to `jest.mock` for mocking ES6 modules with the `factory` parameter ([#8550](https://github.com/facebook/jest/pull/8550)) +- `[docs]` Add information about using `jest.doMock` with ES6 modules ([#8573](https://github.com/facebook/jest/pull/8573)) ### Performance From e255a400e280f8c49466a9ab01cc3722e77616d0 Mon Sep 17 00:00:00 2001 From: Spyros Argalias Date: Tue, 18 Jun 2019 00:02:18 +0100 Subject: [PATCH 3/5] docs(jest.doMock): Fix statement on import hoisting behavior --- docs/JestObjectAPI.md | 2 +- website/versioned_docs/version-22.x/JestObjectAPI.md | 2 +- website/versioned_docs/version-23.x/JestObjectAPI.md | 2 +- website/versioned_docs/version-24.0/JestObjectAPI.md | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index 251f011e4c10..d329090a9090 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -317,7 +317,7 @@ test('moduleName 2', () => { Using `jest.doMock()` with ES6 modules requires additional steps: - We have to specify the `__esModule: true` property (see the [`jest.mock()`](#jestmockmodulename-factory-options) API for more information). -- Static ES6 module imports can only appear at the top of the file, so instead we have to import them dynamically using `import()`. +- Static ES6 module imports are hoisted to the top of the file, so instead we have to import them dynamically using `import()`. - Finally, we need an environment which supports dynamic importing. Please see [Using Babel](GettingStarted.md#using-babel) for the initial setup. Then add the plugin [babel-plugin-dynamic-import-node](https://www.npmjs.com/package/babel-plugin-dynamic-import-node), or an equivalent, to your Babel config to enable dynamic importing in Node. ```js diff --git a/website/versioned_docs/version-22.x/JestObjectAPI.md b/website/versioned_docs/version-22.x/JestObjectAPI.md index 5bf6cd1ab04d..492f49c6eb11 100644 --- a/website/versioned_docs/version-22.x/JestObjectAPI.md +++ b/website/versioned_docs/version-22.x/JestObjectAPI.md @@ -371,7 +371,7 @@ test('moduleName 2', () => { Using `jest.doMock()` with ES6 modules requires additional steps: - We have to specify the `__esModule: true` property (see the [`jest.mock()`](#jestmockmodulename-factory-options) API for more information). -- Static ES6 module imports can only appear at the top of the file, so instead we have to import them dynamically using `import()`. +- Static ES6 module imports are hoisted to the top of the file, so instead we have to import them dynamically using `import()`. - Finally, we need an environment which supports dynamic importing. Please see [Using Babel](GettingStarted.md#using-babel) for the initial setup. Then add the plugin [babel-plugin-dynamic-import-node](https://www.npmjs.com/package/babel-plugin-dynamic-import-node), or an equivalent, to your Babel config to enable dynamic importing in Node. ```js diff --git a/website/versioned_docs/version-23.x/JestObjectAPI.md b/website/versioned_docs/version-23.x/JestObjectAPI.md index ecf76d36fd85..a72e9adae9fd 100644 --- a/website/versioned_docs/version-23.x/JestObjectAPI.md +++ b/website/versioned_docs/version-23.x/JestObjectAPI.md @@ -372,7 +372,7 @@ test('moduleName 2', () => { Using `jest.doMock()` with ES6 modules requires additional steps: - We have to specify the `__esModule: true` property (see the [`jest.mock()`](#jestmockmodulename-factory-options) API for more information). -- Static ES6 module imports can only appear at the top of the file, so instead we have to import them dynamically using `import()`. +- Static ES6 module imports are hoisted to the top of the file, so instead we have to import them dynamically using `import()`. - Finally, we need an environment which supports dynamic importing. Please see [Using Babel](GettingStarted.md#using-babel) for the initial setup. Then add the plugin [babel-plugin-dynamic-import-node](https://www.npmjs.com/package/babel-plugin-dynamic-import-node), or an equivalent, to your Babel config to enable dynamic importing in Node. ```js diff --git a/website/versioned_docs/version-24.0/JestObjectAPI.md b/website/versioned_docs/version-24.0/JestObjectAPI.md index d8bdd8764e48..83a652afc3af 100644 --- a/website/versioned_docs/version-24.0/JestObjectAPI.md +++ b/website/versioned_docs/version-24.0/JestObjectAPI.md @@ -318,7 +318,7 @@ test('moduleName 2', () => { Using `jest.doMock()` with ES6 modules requires additional steps: - We have to specify the `__esModule: true` property (see the [`jest.mock()`](#jestmockmodulename-factory-options) API for more information). -- Static ES6 module imports can only appear at the top of the file, so instead we have to import them dynamically using `import()`. +- Static ES6 module imports are hoisted to the top of the file, so instead we have to import them dynamically using `import()`. - Finally, we need an environment which supports dynamic importing. Please see [Using Babel](GettingStarted.md#using-babel) for the initial setup. Then add the plugin [babel-plugin-dynamic-import-node](https://www.npmjs.com/package/babel-plugin-dynamic-import-node), or an equivalent, to your Babel config to enable dynamic importing in Node. ```js From 10d961fa573086e276d36f987e540d24d4b3f15e Mon Sep 17 00:00:00 2001 From: Spyros Argalias Date: Tue, 18 Jun 2019 17:57:30 +0100 Subject: [PATCH 4/5] docs(jest.doMock): Clarify statement about ES6 imports and `require` The steps outlined in jest.doMock for ES6 imports are only necessary if you don't want to use `require` in your tests. --- CHANGELOG.md | 2 +- docs/JestObjectAPI.md | 2 +- website/versioned_docs/version-22.x/JestObjectAPI.md | 2 +- website/versioned_docs/version-23.x/JestObjectAPI.md | 2 +- website/versioned_docs/version-24.0/JestObjectAPI.md | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a28e0540d4de..b4d31929be92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,7 @@ - `[jest-leak-detector]` remove code repeat ([#8438](https://github.com/facebook/jest/pull/8438) - `[docs]` Add example to `jest.requireActual` ([#8482](https://github.com/facebook/jest/pull/8482) - `[docs]` Add example to `jest.mock` for mocking ES6 modules with the `factory` parameter ([#8550](https://github.com/facebook/jest/pull/8550)) -- `[docs]` Add information about using `jest.doMock` with ES6 modules ([#8573](https://github.com/facebook/jest/pull/8573)) +- `[docs]` Add information about using `jest.doMock` with ES6 imports ([#8573](https://github.com/facebook/jest/pull/8573)) ### Performance diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index d329090a9090..38e6c3b9a283 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -314,7 +314,7 @@ test('moduleName 2', () => { }); ``` -Using `jest.doMock()` with ES6 modules requires additional steps: +Using `jest.doMock()` with ES6 imports requires additional steps. Follow these if you don't want to use `require` in your tests: - We have to specify the `__esModule: true` property (see the [`jest.mock()`](#jestmockmodulename-factory-options) API for more information). - Static ES6 module imports are hoisted to the top of the file, so instead we have to import them dynamically using `import()`. diff --git a/website/versioned_docs/version-22.x/JestObjectAPI.md b/website/versioned_docs/version-22.x/JestObjectAPI.md index 492f49c6eb11..1c3a6db84e9e 100644 --- a/website/versioned_docs/version-22.x/JestObjectAPI.md +++ b/website/versioned_docs/version-22.x/JestObjectAPI.md @@ -368,7 +368,7 @@ test('moduleName 2', () => { }); ``` -Using `jest.doMock()` with ES6 modules requires additional steps: +Using `jest.doMock()` with ES6 imports requires additional steps. Follow these if you don't want to use `require` in your tests: - We have to specify the `__esModule: true` property (see the [`jest.mock()`](#jestmockmodulename-factory-options) API for more information). - Static ES6 module imports are hoisted to the top of the file, so instead we have to import them dynamically using `import()`. diff --git a/website/versioned_docs/version-23.x/JestObjectAPI.md b/website/versioned_docs/version-23.x/JestObjectAPI.md index a72e9adae9fd..358191c8d254 100644 --- a/website/versioned_docs/version-23.x/JestObjectAPI.md +++ b/website/versioned_docs/version-23.x/JestObjectAPI.md @@ -369,7 +369,7 @@ test('moduleName 2', () => { }); ``` -Using `jest.doMock()` with ES6 modules requires additional steps: +Using `jest.doMock()` with ES6 imports requires additional steps. Follow these if you don't want to use `require` in your tests: - We have to specify the `__esModule: true` property (see the [`jest.mock()`](#jestmockmodulename-factory-options) API for more information). - Static ES6 module imports are hoisted to the top of the file, so instead we have to import them dynamically using `import()`. diff --git a/website/versioned_docs/version-24.0/JestObjectAPI.md b/website/versioned_docs/version-24.0/JestObjectAPI.md index 83a652afc3af..293421a47bc7 100644 --- a/website/versioned_docs/version-24.0/JestObjectAPI.md +++ b/website/versioned_docs/version-24.0/JestObjectAPI.md @@ -315,7 +315,7 @@ test('moduleName 2', () => { }); ``` -Using `jest.doMock()` with ES6 modules requires additional steps: +Using `jest.doMock()` with ES6 imports requires additional steps. Follow these if you don't want to use `require` in your tests: - We have to specify the `__esModule: true` property (see the [`jest.mock()`](#jestmockmodulename-factory-options) API for more information). - Static ES6 module imports are hoisted to the top of the file, so instead we have to import them dynamically using `import()`. From fcf22bf4004466904c9126bb319363683bd94bff Mon Sep 17 00:00:00 2001 From: Spyros Argalias Date: Tue, 18 Jun 2019 18:01:22 +0100 Subject: [PATCH 5/5] docs(jest.doMock): Return promise in example tests --- docs/JestObjectAPI.md | 4 ++-- website/versioned_docs/version-22.x/JestObjectAPI.md | 4 ++-- website/versioned_docs/version-23.x/JestObjectAPI.md | 4 ++-- website/versioned_docs/version-24.0/JestObjectAPI.md | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index 38e6c3b9a283..4929e7fe9760 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -333,7 +333,7 @@ test('moduleName 1', () => { foo: 'foo1', }; }); - import('../moduleName').then(moduleName => { + return import('../moduleName').then(moduleName => { expect(moduleName.default).toEqual('default1'); expect(moduleName.foo).toEqual('foo1'); }); @@ -347,7 +347,7 @@ test('moduleName 2', () => { foo: 'foo2', }; }); - import('../moduleName').then(moduleName => { + return import('../moduleName').then(moduleName => { expect(moduleName.default).toEqual('default2'); expect(moduleName.foo).toEqual('foo2'); }); diff --git a/website/versioned_docs/version-22.x/JestObjectAPI.md b/website/versioned_docs/version-22.x/JestObjectAPI.md index 1c3a6db84e9e..15a147f57ee8 100644 --- a/website/versioned_docs/version-22.x/JestObjectAPI.md +++ b/website/versioned_docs/version-22.x/JestObjectAPI.md @@ -387,7 +387,7 @@ test('moduleName 1', () => { foo: 'foo1', }; }); - import('../moduleName').then(moduleName => { + return import('../moduleName').then(moduleName => { expect(moduleName.default).toEqual('default1'); expect(moduleName.foo).toEqual('foo1'); }); @@ -401,7 +401,7 @@ test('moduleName 2', () => { foo: 'foo2', }; }); - import('../moduleName').then(moduleName => { + return import('../moduleName').then(moduleName => { expect(moduleName.default).toEqual('default2'); expect(moduleName.foo).toEqual('foo2'); }); diff --git a/website/versioned_docs/version-23.x/JestObjectAPI.md b/website/versioned_docs/version-23.x/JestObjectAPI.md index 358191c8d254..61ae53d9a81f 100644 --- a/website/versioned_docs/version-23.x/JestObjectAPI.md +++ b/website/versioned_docs/version-23.x/JestObjectAPI.md @@ -388,7 +388,7 @@ test('moduleName 1', () => { foo: 'foo1', }; }); - import('../moduleName').then(moduleName => { + return import('../moduleName').then(moduleName => { expect(moduleName.default).toEqual('default1'); expect(moduleName.foo).toEqual('foo1'); }); @@ -402,7 +402,7 @@ test('moduleName 2', () => { foo: 'foo2', }; }); - import('../moduleName').then(moduleName => { + return import('../moduleName').then(moduleName => { expect(moduleName.default).toEqual('default2'); expect(moduleName.foo).toEqual('foo2'); }); diff --git a/website/versioned_docs/version-24.0/JestObjectAPI.md b/website/versioned_docs/version-24.0/JestObjectAPI.md index 293421a47bc7..3607f338e84d 100644 --- a/website/versioned_docs/version-24.0/JestObjectAPI.md +++ b/website/versioned_docs/version-24.0/JestObjectAPI.md @@ -334,7 +334,7 @@ test('moduleName 1', () => { foo: 'foo1', }; }); - import('../moduleName').then(moduleName => { + return import('../moduleName').then(moduleName => { expect(moduleName.default).toEqual('default1'); expect(moduleName.foo).toEqual('foo1'); }); @@ -348,7 +348,7 @@ test('moduleName 2', () => { foo: 'foo2', }; }); - import('../moduleName').then(moduleName => { + return import('../moduleName').then(moduleName => { expect(moduleName.default).toEqual('default2'); expect(moduleName.foo).toEqual('foo2'); });