Skip to content

Commit d472868

Browse files
SimenBCodyRay
andauthored
fix: log JSDOM errors more cleanly (#12386)
Co-authored-by: Cody Ray Hoeft <[email protected]>
1 parent 0c7ec75 commit d472868

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
- `[jest-config]` Pass `moduleTypes` to `ts-node` to enforce CJS when transpiling ([#12397](https://github.com/facebook/jest/pull/12397))
3636
- `[jest-config, jest-haste-map]` Allow searching for tests in `node_modules` by exposing `retainAllFiles` ([#11084](https://github.com/facebook/jest/pull/11084))
3737
- `[jest-environment-jsdom]` Make `jsdom` accessible to extending environments again ([#12232](https://github.com/facebook/jest/pull/12232))
38+
- `[jest-environment-jsdom]` Log JSDOM errors more cleanly ([#12386](https://github.com/facebook/jest/pull/12386))
3839
- `[@jest/expect-utils]` [**BREAKING**] Fix false positives when looking for `undefined` prop ([#8923](https://github.com/facebook/jest/pull/8923))
3940
- `[jest-haste-map]` Don't use partial results if file crawl errors ([#12420](https://github.com/facebook/jest/pull/12420))
4041
- `[jest-jasmine2, jest-types]` [**BREAKING**] Move all `jasmine` specific types from `@jest/types` to its own package ([#12125](https://github.com/facebook/jest/pull/12125))

e2e/console-jsdom/__tests__/console.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,11 @@ test('can mock console.error calls from jsdom', () => {
3737
window.removeEventListener('error', onError);
3838

3939
expect(console.error).toHaveBeenCalledTimes(1);
40+
expect(console.error).toHaveBeenCalledWith(
41+
expect.objectContaining({
42+
detail: expect.objectContaining({
43+
message: 'this is an error in an event callback',
44+
}),
45+
}),
46+
);
4047
});

packages/jest-environment-jsdom/src/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
3636

3737
constructor(config: JestEnvironmentConfig, context: EnvironmentContext) {
3838
const {projectConfig} = config;
39+
40+
const virtualConsole = new VirtualConsole();
41+
virtualConsole.sendTo(context.console, {omitJSDOMErrors: true});
42+
virtualConsole.on('jsdomError', error => {
43+
context.console.error(error);
44+
});
45+
3946
this.dom = new JSDOM(
4047
typeof projectConfig.testEnvironmentOptions.html === 'string'
4148
? projectConfig.testEnvironmentOptions.html
@@ -50,7 +57,7 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
5057
: undefined,
5158
runScripts: 'dangerously',
5259
url: 'http://localhost/',
53-
virtualConsole: new VirtualConsole().sendTo(context.console),
60+
virtualConsole,
5461
...projectConfig.testEnvironmentOptions,
5562
},
5663
);

0 commit comments

Comments
 (0)