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

fix(reporters): make sure to handle empty files in v8 coverage #10819

Merged
merged 1 commit into from Nov 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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