Skip to content

Commit

Permalink
Fix stack trace for errors thrown by snapshot serializers (#4787)
Browse files Browse the repository at this point in the history
* Fix stack trace for errors thrown by snapshot serializers

In this commit, we are creating a new custom error called
`PrettyFormatPluginError`, this error is thrown by `prettyFormat`
function when a serializer throws an error.

In the `expect` module, we skip `Error.captureStackTrace` if the error
name is `PrettyFormatPluginError`, this way the stack trace stays
intact.

Fixes #3302

* Update changelog

* Make sure new tests have assertions

* Update CHANGELOG.md

* Update index.js
  • Loading branch information
nicolasiensen authored and cpojer committed Oct 30, 2017
1 parent 27228e8 commit cc802fb
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* `[pretty-format]` Prevent error in pretty-format for window in jsdom test env ([#4750](https://github.com/facebook/jest/pull/4750))
* `[jest-resolve]` Preserve module identity for symlinks ([#4761](https://github.com/facebook/jest/pull/4761))
* `[jest-config]` Include error message for `preset` json ([#4766](https://github.com/facebook/jest/pull/4766))
* `[pretty-format]` Throw `PrettyFormatPluginError` if a plugin halts with an exception ([#4787](https://github.com/facebook/jest/pull/4787))
* `[expect]` Keep the stack trace unchanged when `PrettyFormatPluginError` is thrown by pretty-format ([#4787](https://github.com/facebook/jest/pull/4787))

### Features
* `[jest-environment-jsdom]` [**BREAKING**] Upgrade to JSDOM@11 ([#4770](https://github.com/facebook/jest/pull/4770))
Expand Down
12 changes: 7 additions & 5 deletions packages/expect/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,14 @@ const makeThrowingMatcher = (
try {
result = matcher.apply(matcherContext, [actual].concat(args));
} catch (error) {
if (!(error instanceof JestAssertionError)) {
// Try to remove this and deeper functions from the stack trace frame.
if (
!(error instanceof JestAssertionError) &&
error.name !== 'PrettyFormatPluginError' &&
// Guard for some environments (browsers) that do not support this feature.
if (Error.captureStackTrace) {
Error.captureStackTrace(error, throwingMatcher);
}
Error.captureStackTrace
) {
// Try to remove this and deeper functions from the stack trace frame.
Error.captureStackTrace(error, throwingMatcher);
}
throw error;
}
Expand Down
60 changes: 60 additions & 0 deletions packages/pretty-format/src/__tests__/pretty_format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,66 @@ describe('prettyFormat()', () => {
}).toThrow();
});

it('throws PrettyFormatPluginError if test throws an error', () => {
expect.hasAssertions();
const options = {
plugins: [
{
print: () => '',
test() {
throw new Error('Where is the error?');
},
},
],
};

try {
prettyFormat('', options);
} catch (error) {
expect(error.name).toBe('PrettyFormatPluginError');
}
});

it('throws PrettyFormatPluginError if print throws an error', () => {
expect.hasAssertions();
const options = {
plugins: [
{
print: () => {
throw new Error('Where is the error?');
},
test: () => true,
},
],
};

try {
prettyFormat('', options);
} catch (error) {
expect(error.name).toBe('PrettyFormatPluginError');
}
});

it('throws PrettyFormatPluginError if serialize throws an error', () => {
expect.hasAssertions();
const options = {
plugins: [
{
serialize: () => {
throw new Error('Where is the error?');
},
test: () => true,
},
],
};

try {
prettyFormat('', options);
} catch (error) {
expect(error.name).toBe('PrettyFormatPluginError');
}
});

it('supports plugins with deeply nested arrays (#24)', () => {
const val = [[1, 2], [3, 4]];
expect(
Expand Down
60 changes: 39 additions & 21 deletions packages/pretty-format/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ const isWindow = val => typeof window !== 'undefined' && val === window;
const SYMBOL_REGEXP = /^Symbol\((.*)\)(.*)$/;
const NEWLINE_REGEXP = /\n/gi;

class PrettyFormatPluginError extends Error {
constructor(message, stack) {
super(message);
this.stack = stack;
this.name = this.constructor.name;
}
}

function isToStringedArrayType(toStringed: string): boolean {
return (
toStringed === '[object Array]' ||
Expand Down Expand Up @@ -246,25 +254,31 @@ function printPlugin(
depth: number,
refs: Refs,
): string {
const printed = plugin.serialize
? plugin.serialize(val, config, indentation, depth, refs, printer)
: plugin.print(
val,
valChild => printer(valChild, config, indentation, depth, refs),
str => {
const indentationNext = indentation + config.indent;
return (
indentationNext +
str.replace(NEWLINE_REGEXP, '\n' + indentationNext)
);
},
{
edgeSpacing: config.spacingOuter,
min: config.min,
spacing: config.spacingInner,
},
config.colors,
);
let printed;

try {
printed = plugin.serialize
? plugin.serialize(val, config, indentation, depth, refs, printer)
: plugin.print(
val,
valChild => printer(valChild, config, indentation, depth, refs),
str => {
const indentationNext = indentation + config.indent;
return (
indentationNext +
str.replace(NEWLINE_REGEXP, '\n' + indentationNext)
);
},
{
edgeSpacing: config.spacingOuter,
min: config.min,
spacing: config.spacingInner,
},
config.colors,
);
} catch (error) {
throw new PrettyFormatPluginError(error.message, error.stack);
}
if (typeof printed !== 'string') {
throw new Error(
`pretty-format: Plugin must return type "string" but instead returned "${typeof printed}".`,
Expand All @@ -275,8 +289,12 @@ function printPlugin(

function findPlugin(plugins: Plugins, val: any) {
for (let p = 0; p < plugins.length; p++) {
if (plugins[p].test(val)) {
return plugins[p];
try {
if (plugins[p].test(val)) {
return plugins[p];
}
} catch (error) {
throw new PrettyFormatPluginError(error.message, error.stack);
}
}

Expand Down

0 comments on commit cc802fb

Please sign in to comment.