Skip to content

Commit 3a30c9d

Browse files
authored
fix(reporters): make sure to handle empty files in v8 coverage (#10819)
1 parent b7365b3 commit 3a30c9d

8 files changed

Lines changed: 71 additions & 9 deletions

File tree

CHANGELOG.md

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

1010
- `[jest-console]` `console.dir` now respects the second argument correctly ([#10638](https://github.com/facebook/jest/pull/10638))
1111
- `[expect]` [**BREAKING**] Revise `expect.not.objectContaining()` to be the inverse of `expect.objectContaining()`, as documented. ([#10708](https://github.com/facebook/jest/pull/10708))
12+
- `[jest-reporter]` Handle empty files when reporting code coverage with V8 ([#10819](https://github.com/facebook/jest/pull/10819))
1213
- `[jest-resolve]` Replace read-pkg-up with escalade package ([#10781](https://github.com/facebook/jest/pull/10781))
1314
- `[jest-runtime]` [**BREAKING**] Do not inject `global` variable into module wrapper ([#10644](https://github.com/facebook/jest/pull/10644))
1415
- `[jest-runtime]` [**BREAKING**] remove long-deprecated `jest.addMatchers`, `jest.resetModuleRegistry`, and `jest.runTimersToTime` ([#9853](https://github.com/facebook/jest/pull/9853))
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`prints coverage 1`] = `
4-
" console.log
3+
exports[`prints coverage with empty sourcemaps 1`] = `
4+
----------|---------|----------|---------|---------|-------------------
5+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
6+
----------|---------|----------|---------|---------|-------------------
7+
All files | 100 | 100 | 100 | 100 |
8+
types.ts | 100 | 100 | 100 | 100 |
9+
----------|---------|----------|---------|---------|-------------------
10+
`;
11+
12+
exports[`prints coverage with missing sourcemaps 1`] = `
13+
console.log
514
42
615
716
at Object.log (__tests__/Thing.test.js:10:9)
@@ -12,5 +21,5 @@ File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
1221
All files | 100 | 100 | 100 | 100 |
1322
Thing.js | 100 | 100 | 100 | 100 |
1423
x.css | 100 | 100 | 100 | 100 |
15-
----------|---------|----------|---------|---------|-------------------"
24+
----------|---------|----------|---------|---------|-------------------
1625
`;

e2e/__tests__/v8Coverage.test.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
*/
77

88
import * as path from 'path';
9+
import wrap from 'jest-snapshot-serializer-raw';
910
import runJest from '../runJest';
1011

1112
const DIR = path.resolve(__dirname, '../v8-coverage');
1213

13-
test('prints coverage', () => {
14+
test('prints coverage with missing sourcemaps', () => {
1415
const sourcemapDir = path.join(DIR, 'no-sourcemap');
1516

1617
const {stdout, exitCode} = runJest(
@@ -20,5 +21,18 @@ test('prints coverage', () => {
2021
);
2122

2223
expect(exitCode).toBe(0);
23-
expect(stdout).toMatchSnapshot();
24+
expect(wrap(stdout)).toMatchSnapshot();
25+
});
26+
27+
test('prints coverage with empty sourcemaps', () => {
28+
const sourcemapDir = path.join(DIR, 'empty-sourcemap');
29+
30+
const {stdout, exitCode} = runJest(
31+
sourcemapDir,
32+
['--coverage', '--coverage-provider', 'v8'],
33+
{stripAnsi: true},
34+
);
35+
36+
expect(exitCode).toBe(0);
37+
expect(wrap(stdout)).toMatchSnapshot();
2438
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
module.exports = {
9+
presets: ['@babel/preset-env', '@babel/preset-typescript'],
10+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "empty-sourcemap",
3+
"version": "1.0.0",
4+
"jest": {
5+
"testEnvironment": "node"
6+
}
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import * as types from './types';
9+
10+
test('dummy-test', () => {
11+
expect(types).toEqual({default: {}});
12+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export interface obj {}

packages/jest-reporters/src/CoverageReporter.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import type {
3737
// This is fixed in a newer versions of source-map, but our dependencies are still stuck on old versions
3838
interface FixedRawSourceMap extends Omit<RawSourceMap, 'version'> {
3939
version: number;
40-
file: string;
40+
file?: string;
4141
}
4242

4343
const FAIL_COLOR = chalk.bold.red;
@@ -442,8 +442,7 @@ export default class CoverageReporter extends BaseReporter {
442442
let sourcemapContent: FixedRawSourceMap | undefined = undefined;
443443

444444
if (
445-
fileTransform &&
446-
fileTransform.sourceMapPath &&
445+
fileTransform?.sourceMapPath &&
447446
fs.existsSync(fileTransform.sourceMapPath)
448447
) {
449448
sourcemapContent = JSON.parse(
@@ -458,7 +457,9 @@ export default class CoverageReporter extends BaseReporter {
458457
? {
459458
originalSource: fileTransform.originalCode,
460459
source: fileTransform.code,
461-
sourceMap: {sourcemap: sourcemapContent},
460+
sourceMap: {
461+
sourcemap: {file: res.url, ...sourcemapContent},
462+
},
462463
}
463464
: {source: fs.readFileSync(res.url, 'utf8')},
464465
);

0 commit comments

Comments
 (0)