Skip to content

Commit 8c5e870

Browse files
pedrottimarkcpojer
authored andcommitted
Prevent error in pretty-format for window in jsdom test env (#4750)
* Prevent error in pretty-format for window in jsdom test env * Delete global * Save file after correcting comments :( * Edit comments after perf measurements * add to Fixes in changelog * Simplify with typeof window as suggested by SimenB
1 parent 688fba7 commit 8c5e870

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* `[jest-util]` Fix `runOnlyPendingTimers` for `setTimeout` inside `setImmediate` ([#4608](https://github.com/facebook/jest/pull/4608))
1515
* `[jest-message-util]` Always remove node internals from stacktraces ([#4695](https://github.com/facebook/jest/pull/4695))
1616
* `[jest-resolve]` changes method of determining builtin modules to include missing builtins ([#4740](https://github.com/facebook/jest/pull/4740))
17+
* `[pretty-format]` Prevent error in pretty-format for window in jsdom test env ([#4750](https://github.com/facebook/jest/pull/4750))
1718

1819
### Features
1920
* `[jest-environment-*]` [**BREAKING**] Add Async Test Environment APIs, dispose is now teardown ([#4506](https://github.com/facebook/jest/pull/4506))

packages/pretty-format/src/__tests__/dom_element.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ const toPrettyPrintTo = require('./expect_util').getPrettyPrint([DOMElement]);
1818
const expect: any = global.expect;
1919
expect.extend({toPrettyPrintTo});
2020

21+
describe('pretty-format', () => {
22+
// Test is not related to plugin but is related to jsdom testing environment.
23+
it('prints global window as constructor name alone', () => {
24+
expect(prettyFormat(window)).toEqual('[Window]');
25+
});
26+
});
27+
2128
describe('DOMElement Plugin', () => {
2229
it('supports a single HTML element', () => {
2330
expect(document.createElement('div')).toPrettyPrintTo('<div />');

packages/pretty-format/src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ const errorToString = Error.prototype.toString;
4141
const regExpToString = RegExp.prototype.toString;
4242
const symbolToString = Symbol.prototype.toString;
4343

44+
// Is val is equal to global window object? Works even if it does not exist :)
45+
/* global window */
46+
const isWindow = val => typeof window !== 'undefined' && val === window;
47+
4448
const SYMBOL_REGEXP = /^Symbol\((.*)\)(.*)$/;
4549
const NEWLINE_REGEXP = /\n/gi;
4650

@@ -224,7 +228,9 @@ function printComplexValue(
224228
'}';
225229
}
226230

227-
return hitMaxDepth
231+
// Avoid failure to serialize global window object in jsdom test environment.
232+
// For example, not even relevant if window is prop of React element.
233+
return hitMaxDepth || isWindow(val)
228234
? '[' + (val.constructor ? val.constructor.name : 'Object') + ']'
229235
: (min ? '' : (val.constructor ? val.constructor.name : 'Object') + ' ') +
230236
'{' +

0 commit comments

Comments
 (0)