Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Make console.error and warn log a stack trace and codeframe like thrown errors do #9741

Merged
merged 31 commits into from Apr 12, 2020
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
8d1e059
Put full stack trace in console buffer
ychi Apr 1, 2020
d9390b6
Rely on source-map-support
ychi Apr 4, 2020
1ed4e36
Incorporate review feedbacks
ychi Apr 8, 2020
6e153d1
Update packages/jest-console/src/getConsoleOutput.ts
ychi Apr 9, 2020
1b1e713
Ignore unused parameter (sourceMaps)
ychi Apr 9, 2020
51258fb
Remove redundant newline
ychi Apr 9, 2020
d15996c
More review feedbacks
ychi Apr 9, 2020
881da4d
Remove unused sourcemap
ychi Apr 9, 2020
d33b5c3
lint
ychi Apr 9, 2020
23b34cf
update snapshots
ychi Apr 9, 2020
0fbf49b
correct package name & bump dep version
ychi Apr 10, 2020
fd6a4f1
Review feedbacks
ychi Apr 10, 2020
211bd7c
fix declarationErrors normalisation
ychi Apr 10, 2020
a9d18b5
fix v8 coverage test
ychi Apr 10, 2020
16efc8a
Review feedbacks
ychi Apr 11, 2020
3fbcd89
chore: consisten newlines
SimenB Apr 11, 2020
bad2aea
Merge branch 'master' into stack-for-error-n-warn
SimenB Apr 11, 2020
afd68b6
update snaps
SimenB Apr 11, 2020
fb0854e
tweak newlines in between messages
SimenB Apr 11, 2020
34901a3
fix circus test
SimenB Apr 11, 2020
3a8908d
fix test diff between node versions
SimenB Apr 11, 2020
58cd2f6
another circus fix
SimenB Apr 11, 2020
fb47353
use snapshot
SimenB Apr 11, 2020
21ee253
runtime can be const now
SimenB Apr 11, 2020
1e99400
use default param
SimenB Apr 11, 2020
8b6b76d
changelog
SimenB Apr 11, 2020
3d9d47d
unit tests jest-message-util
SimenB Apr 12, 2020
fae0133
maybe consistent snap?
SimenB Apr 12, 2020
3ac517f
Merge branch 'master' into stack-for-error-n-warn
SimenB Apr 12, 2020
c4386d5
eslint is hard
SimenB Apr 12, 2020
5d3a8e9
is this consistent?
SimenB Apr 12, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

### Features

- `[jest-console]` Add code frame to `console.error` and `console.warn` ([#9741](https://github.com/facebook/jest/pull/9741))

### Fixes

- `[expect]` Restore support for passing functions to `toHaveLength` matcher ([#9796](https://github.com/facebook/jest/pull/9796))
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
SimenB marked this conversation as resolved.
Show resolved Hide resolved
{"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