Skip to content

Commit 019041a

Browse files
committed
jest-jasmine2: pretty-print non-Error errors
1 parent 3919925 commit 019041a

File tree

6 files changed

+186
-55
lines changed

6 files changed

+186
-55
lines changed

integration-tests/__tests__/__snapshots__/failures.test.js.snap

Lines changed: 128 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,96 @@ exports[`not throwing Error objects 4`] = `
6868
"
6969
`;
7070
71+
exports[`not throwing Error objects 5`] = `
72+
"FAIL __tests__/during_tests.test.js
73+
✕ Promise thrown during test
74+
✕ Boolean thrown during test
75+
✕ undefined thrown during test
76+
✕ Object thrown during test
77+
✕ Error during test
78+
✕ done(Error)
79+
✕ done(non-error)
80+
81+
● Promise thrown during test
82+
83+
thrown: Promise {}
84+
85+
at packages/jest-jasmine2/build/expectation_result_factory.js:42:47
86+
87+
● Boolean thrown during test
88+
89+
thrown: false
90+
91+
at packages/jest-jasmine2/build/expectation_result_factory.js:42:47
92+
93+
● undefined thrown during test
94+
95+
thrown: undefined
96+
97+
at packages/jest-jasmine2/build/expectation_result_factory.js:42:47
98+
99+
● Object thrown during test
100+
101+
thrown: Object {
102+
\\"notAnError\\": Array [
103+
Object {
104+
\\"hello\\": true,
105+
\\"tooDeep\\": [Object],
106+
},
107+
],
108+
}
109+
110+
at packages/jest-jasmine2/build/expectation_result_factory.js:42:47
111+
112+
● Error during test
113+
114+
ReferenceError: doesNotExist is not defined
115+
116+
26 | test('Error during test', () => {
117+
27 | // eslint-disable-next-line no-undef
118+
> 28 | doesNotExist.alsoThisNot;
119+
29 | });
120+
30 |
121+
31 | test('done(Error)', done => {
122+
123+
at __tests__/during_tests.test.js:28:3
124+
125+
done(Error)
126+
127+
this is an error
128+
129+
30 |
130+
31 | test('done(Error)', done => {
131+
> 32 | done(new Error('this is an error'));
132+
33 | });
133+
34 |
134+
35 | test('done(non-error)', done => {
135+
136+
at __tests__/during_tests.test.js:32:8
137+
138+
done(non-error)
139+
140+
Failed: Object {
141+
\\"notAnError\\": Array [
142+
Object {
143+
\\"hello\\": true,
144+
\\"tooDeep\\": [Object],
145+
},
146+
],
147+
}
148+
149+
34 |
150+
35 | test('done(non-error)', done => {
151+
> 36 | done(deepObject);
152+
37 | });
153+
38 |
154+
155+
at packages/jest-jasmine2/build/expectation_result_factory.js:42:47
156+
at __tests__/during_tests.test.js:36:3
157+
158+
"
159+
`;
160+
71161
exports[`works with assertions in separate files 1`] = `
72162
"FAIL __tests__/test_macro.test.js
73163
✕ use some imported macro to make assertion
@@ -142,7 +232,7 @@ exports[`works with node assert 1`] = `
142232
143233
● assert
144234
145-
assert.equal(received, expected) or assert(received)
235+
thrown: \\"assert.equal(received, expected) or assert(received)
146236
147237
Expected value to be (operator: ==):
148238
true
@@ -160,7 +250,7 @@ exports[`works with node assert 1`] = `
160250
161251
assert with a message
162252
163-
assert.equal(received, expected) or assert(received)
253+
thrown: \\"assert.equal(received, expected) or assert(received)
164254
165255
Expected value to be (operator: ==):
166256
true
@@ -181,7 +271,7 @@ exports[`works with node assert 1`] = `
181271
182272
assert.ok
183273
184-
assert.equal(received, expected) or assert(received)
274+
thrown: \\"assert.equal(received, expected) or assert(received)
185275
186276
Expected value to be (operator: ==):
187277
true
@@ -199,7 +289,7 @@ exports[`works with node assert 1`] = `
199289
200290
assert.ok with a message
201291
202-
assert.equal(received, expected) or assert(received)
292+
thrown: \\"assert.equal(received, expected) or assert(received)
203293
204294
Expected value to be (operator: ==):
205295
true
@@ -220,7 +310,7 @@ exports[`works with node assert 1`] = `
220310
221311
assert.equal
222312
223-
assert.equal(received, expected) or assert(received)
313+
thrown: \\"assert.equal(received, expected) or assert(received)
224314
225315
Expected value to be (operator: ==):
226316
2
@@ -238,7 +328,7 @@ exports[`works with node assert 1`] = `
238328
239329
assert.notEqual
240330
241-
assert.notEqual(received, expected)
331+
thrown: \\"assert.notEqual(received, expected)
242332
243333
Expected value not to be (operator: !=):
244334
1
@@ -260,23 +350,23 @@ exports[`works with node assert 1`] = `
260350
261351
assert.deepEqual
262352
263-
assert.deepEqual(received, expected)
353+
thrown: \\"assert.deepEqual(received, expected)
264354
265355
Expected value to deeply equal to:
266-
{\\"a\\": {\\"b\\": {\\"c\\": 6}}}
356+
{\\\\\\"a\\\\\\": {\\\\\\"b\\\\\\": {\\\\\\"c\\\\\\": 6}}}
267357
Received:
268-
{\\"a\\": {\\"b\\": {\\"c\\": 5}}}
358+
{\\\\\\"a\\\\\\": {\\\\\\"b\\\\\\": {\\\\\\"c\\\\\\": 5}}}
269359
270360
Difference:
271361
272362
- Expected
273363
+ Received
274364
275365
Object {
276-
\\"a\\": Object {
277-
\\"b\\": Object {
278-
- \\"c\\": 6,
279-
+ \\"c\\": 5,
366+
\\\\\\"a\\\\\\": Object {
367+
\\\\\\"b\\\\\\": Object {
368+
- \\\\\\"c\\\\\\": 6,
369+
+ \\\\\\"c\\\\\\": 5,
280370
},
281371
},
282372
}
@@ -292,12 +382,12 @@ exports[`works with node assert 1`] = `
292382
293383
assert.deepEqual with a message
294384
295-
assert.deepEqual(received, expected)
385+
thrown: \\"assert.deepEqual(received, expected)
296386
297387
Expected value to deeply equal to:
298-
{\\"a\\": {\\"b\\": {\\"c\\": 7}}}
388+
{\\\\\\"a\\\\\\": {\\\\\\"b\\\\\\": {\\\\\\"c\\\\\\": 7}}}
299389
Received:
300-
{\\"a\\": {\\"b\\": {\\"c\\": 5}}}
390+
{\\\\\\"a\\\\\\": {\\\\\\"b\\\\\\": {\\\\\\"c\\\\\\": 5}}}
301391
302392
Message:
303393
this is a message
@@ -308,10 +398,10 @@ exports[`works with node assert 1`] = `
308398
+ Received
309399
310400
Object {
311-
\\"a\\": Object {
312-
\\"b\\": Object {
313-
- \\"c\\": 7,
314-
+ \\"c\\": 5,
401+
\\\\\\"a\\\\\\": Object {
402+
\\\\\\"b\\\\\\": Object {
403+
- \\\\\\"c\\\\\\": 7,
404+
+ \\\\\\"c\\\\\\": 5,
315405
},
316406
},
317407
}
@@ -327,12 +417,12 @@ exports[`works with node assert 1`] = `
327417
328418
assert.notDeepEqual
329419
330-
assert.notDeepEqual(received, expected)
420+
thrown: \\"assert.notDeepEqual(received, expected)
331421
332422
Expected value not to deeply equal to:
333-
{\\"a\\": 1}
423+
{\\\\\\"a\\\\\\": 1}
334424
Received:
335-
{\\"a\\": 1}
425+
{\\\\\\"a\\\\\\": 1}
336426
337427
Difference:
338428
@@ -349,7 +439,7 @@ exports[`works with node assert 1`] = `
349439
350440
assert.strictEqual
351441
352-
assert.strictEqual(received, expected)
442+
thrown: \\"assert.strictEqual(received, expected)
353443
354444
Expected value to be (operator: ===):
355445
NaN
@@ -367,7 +457,7 @@ exports[`works with node assert 1`] = `
367457
368458
assert.notStrictEqual
369459
370-
assert.notStrictEqual(received, expected)
460+
thrown: \\"assert.notStrictEqual(received, expected)
371461
372462
Expected value not to be (operator: !==):
373463
1
@@ -392,21 +482,21 @@ exports[`works with node assert 1`] = `
392482
393483
assert.deepStrictEqual
394484
395-
assert.deepStrictEqual(received, expected)
485+
thrown: \\"assert.deepStrictEqual(received, expected)
396486
397487
Expected value to deeply and strictly equal to:
398-
{\\"a\\": 2}
488+
{\\\\\\"a\\\\\\": 2}
399489
Received:
400-
{\\"a\\": 1}
490+
{\\\\\\"a\\\\\\": 1}
401491
402492
Difference:
403493
404494
- Expected
405495
+ Received
406496
407497
Object {
408-
- \\"a\\": 2,
409-
+ \\"a\\": 1,
498+
- \\\\\\"a\\\\\\": 2,
499+
+ \\\\\\"a\\\\\\": 1,
410500
}
411501
412502
57 |
@@ -420,12 +510,12 @@ exports[`works with node assert 1`] = `
420510
421511
assert.notDeepStrictEqual
422512
423-
assert.notDeepStrictEqual(received, expected)
513+
thrown: \\"assert.notDeepStrictEqual(received, expected)
424514
425515
Expected value not to deeply and strictly equal to:
426-
{\\"a\\": 1}
516+
{\\\\\\"a\\\\\\": 1}
427517
Received:
428-
{\\"a\\": 1}
518+
{\\\\\\"a\\\\\\": 1}
429519
430520
Difference:
431521
@@ -442,12 +532,13 @@ exports[`works with node assert 1`] = `
442532
443533
assert.ifError
444534
445-
Error
446-
1 thrown
535+
thrown: 1
536+
537+
at packages/jest-jasmine2/build/expectation_result_factory.js:42:47
447538
448539
assert.doesNotThrow
449540
450-
assert.doesNotThrow(function)
541+
thrown: \\"assert.doesNotThrow(function)
451542
452543
Expected the function not to throw an error.
453544
Instead, it threw:
@@ -467,7 +558,7 @@ exports[`works with node assert 1`] = `
467558
468559
assert.throws
469560
470-
assert.throws(function)
561+
thrown: \\"assert.throws(function)
471562
472563
Expected the function to throw an error.
473564
But it didn't throw anything.

integration-tests/__tests__/failures.test.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ test('not throwing Error objects', () => {
5252
expect(extractSummary(stderr).rest).toMatchSnapshot();
5353
stderr = runJest(dir, ['assertion_count.test.js']).stderr;
5454
expect(extractSummary(cleanupStackTrace(stderr)).rest).toMatchSnapshot();
55+
stderr = runJest(dir, ['during_tests.test.js']).stderr;
56+
expect(extractSummary(stderr).rest).toMatchSnapshot();
5557
});
5658

5759
test('works with node assert', () => {
@@ -63,23 +65,23 @@ test('works with node assert', () => {
6365
if (Number(process.versions.node.split('.')[0]) >= 9) {
6466
expect(summary).toContain(`
6567
assert.doesNotThrow(function)
66-
68+
6769
Expected the function not to throw an error.
6870
Instead, it threw:
6971
[Error: err!]
70-
72+
7173
Message:
7274
Got unwanted exception.
7375
err!
7476
err!
7577
76-
69 |
78+
69 |
7779
70 | test('assert.doesNotThrow', () => {
7880
> 71 | assert.doesNotThrow(() => {
7981
72 | throw Error('err!');
8082
73 | });
8183
74 | });
82-
84+
8385
at __tests__/node_assertion_error.test.js:71:10
8486
`);
8587

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
const deepObject = {
4+
notAnError: [{hello: true, tooDeep: {notVisible: true}}],
5+
};
6+
7+
test('Promise thrown during test', () => {
8+
throw Promise.resolve(5);
9+
});
10+
11+
test('Boolean thrown during test', () => {
12+
// eslint-disable-next-line no-throw-literal
13+
throw false;
14+
});
15+
16+
test('undefined thrown during test', () => {
17+
// eslint-disable-next-line no-throw-literal
18+
throw undefined;
19+
});
20+
21+
test('Object thrown during test', () => {
22+
// eslint-disable-next-line no-throw-literal
23+
throw deepObject;
24+
});
25+
26+
test('Error during test', () => {
27+
// eslint-disable-next-line no-undef
28+
doesNotExist.alsoThisNot;
29+
});
30+
31+
test('done(Error)', done => {
32+
done(new Error('this is an error'));
33+
});
34+
35+
test('done(non-error)', done => {
36+
done(deepObject);
37+
});

0 commit comments

Comments
 (0)