Skip to content

Commit

Permalink
feat: Make console.error and warn log a stack trace and codeframe lik…
Browse files Browse the repository at this point in the history
…e thrown errors do (#9741)
  • Loading branch information
ychi committed Apr 12, 2020
1 parent f215902 commit c830517
Show file tree
Hide file tree
Showing 23 changed files with 373 additions and 103 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

### Features

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

### Fixes
Expand Down
20 changes: 15 additions & 5 deletions e2e/__tests__/__snapshots__/beforeAllFiltered.ts.snap
@@ -1,19 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Correct BeforeAll run ensures the BeforeAll of ignored suite is not run 1`] = `
console.log __tests__/beforeAllFiltered.test.js:10
console.log
beforeAll 1
console.log __tests__/beforeAllFiltered.test.js:13
at log (__tests__/beforeAllFiltered.test.js:10:13)
console.log
beforeEach 1
console.log __tests__/beforeAllFiltered.test.js:22
at log (__tests__/beforeAllFiltered.test.js:13:13)
console.log
It Foo
console.log __tests__/beforeAllFiltered.test.js:16
at log (__tests__/beforeAllFiltered.test.js:22:13)
console.log
afterEach 1
console.log __tests__/beforeAllFiltered.test.js:19
at log (__tests__/beforeAllFiltered.test.js:16:13)
console.log
afterAll 1
at log (__tests__/beforeAllFiltered.test.js:19:13)
`;
20 changes: 15 additions & 5 deletions e2e/__tests__/__snapshots__/beforeEachQueue.ts.snap
@@ -1,19 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Correct beforeEach order ensures the correct order for beforeEach 1`] = `
console.log __tests__/beforeEachQueue.test.js:10
console.log
BeforeEach
console.log __tests__/beforeEachQueue.test.js:14
at Object.log (__tests__/beforeEachQueue.test.js:10:13)
console.log
It Foo
console.log __tests__/beforeEachQueue.test.js:17
at Object.log (__tests__/beforeEachQueue.test.js:14:13)
console.log
BeforeEach Inline Foo
console.log __tests__/beforeEachQueue.test.js:10
at Object.log (__tests__/beforeEachQueue.test.js:17:15)
console.log
BeforeEach
console.log __tests__/beforeEachQueue.test.js:22
at Object.log (__tests__/beforeEachQueue.test.js:10:13)
console.log
It Bar
at Object.log (__tests__/beforeEachQueue.test.js:22:13)
`;
87 changes: 72 additions & 15 deletions e2e/__tests__/__snapshots__/console.test.ts.snap
Expand Up @@ -4,14 +4,40 @@ exports[`console printing 1`] = `
PASS __tests__/console.test.js
● Console
console.log __tests__/console.test.js:10
console.log
This is a log message.
console.info __tests__/console.test.js:12
at Object.log (__tests__/console.test.js:10:11)
console.info
This is an info message.
console.warn __tests__/console.test.js:14
at Object.info (__tests__/console.test.js:12:11)
console.warn
This is a warning message.
console.error __tests__/console.test.js:16
12 | console.info('This is an info message.');
13 |
> 14 | console.warn('This is a warning message.');
| ^
15 |
16 | console.error('This is an error message.');
17 | });
at Object.warn (__tests__/console.test.js:14:11)
console.error
This is an error message.
14 | console.warn('This is a warning message.');
15 |
> 16 | console.error('This is an error message.');
| ^
17 | });
18 |
at Object.error (__tests__/console.test.js:16:11)
`;

exports[`console printing 2`] = `
Expand All @@ -23,18 +49,41 @@ Ran all test suites.
`;
exports[`console printing with --verbose 1`] = `
console.log __tests__/console.test.js:10
console.log
This is a log message.
console.info __tests__/console.test.js:12
at Object.log (__tests__/console.test.js:10:11)
console.info
This is an info message.
console.warn __tests__/console.test.js:14
at Object.info (__tests__/console.test.js:12:11)
console.warn
This is a warning message.
console.error __tests__/console.test.js:16
12 | console.info('This is an info message.');
13 |
> 14 | console.warn('This is a warning message.');
| ^
15 |
16 | console.error('This is an error message.');
17 | });
at Object.warn (__tests__/console.test.js:14:11)
console.error
This is an error message.
14 | console.warn('This is a warning message.');
15 |
> 16 | console.error('This is an error message.');
| ^
17 | });
18 |
at Object.error (__tests__/console.test.js:16:11)
`;
exports[`console printing with --verbose 2`] = `
Expand All @@ -56,17 +105,25 @@ exports[`does not error out when using winston 2`] = `
PASS __tests__/console.test.js
● Console
console.log node_modules/winston/lib/winston/transports/console.js:79
console.log
{"level":"info","message":"Log message from winston"}
console.log node_modules/winston/lib/winston/transports/console.js:79
at Console.log (node_modules/winston/lib/winston/transports/console.js:79:23)
console.log
{"message":"Info message from winston","level":"info"}
console.log node_modules/winston/lib/winston/transports/console.js:79
at Console.log (node_modules/winston/lib/winston/transports/console.js:79:23)
console.log
{"message":"Warn message from winston","level":"warn"}
console.log node_modules/winston/lib/winston/transports/console.js:79
at Console.log (node_modules/winston/lib/winston/transports/console.js:79:23)
console.log
{"message":"Error message from winston","level":"error"}
at Console.log (node_modules/winston/lib/winston/transports/console.js:79:23)
`;
exports[`does not error out when using winston 3`] = `
Expand Down
Expand Up @@ -15,6 +15,6 @@ PASS __tests__/console.test.js
14 | });
15 |
at BufferedConsole.log (../../packages/jest-console/build/BufferedConsole.js:199:10)
at BufferedConsole.log (../../packages/jest-console/build/BufferedConsole.js:201:10)
at log (__tests__/console.test.js:12:13)
`;
Expand Up @@ -17,7 +17,9 @@ Ran all test suites.
`;
exports[`prints console.logs when run with forceExit 3`] = `
console.log __tests__/a-banana.js:1
console.log
Hey
at Object.<anonymous> (__tests__/a-banana.js:1:1)
`;
4 changes: 2 additions & 2 deletions e2e/__tests__/__snapshots__/declarationErrors.test.ts.snap
Expand Up @@ -16,7 +16,7 @@ exports[`warns if describe returns a Promise 1`] = `
14 | });
at Object.describe (__tests__/describeReturnPromise.test.js:11:1)
`;

Expand All @@ -36,6 +36,6 @@ exports[`warns if describe returns something 1`] = `
14 | });
at Object.describe (__tests__/describeReturnSomething.test.js:11:1)
`;
4 changes: 3 additions & 1 deletion e2e/__tests__/__snapshots__/jest.config.js.test.ts.snap
@@ -1,9 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`traverses directory tree up until it finds jest.config 1`] = `
console.log ../../../__tests__/a-banana.js:3
console.log
<<REPLACED>>/jest.config.js/some/nested/directory
at Object.log (__tests__/a-banana.js:3:27)
`;
exports[`traverses directory tree up until it finds jest.config 2`] = `
Expand Down
16 changes: 16 additions & 0 deletions e2e/__tests__/__snapshots__/v8Coverage.test.ts.snap
@@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`on node >=10 prints coverage 1`] = `
" console.log
42
at Object.log (__tests__/Thing.test.js:10:9)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
Thing.js | 100 | 100 | 100 | 100 |
x.css | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------"
`;
8 changes: 6 additions & 2 deletions e2e/__tests__/beforeAllFiltered.ts
Expand Up @@ -10,7 +10,11 @@ import runJest from '../runJest';

describe('Correct BeforeAll run', () => {
it('ensures the BeforeAll of ignored suite is not run', () => {
const result = runJest('before-all-filtered');
expect(wrap(result.stdout.replace(/\\/g, '/'))).toMatchSnapshot();
let {stdout} = runJest('before-all-filtered');

// for some reason Circus does not have the `Object` part
stdout = stdout.replace(/at Object.log \(/g, 'at log (');

expect(wrap(stdout)).toMatchSnapshot();
});
});
18 changes: 17 additions & 1 deletion e2e/__tests__/consoleLogOutputWhenRunInBand.test.ts
Expand Up @@ -15,6 +15,8 @@ const DIR = path.resolve(__dirname, '../console-log-output-when-run-in-band');
beforeEach(() => cleanup(DIR));
afterAll(() => cleanup(DIR));

const nodeMajorVersion = Number(process.versions.node.split('.')[0]);

test('prints console.logs when run with forceExit', () => {
writeFiles(DIR, {
'__tests__/a-banana.js': `
Expand All @@ -23,12 +25,26 @@ test('prints console.logs when run with forceExit', () => {
'package.json': '{}',
});

const {stderr, stdout, exitCode} = runJest(DIR, [
const {stderr, exitCode, ...res} = runJest(DIR, [
'-i',
'--ci=false',
'--forceExit',
]);
let {stdout} = res;

const {rest, summary} = extractSummary(stderr);

if (nodeMajorVersion < 12) {
expect(stdout).toContain(
'at Object.<anonymous>.test (__tests__/a-banana.js:1:1)',
);

stdout = stdout.replace(
'at Object.<anonymous>.test (__tests__/a-banana.js:1:1)',
'at Object.<anonymous> (__tests__/a-banana.js:1:1)',
);
}

expect(exitCode).toBe(0);
expect(wrap(rest)).toMatchSnapshot();
expect(wrap(summary)).toMatchSnapshot();
Expand Down
3 changes: 2 additions & 1 deletion e2e/__tests__/declarationErrors.test.ts
Expand Up @@ -12,7 +12,8 @@ const normalizeCircusJasmine = (str: string) =>
wrap(
str
.replace(/console\.log .+:\d+/, 'console.log')
.replace(/.+addSpecsToSuite (.+:\d+:\d+).+\n/g, ''),
.replace(/.+addSpecsToSuite (.+:\d+:\d+).+\n/g, '')
.replace(/.+_dispatchDescribe (.+:\d+:\d+).+\n/g, ''),
);

it('warns if describe returns a Promise', () => {
Expand Down
4 changes: 3 additions & 1 deletion e2e/__tests__/resolveBrowserField.test.ts
Expand Up @@ -63,8 +63,10 @@ test('preserves module identity for symlinks when using browser field resolution
const {stdout, stderr, exitCode} = runJest(DIR, ['--no-watchman']);
expect(stderr).toContain('Test Suites: 1 passed, 1 total');
expect(wrap(stdout.trim())).toMatchInlineSnapshot(`
console.log packages/needs-preserved-id/index.js:1
console.log
needs-preserved-id executed
at Object.<anonymous> (packages/needs-preserved-id/index.js:1:13)
`);
expect(exitCode).toEqual(0);
});
21 changes: 2 additions & 19 deletions e2e/__tests__/v8Coverage.test.ts
Expand Up @@ -14,6 +14,7 @@ const DIR = path.resolve(__dirname, '../v8-coverage');
onNodeVersions('>=10', () => {
test('prints coverage', () => {
const sourcemapDir = path.join(DIR, 'no-sourcemap');

const {stdout, exitCode} = runJest(
sourcemapDir,
['--coverage', '--coverage-provider', 'v8'],
Expand All @@ -23,24 +24,6 @@ onNodeVersions('>=10', () => {
);

expect(exitCode).toBe(0);
expect(
'\n' +
stdout
.split('\n')
.map(s => s.trimRight())
.join('\n') +
'\n',
).toEqual(`
console.log __tests__/Thing.test.js:10
42
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
Thing.js | 100 | 100 | 100 | 100 |
x.css | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
`);
expect(stdout).toMatchSnapshot();
});
});
3 changes: 2 additions & 1 deletion packages/jest-console/package.json
Expand Up @@ -17,8 +17,9 @@
}
},
"dependencies": {
"@jest/source-map": "^25.2.6",
"@jest/types": "^25.3.0",
"chalk": "^3.0.0",
"jest-message-util": "^25.3.0",
"jest-util": "^25.3.0",
"slash": "^3.0.0"
},
Expand Down

0 comments on commit c830517

Please sign in to comment.