Skip to content

Commit 2ff56ce

Browse files
novemberbornsindresorhus
authored andcommitted
Don't call toJSON() when formatting values
Fixes #1233. Our assertions don't call toJSON(), so we shouldn't use it either when displaying actual and expected values.
1 parent 795f097 commit 2ff56ce

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/serialize-error.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const extractStack = require('./extract-stack');
99

1010
function serializeValue(value) {
1111
return prettyFormat(value, {
12+
callToJSON: false,
1213
plugins: [reactTestPlugin],
1314
highlight: true
1415
});

test/serialize-error.js

+27
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ sourceMapSupport.install({environment: 'node'});
1717

1818
function serializeValue(value) {
1919
return prettyFormat(value, {
20+
callToJSON: false,
2021
plugins: [reactTestPlugin],
2122
highlight: true
2223
});
@@ -183,3 +184,29 @@ test('skip actual and expected if output is off', t => {
183184
t.notOk(serializedErr.expectedType);
184185
t.end();
185186
});
187+
188+
test('does not call toJSON() when serializing actual and expected', t => {
189+
const err = Object.assign(new Error(), {
190+
showOutput: true,
191+
actual: {
192+
foo: 'bar',
193+
toJSON() {
194+
return {
195+
foo: 'BAR'
196+
};
197+
}
198+
},
199+
expected: {
200+
foo: 'thud',
201+
toJSON() {
202+
return {
203+
foo: 'BAR'
204+
};
205+
}
206+
}
207+
});
208+
209+
const serializedErr = serialize(err);
210+
t.notSame(serializedErr.actual, serializedErr.expected);
211+
t.end();
212+
});

0 commit comments

Comments
 (0)