Skip to content

Commit

Permalink
fix(reporters): make sure to handle empty files in v8 coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Nov 14, 2020
1 parent b7365b3 commit a862e92
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 6 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
- `[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
11 changes: 10 additions & 1 deletion e2e/__tests__/__snapshots__/v8Coverage.test.ts.snap
@@ -1,6 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`prints coverage 1`] = `
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
Expand Down
15 changes: 14 additions & 1 deletion e2e/__tests__/v8Coverage.test.ts
Expand Up @@ -10,7 +10,7 @@ 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 @@ -22,3 +22,16 @@ test('prints coverage', () => {
expect(exitCode).toBe(0);
expect(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(stdout).toMatchSnapshot();
});
3 changes: 3 additions & 0 deletions e2e/v8-coverage/empty-sourcemap/babel.config.js
@@ -0,0 +1,3 @@
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"
}
}
5 changes: 5 additions & 0 deletions e2e/v8-coverage/empty-sourcemap/test.ts
@@ -0,0 +1,5 @@
import * as types from './types';

test('dummy-test', () => {
expect(types).toEqual({default: {}});
});
2 changes: 2 additions & 0 deletions e2e/v8-coverage/empty-sourcemap/types.ts
@@ -0,0 +1,2 @@
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 a862e92

Please sign in to comment.