Skip to content

Commit c830517

Browse files
authored
feat: Make console.error and warn log a stack trace and codeframe like thrown errors do (jestjs#9741)
1 parent f215902 commit c830517

23 files changed

+373
-103
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Features
44

5+
- `[jest-console]` Add code frame to `console.error` and `console.warn` ([#9741](https://github.com/facebook/jest/pull/9741))
56
- `[@jest/globals]` New package so Jest's globals can be explicitly imported ([#9801](https://github.com/facebook/jest/pull/9801))
67

78
### Fixes
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`Correct BeforeAll run ensures the BeforeAll of ignored suite is not run 1`] = `
4-
console.log __tests__/beforeAllFiltered.test.js:10
4+
console.log
55
beforeAll 1
66
7-
console.log __tests__/beforeAllFiltered.test.js:13
7+
at log (__tests__/beforeAllFiltered.test.js:10:13)
8+
9+
console.log
810
beforeEach 1
911
10-
console.log __tests__/beforeAllFiltered.test.js:22
12+
at log (__tests__/beforeAllFiltered.test.js:13:13)
13+
14+
console.log
1115
It Foo
1216
13-
console.log __tests__/beforeAllFiltered.test.js:16
17+
at log (__tests__/beforeAllFiltered.test.js:22:13)
18+
19+
console.log
1420
afterEach 1
1521
16-
console.log __tests__/beforeAllFiltered.test.js:19
22+
at log (__tests__/beforeAllFiltered.test.js:16:13)
23+
24+
console.log
1725
afterAll 1
1826
27+
at log (__tests__/beforeAllFiltered.test.js:19:13)
28+
1929
`;
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`Correct beforeEach order ensures the correct order for beforeEach 1`] = `
4-
console.log __tests__/beforeEachQueue.test.js:10
4+
console.log
55
BeforeEach
66
7-
console.log __tests__/beforeEachQueue.test.js:14
7+
at Object.log (__tests__/beforeEachQueue.test.js:10:13)
8+
9+
console.log
810
It Foo
911
10-
console.log __tests__/beforeEachQueue.test.js:17
12+
at Object.log (__tests__/beforeEachQueue.test.js:14:13)
13+
14+
console.log
1115
BeforeEach Inline Foo
1216
13-
console.log __tests__/beforeEachQueue.test.js:10
17+
at Object.log (__tests__/beforeEachQueue.test.js:17:15)
18+
19+
console.log
1420
BeforeEach
1521
16-
console.log __tests__/beforeEachQueue.test.js:22
22+
at Object.log (__tests__/beforeEachQueue.test.js:10:13)
23+
24+
console.log
1725
It Bar
1826
27+
at Object.log (__tests__/beforeEachQueue.test.js:22:13)
28+
1929
`;

e2e/__tests__/__snapshots__/console.test.ts.snap

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,40 @@ exports[`console printing 1`] = `
44
PASS __tests__/console.test.js
55
● Console
66
7-
console.log __tests__/console.test.js:10
7+
console.log
88
This is a log message.
9-
console.info __tests__/console.test.js:12
9+
10+
at Object.log (__tests__/console.test.js:10:11)
11+
12+
console.info
1013
This is an info message.
11-
console.warn __tests__/console.test.js:14
14+
15+
at Object.info (__tests__/console.test.js:12:11)
16+
17+
console.warn
1218
This is a warning message.
13-
console.error __tests__/console.test.js:16
19+
20+
12 | console.info('This is an info message.');
21+
13 |
22+
> 14 | console.warn('This is a warning message.');
23+
| ^
24+
15 |
25+
16 | console.error('This is an error message.');
26+
17 | });
27+
28+
at Object.warn (__tests__/console.test.js:14:11)
29+
30+
console.error
1431
This is an error message.
32+
33+
14 | console.warn('This is a warning message.');
34+
15 |
35+
> 16 | console.error('This is an error message.');
36+
| ^
37+
17 | });
38+
18 |
39+
40+
at Object.error (__tests__/console.test.js:16:11)
1541
`;
1642

1743
exports[`console printing 2`] = `
@@ -23,18 +49,41 @@ Ran all test suites.
2349
`;
2450
2551
exports[`console printing with --verbose 1`] = `
26-
console.log __tests__/console.test.js:10
52+
console.log
2753
This is a log message.
2854
29-
console.info __tests__/console.test.js:12
55+
at Object.log (__tests__/console.test.js:10:11)
56+
57+
console.info
3058
This is an info message.
3159
32-
console.warn __tests__/console.test.js:14
60+
at Object.info (__tests__/console.test.js:12:11)
61+
62+
console.warn
3363
This is a warning message.
3464
35-
console.error __tests__/console.test.js:16
65+
12 | console.info('This is an info message.');
66+
13 |
67+
> 14 | console.warn('This is a warning message.');
68+
| ^
69+
15 |
70+
16 | console.error('This is an error message.');
71+
17 | });
72+
73+
at Object.warn (__tests__/console.test.js:14:11)
74+
75+
console.error
3676
This is an error message.
3777
78+
14 | console.warn('This is a warning message.');
79+
15 |
80+
> 16 | console.error('This is an error message.');
81+
| ^
82+
17 | });
83+
18 |
84+
85+
at Object.error (__tests__/console.test.js:16:11)
86+
3887
`;
3988
4089
exports[`console printing with --verbose 2`] = `
@@ -56,17 +105,25 @@ exports[`does not error out when using winston 2`] = `
56105
PASS __tests__/console.test.js
57106
● Console
58107
59-
console.log node_modules/winston/lib/winston/transports/console.js:79
108+
console.log
60109
{"level":"info","message":"Log message from winston"}
61-
62-
console.log node_modules/winston/lib/winston/transports/console.js:79
110+
111+
at Console.log (node_modules/winston/lib/winston/transports/console.js:79:23)
112+
113+
console.log
63114
{"message":"Info message from winston","level":"info"}
64-
65-
console.log node_modules/winston/lib/winston/transports/console.js:79
115+
116+
at Console.log (node_modules/winston/lib/winston/transports/console.js:79:23)
117+
118+
console.log
66119
{"message":"Warn message from winston","level":"warn"}
67-
68-
console.log node_modules/winston/lib/winston/transports/console.js:79
120+
121+
at Console.log (node_modules/winston/lib/winston/transports/console.js:79:23)
122+
123+
console.log
69124
{"message":"Error message from winston","level":"error"}
125+
126+
at Console.log (node_modules/winston/lib/winston/transports/console.js:79:23)
70127
`;
71128
72129
exports[`does not error out when using winston 3`] = `

e2e/__tests__/__snapshots__/consoleAfterTeardown.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ PASS __tests__/console.test.js
1515
14 | });
1616
15 |
1717
18-
at BufferedConsole.log (../../packages/jest-console/build/BufferedConsole.js:199:10)
18+
at BufferedConsole.log (../../packages/jest-console/build/BufferedConsole.js:201:10)
1919
at log (__tests__/console.test.js:12:13)
2020
`;

e2e/__tests__/__snapshots__/consoleLogOutputWhenRunInBand.test.ts.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ Ran all test suites.
1717
`;
1818
1919
exports[`prints console.logs when run with forceExit 3`] = `
20-
console.log __tests__/a-banana.js:1
20+
console.log
2121
Hey
2222
23+
at Object.<anonymous> (__tests__/a-banana.js:1:1)
24+
2325
`;

e2e/__tests__/__snapshots__/declarationErrors.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exports[`warns if describe returns a Promise 1`] = `
1616
14 | });
1717
1818
at Object.describe (__tests__/describeReturnPromise.test.js:11:1)
19-
19+
2020
2121
`;
2222

@@ -36,6 +36,6 @@ exports[`warns if describe returns something 1`] = `
3636
14 | });
3737
3838
at Object.describe (__tests__/describeReturnSomething.test.js:11:1)
39-
39+
4040
4141
`;

e2e/__tests__/__snapshots__/jest.config.js.test.ts.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`traverses directory tree up until it finds jest.config 1`] = `
4-
console.log ../../../__tests__/a-banana.js:3
4+
console.log
55
<<REPLACED>>/jest.config.js/some/nested/directory
66
7+
at Object.log (__tests__/a-banana.js:3:27)
8+
79
`;
810
911
exports[`traverses directory tree up until it finds jest.config 2`] = `
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`on node >=10 prints coverage 1`] = `
4+
" console.log
5+
42
6+
7+
at Object.log (__tests__/Thing.test.js:10:9)
8+
9+
----------|---------|----------|---------|---------|-------------------
10+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
11+
----------|---------|----------|---------|---------|-------------------
12+
All files | 100 | 100 | 100 | 100 |
13+
Thing.js | 100 | 100 | 100 | 100 |
14+
x.css | 100 | 100 | 100 | 100 |
15+
----------|---------|----------|---------|---------|-------------------"
16+
`;

e2e/__tests__/beforeAllFiltered.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import runJest from '../runJest';
1010

1111
describe('Correct BeforeAll run', () => {
1212
it('ensures the BeforeAll of ignored suite is not run', () => {
13-
const result = runJest('before-all-filtered');
14-
expect(wrap(result.stdout.replace(/\\/g, '/'))).toMatchSnapshot();
13+
let {stdout} = runJest('before-all-filtered');
14+
15+
// for some reason Circus does not have the `Object` part
16+
stdout = stdout.replace(/at Object.log \(/g, 'at log (');
17+
18+
expect(wrap(stdout)).toMatchSnapshot();
1519
});
1620
});

0 commit comments

Comments
 (0)