Skip to content

Commit

Permalink
Prevent error in pretty-format for window in jsdom test env (#4750)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pedrottimark authored and cpojer committed Oct 24, 2017
1 parent 688fba7 commit 8c5e870
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* `[jest-util]` Fix `runOnlyPendingTimers` for `setTimeout` inside `setImmediate` ([#4608](https://github.com/facebook/jest/pull/4608))
* `[jest-message-util]` Always remove node internals from stacktraces ([#4695](https://github.com/facebook/jest/pull/4695))
* `[jest-resolve]` changes method of determining builtin modules to include missing builtins ([#4740](https://github.com/facebook/jest/pull/4740))
* `[pretty-format]` Prevent error in pretty-format for window in jsdom test env ([#4750](https://github.com/facebook/jest/pull/4750))

### Features
* `[jest-environment-*]` [**BREAKING**] Add Async Test Environment APIs, dispose is now teardown ([#4506](https://github.com/facebook/jest/pull/4506))
Expand Down
7 changes: 7 additions & 0 deletions packages/pretty-format/src/__tests__/dom_element.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ const toPrettyPrintTo = require('./expect_util').getPrettyPrint([DOMElement]);
const expect: any = global.expect;
expect.extend({toPrettyPrintTo});

describe('pretty-format', () => {
// Test is not related to plugin but is related to jsdom testing environment.
it('prints global window as constructor name alone', () => {
expect(prettyFormat(window)).toEqual('[Window]');
});
});

describe('DOMElement Plugin', () => {
it('supports a single HTML element', () => {
expect(document.createElement('div')).toPrettyPrintTo('<div />');
Expand Down
8 changes: 7 additions & 1 deletion packages/pretty-format/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ const errorToString = Error.prototype.toString;
const regExpToString = RegExp.prototype.toString;
const symbolToString = Symbol.prototype.toString;

// Is val is equal to global window object? Works even if it does not exist :)
/* global window */
const isWindow = val => typeof window !== 'undefined' && val === window;

const SYMBOL_REGEXP = /^Symbol\((.*)\)(.*)$/;
const NEWLINE_REGEXP = /\n/gi;

Expand Down Expand Up @@ -224,7 +228,9 @@ function printComplexValue(
'}';
}

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

0 comments on commit 8c5e870

Please sign in to comment.