Skip to content

Commit

Permalink
fix(reporters): make sure to handle empty files in v8 coverage (#10819)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Nov 14, 2020
1 parent b7365b3 commit 3a30c9d
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@

- `[jest-console]` `console.dir` now respects the second argument correctly ([#10638](https://github.com/facebook/jest/pull/10638))
- `[expect]` [**BREAKING**] Revise `expect.not.objectContaining()` to be the inverse of `expect.objectContaining()`, as documented. ([#10708](https://github.com/facebook/jest/pull/10708))
- `[jest-reporter]` Handle empty files when reporting code coverage with V8 ([#10819](https://github.com/facebook/jest/pull/10819))
- `[jest-resolve]` Replace read-pkg-up with escalade package ([#10781](https://github.com/facebook/jest/pull/10781))
- `[jest-runtime]` [**BREAKING**] Do not inject `global` variable into module wrapper ([#10644](https://github.com/facebook/jest/pull/10644))
- `[jest-runtime]` [**BREAKING**] remove long-deprecated `jest.addMatchers`, `jest.resetModuleRegistry`, and `jest.runTimersToTime` ([#9853](https://github.com/facebook/jest/pull/9853))
Expand Down
15 changes: 12 additions & 3 deletions e2e/__tests__/__snapshots__/v8Coverage.test.ts.snap
@@ -1,7 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`prints coverage 1`] = `
" console.log
exports[`prints coverage with empty sourcemaps 1`] = `
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
types.ts | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
`;

exports[`prints coverage with missing sourcemaps 1`] = `
console.log
42
at Object.log (__tests__/Thing.test.js:10:9)
Expand All @@ -12,5 +21,5 @@ 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 |
----------|---------|----------|---------|---------|-------------------"
----------|---------|----------|---------|---------|-------------------
`;
18 changes: 16 additions & 2 deletions e2e/__tests__/v8Coverage.test.ts
Expand Up @@ -6,11 +6,12 @@
*/

import * as path from 'path';
import wrap from 'jest-snapshot-serializer-raw';
import runJest from '../runJest';

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

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

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

expect(exitCode).toBe(0);
expect(stdout).toMatchSnapshot();
expect(wrap(stdout)).toMatchSnapshot();
});

test('prints coverage with empty sourcemaps', () => {
const sourcemapDir = path.join(DIR, 'empty-sourcemap');

const {stdout, exitCode} = runJest(
sourcemapDir,
['--coverage', '--coverage-provider', 'v8'],
{stripAnsi: true},
);

expect(exitCode).toBe(0);
expect(wrap(stdout)).toMatchSnapshot();
});
10 changes: 10 additions & 0 deletions e2e/v8-coverage/empty-sourcemap/babel.config.js
@@ -0,0 +1,10 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = {
presets: ['@babel/preset-env', '@babel/preset-typescript'],
};
7 changes: 7 additions & 0 deletions e2e/v8-coverage/empty-sourcemap/package.json
@@ -0,0 +1,7 @@
{
"name": "empty-sourcemap",
"version": "1.0.0",
"jest": {
"testEnvironment": "node"
}
}
12 changes: 12 additions & 0 deletions e2e/v8-coverage/empty-sourcemap/test.ts
@@ -0,0 +1,12 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import * as types from './types';

test('dummy-test', () => {
expect(types).toEqual({default: {}});
});
8 changes: 8 additions & 0 deletions e2e/v8-coverage/empty-sourcemap/types.ts
@@ -0,0 +1,8 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

export interface obj {}
9 changes: 5 additions & 4 deletions packages/jest-reporters/src/CoverageReporter.ts
Expand Up @@ -37,7 +37,7 @@ import type {
// This is fixed in a newer versions of source-map, but our dependencies are still stuck on old versions
interface FixedRawSourceMap extends Omit<RawSourceMap, 'version'> {
version: number;
file: string;
file?: string;
}

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

if (
fileTransform &&
fileTransform.sourceMapPath &&
fileTransform?.sourceMapPath &&
fs.existsSync(fileTransform.sourceMapPath)
) {
sourcemapContent = JSON.parse(
Expand All @@ -458,7 +457,9 @@ export default class CoverageReporter extends BaseReporter {
? {
originalSource: fileTransform.originalCode,
source: fileTransform.code,
sourceMap: {sourcemap: sourcemapContent},
sourceMap: {
sourcemap: {file: res.url, ...sourcemapContent},
},
}
: {source: fs.readFileSync(res.url, 'utf8')},
);
Expand Down

0 comments on commit 3a30c9d

Please sign in to comment.