From 4b9f45b38b55f157e0e41217bebd97419f829f1b Mon Sep 17 00:00:00 2001 From: MarvelSQ Date: Mon, 11 Oct 2021 16:30:37 +0800 Subject: [PATCH 1/7] feat(environment-jsdom): allow passing html content to jsdom environment --- packages/jest-environment-jsdom/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-environment-jsdom/src/index.ts b/packages/jest-environment-jsdom/src/index.ts index 39b63efc4607..6564d9e675fe 100644 --- a/packages/jest-environment-jsdom/src/index.ts +++ b/packages/jest-environment-jsdom/src/index.ts @@ -31,7 +31,7 @@ class JSDOMEnvironment implements JestEnvironment { moduleMocker: ModuleMocker | null; constructor(config: Config.ProjectConfig, options?: EnvironmentContext) { - this.dom = new JSDOM('', { + this.dom = new JSDOM(typeof config.testEnvironmentOptions.html === 'string' ? config.testEnvironmentOptions.html : '', { pretendToBeVisual: true, resources: typeof config.testEnvironmentOptions.userAgent === 'string' From ae8958b7fe3bd43152e0481f71a32da8237d3b81 Mon Sep 17 00:00:00 2001 From: MarvelSQ Date: Tue, 12 Oct 2021 17:42:20 +0800 Subject: [PATCH 2/7] test(environment-jsdom): add test case --- e2e/custom-jsdom-html/__tests__/test.js | 3 +++ e2e/custom-jsdom-html/babel.config.js | 5 +++++ e2e/custom-jsdom-html/package.json | 13 +++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 e2e/custom-jsdom-html/__tests__/test.js create mode 100644 e2e/custom-jsdom-html/babel.config.js create mode 100644 e2e/custom-jsdom-html/package.json diff --git a/e2e/custom-jsdom-html/__tests__/test.js b/e2e/custom-jsdom-html/__tests__/test.js new file mode 100644 index 000000000000..f5a0d65e9260 --- /dev/null +++ b/e2e/custom-jsdom-html/__tests__/test.js @@ -0,0 +1,3 @@ +test('jsdom custom html', async () => { + expect(document.getElementById('root')).toBeTruthy(); +}); \ No newline at end of file diff --git a/e2e/custom-jsdom-html/babel.config.js b/e2e/custom-jsdom-html/babel.config.js new file mode 100644 index 000000000000..f6d9955fd649 --- /dev/null +++ b/e2e/custom-jsdom-html/babel.config.js @@ -0,0 +1,5 @@ +module.exports = { + preset: [['@babel/env', { + targets: 'current' + }]] +} \ No newline at end of file diff --git a/e2e/custom-jsdom-html/package.json b/e2e/custom-jsdom-html/package.json new file mode 100644 index 000000000000..607216fc198d --- /dev/null +++ b/e2e/custom-jsdom-html/package.json @@ -0,0 +1,13 @@ +{ + "dependencies": { + "@babel/core": "^7.2.2", + "@babel/preset-env": "^7.2.2" + }, + "jest": { + "testEnvironment": "jsdom", + "testEnvironmentOptions": { + "html": "
" + } + } + } + \ No newline at end of file From 65b439b0d8aedc7cf08decc2e49a13da0d3f7a47 Mon Sep 17 00:00:00 2001 From: MarvelSQ Date: Tue, 12 Oct 2021 10:38:05 +0000 Subject: [PATCH 3/7] fix(custom-jsdom-html): fix style & babel config --- e2e/custom-jsdom-html/__tests__/test.js | 5 +-- e2e/custom-jsdom-html/babel.config.js | 15 ++++++--- packages/jest-environment-jsdom/src/index.ts | 33 ++++++++++++-------- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/e2e/custom-jsdom-html/__tests__/test.js b/e2e/custom-jsdom-html/__tests__/test.js index f5a0d65e9260..d67fc71c3335 100644 --- a/e2e/custom-jsdom-html/__tests__/test.js +++ b/e2e/custom-jsdom-html/__tests__/test.js @@ -1,3 +1,4 @@ -test('jsdom custom html', async () => { +test('jsdom custom html', () => { + /* eslint-disable-next-line no-undef */ expect(document.getElementById('root')).toBeTruthy(); -}); \ No newline at end of file +}); diff --git a/e2e/custom-jsdom-html/babel.config.js b/e2e/custom-jsdom-html/babel.config.js index f6d9955fd649..1ac8f765542a 100644 --- a/e2e/custom-jsdom-html/babel.config.js +++ b/e2e/custom-jsdom-html/babel.config.js @@ -1,5 +1,12 @@ module.exports = { - preset: [['@babel/env', { - targets: 'current' - }]] -} \ No newline at end of file + presets: [ + [ + '@babel/env', + { + targets: { + node: 'current', + }, + }, + ], + ], +}; diff --git a/packages/jest-environment-jsdom/src/index.ts b/packages/jest-environment-jsdom/src/index.ts index 6564d9e675fe..8db58a76a36e 100644 --- a/packages/jest-environment-jsdom/src/index.ts +++ b/packages/jest-environment-jsdom/src/index.ts @@ -31,19 +31,26 @@ class JSDOMEnvironment implements JestEnvironment { moduleMocker: ModuleMocker | null; constructor(config: Config.ProjectConfig, options?: EnvironmentContext) { - this.dom = new JSDOM(typeof config.testEnvironmentOptions.html === 'string' ? config.testEnvironmentOptions.html : '', { - pretendToBeVisual: true, - resources: - typeof config.testEnvironmentOptions.userAgent === 'string' - ? new ResourceLoader({ - userAgent: config.testEnvironmentOptions.userAgent, - }) - : undefined, - runScripts: 'dangerously', - url: config.testURL, - virtualConsole: new VirtualConsole().sendTo(options?.console || console), - ...config.testEnvironmentOptions, - }); + this.dom = new JSDOM( + typeof config.testEnvironmentOptions.html === 'string' + ? config.testEnvironmentOptions.html + : '', + { + pretendToBeVisual: true, + resources: + typeof config.testEnvironmentOptions.userAgent === 'string' + ? new ResourceLoader({ + userAgent: config.testEnvironmentOptions.userAgent, + }) + : undefined, + runScripts: 'dangerously', + url: config.testURL, + virtualConsole: new VirtualConsole().sendTo( + options?.console || console, + ), + ...config.testEnvironmentOptions, + }, + ); const global = (this.global = this.dom.window.document .defaultView as unknown as Win); From 4b301117dd98bb003c6b1ec42703404ced8ab304 Mon Sep 17 00:00:00 2001 From: MarvelSQ Date: Tue, 12 Oct 2021 10:56:05 +0000 Subject: [PATCH 4/7] docs(custom-jsdom-html): add html to Configuration --- docs/Configuration.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Configuration.md b/docs/Configuration.md index 5b051f3ff450..56131b06edfb 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -1170,7 +1170,7 @@ beforeAll(() => { Default: `{}` -Test environment options that will be passed to the `testEnvironment`. The relevant options depend on the environment. For example, you can override options given to [jsdom](https://github.com/jsdom/jsdom) such as `{userAgent: "Agent/007"}`. +Test environment options that will be passed to the `testEnvironment`. The relevant options depend on the environment. For example, you can override options given to [jsdom](https://github.com/jsdom/jsdom) such as `{html: "", userAgent: "Agent/007"}`. ### `testFailureExitCode` \[number] From 6103b5578bdd7ed904f7a8d115a7adb8bc1c99ec Mon Sep 17 00:00:00 2001 From: MarvelSQ Date: Tue, 12 Oct 2021 11:02:53 +0000 Subject: [PATCH 5/7] docs(custom-jsdom-html): update CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b706156bc4b3..8ccc89295006 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ ## main ### Features - +- `[jest-config]` Add `testEnvironmentOptions.html` to apply to jsdom input ([11950](https://github.com/facebook/jest/pull/11950)) ### Fixes - `[jest-runtime]` Ensure absolute paths can be resolved within test modules ([11943](https://github.com/facebook/jest/pull/11943)) From 144a9d5c639ded5bf53102c298a28c2863621141 Mon Sep 17 00:00:00 2001 From: MarvelSQ Date: Fri, 15 Oct 2021 14:52:53 +0000 Subject: [PATCH 6/7] fix(custom-jsdom-html): fix formatting --- CHANGELOG.md | 2 ++ e2e/custom-jsdom-html/package.json | 19 +++++++++---------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ccc89295006..700505ffd8b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,9 @@ ## main ### Features + - `[jest-config]` Add `testEnvironmentOptions.html` to apply to jsdom input ([11950](https://github.com/facebook/jest/pull/11950)) + ### Fixes - `[jest-runtime]` Ensure absolute paths can be resolved within test modules ([11943](https://github.com/facebook/jest/pull/11943)) diff --git a/e2e/custom-jsdom-html/package.json b/e2e/custom-jsdom-html/package.json index 607216fc198d..ef3ddfb5b0c4 100644 --- a/e2e/custom-jsdom-html/package.json +++ b/e2e/custom-jsdom-html/package.json @@ -1,13 +1,12 @@ { - "dependencies": { - "@babel/core": "^7.2.2", - "@babel/preset-env": "^7.2.2" - }, - "jest": { - "testEnvironment": "jsdom", - "testEnvironmentOptions": { - "html": "
" - } + "dependencies": { + "@babel/core": "^7.2.2", + "@babel/preset-env": "^7.2.2" + }, + "jest": { + "testEnvironment": "jsdom", + "testEnvironmentOptions": { + "html": "
" } } - \ No newline at end of file +} From 7e7e5b4dfc0acc7e9782b2d7fcec7878c63ddcc0 Mon Sep 17 00:00:00 2001 From: MarvelSQ Date: Sat, 16 Oct 2021 06:13:03 +0000 Subject: [PATCH 7/7] fix(custom-jsdom-html): add Copyright Header --- e2e/custom-jsdom-html/__tests__/test.js | 7 +++++++ e2e/custom-jsdom-html/babel.config.js | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/e2e/custom-jsdom-html/__tests__/test.js b/e2e/custom-jsdom-html/__tests__/test.js index d67fc71c3335..5a14ac9299f3 100644 --- a/e2e/custom-jsdom-html/__tests__/test.js +++ b/e2e/custom-jsdom-html/__tests__/test.js @@ -1,3 +1,10 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + test('jsdom custom html', () => { /* eslint-disable-next-line no-undef */ expect(document.getElementById('root')).toBeTruthy(); diff --git a/e2e/custom-jsdom-html/babel.config.js b/e2e/custom-jsdom-html/babel.config.js index 1ac8f765542a..cea7d0f507a7 100644 --- a/e2e/custom-jsdom-html/babel.config.js +++ b/e2e/custom-jsdom-html/babel.config.js @@ -1,3 +1,10 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + module.exports = { presets: [ [