mirror of https://github.com/facebook/jest.git
feat(environment-jsdom): allow passing html content to jsdom environment (#11950)
This commit is contained in:
parent
ae1f04bf0a
commit
7f39f0a589
|
@ -2,6 +2,8 @@
|
|||
|
||||
### Features
|
||||
|
||||
- `[jest-config]` Add `testEnvironmentOptions.html` to apply to jsdom input ([11950](https://github.com/facebook/jest/pull/11950))
|
||||
|
||||
### Fixes
|
||||
|
||||
- `[expect]` Tweak and improve types ([#11949](https://github.com/facebook/jest/pull/11949))
|
||||
|
|
|
@ -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: "<html lang="zh-cmn-Hant"></html>", userAgent: "Agent/007"}`.
|
||||
|
||||
### `testFailureExitCode` \[number]
|
||||
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
/**
|
||||
* 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();
|
||||
});
|
|
@ -0,0 +1,19 @@
|
|||
/**
|
||||
* 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: [
|
||||
[
|
||||
'@babel/env',
|
||||
{
|
||||
targets: {
|
||||
node: 'current',
|
||||
},
|
||||
},
|
||||
],
|
||||
],
|
||||
};
|
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"dependencies": {
|
||||
"@babel/core": "^7.2.2",
|
||||
"@babel/preset-env": "^7.2.2"
|
||||
},
|
||||
"jest": {
|
||||
"testEnvironment": "jsdom",
|
||||
"testEnvironmentOptions": {
|
||||
"html": "<div id=\"root\"></div>"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -31,19 +31,26 @@ class JSDOMEnvironment implements JestEnvironment<number> {
|
|||
moduleMocker: ModuleMocker | null;
|
||||
|
||||
constructor(config: Config.ProjectConfig, options?: EnvironmentContext) {
|
||||
this.dom = new JSDOM('<!DOCTYPE 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
|
||||
: '<!DOCTYPE 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);
|
||||
|
||||
|
|
Loading…
Reference in New Issue