Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stack trace for errors thrown by snapshot serializers #4787

Merged
merged 5 commits into from
Oct 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
}
Copy link
Contributor

@pedrottimark pedrottimark Oct 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If for any reason the prettyFormat call doesn’t throw, then the test passes?

What do you think about: DISREGARD FOLLOWING CODE

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either do expect().toThrow() or expect.hasAssertions instead

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SimenB Thank you! Because toThrow matches the message but the name is the test criterion, then our requested change is keep try-catch as originally written and insert expect.hasAssertions() at beginning of each of the 3 test blocks?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. Or .toThrow(PrettyFormatPluginError) if it's exported.

});

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