Skip to content

Commit

Permalink
Don't call toJSON() when formatting values
Browse files Browse the repository at this point in the history
Fixes #1233. Our assertions don't call toJSON(), so we shouldn't use
it either when displaying actual and expected values.
  • Loading branch information
novemberborn authored and sindresorhus committed Feb 18, 2017
1 parent 795f097 commit 2ff56ce
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/serialize-error.js
Expand Up @@ -9,6 +9,7 @@ const extractStack = require('./extract-stack');

function serializeValue(value) {
return prettyFormat(value, {
callToJSON: false,
plugins: [reactTestPlugin],
highlight: true
});
Expand Down
27 changes: 27 additions & 0 deletions test/serialize-error.js
Expand Up @@ -17,6 +17,7 @@ sourceMapSupport.install({environment: 'node'});

function serializeValue(value) {
return prettyFormat(value, {
callToJSON: false,
plugins: [reactTestPlugin],
highlight: true
});
Expand Down Expand Up @@ -183,3 +184,29 @@ test('skip actual and expected if output is off', t => {
t.notOk(serializedErr.expectedType);
t.end();
});

test('does not call toJSON() when serializing actual and expected', t => {
const err = Object.assign(new Error(), {
showOutput: true,
actual: {
foo: 'bar',
toJSON() {
return {
foo: 'BAR'
};
}
},
expected: {
foo: 'thud',
toJSON() {
return {
foo: 'BAR'
};
}
}
});

const serializedErr = serialize(err);
t.notSame(serializedErr.actual, serializedErr.expected);
t.end();
});

0 comments on commit 2ff56ce

Please sign in to comment.