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 race condition with --coverage and babel-jest identical file contents edge case #4432

Merged
merged 4 commits into from
Sep 6, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`collects coverage from duplicate files avoiding shared cache 1`] = `
"---------------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
---------------|----------|----------|----------|----------|----------------|
All files | 100 | 100 | 100 | 100 | |
a | 100 | 100 | 100 | 100 | |
identical.js | 100 | 100 | 100 | 100 | |
b | 100 | 100 | 100 | 100 | |
identical.js | 100 | 100 | 100 | 100 | |
---------------|----------|----------|----------|----------|----------------|
"
`;

exports[`collects coverage only from specified files 1`] = `
"----------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
Expand Down Expand Up @@ -30,14 +43,19 @@ Ran all test suites.
`;

exports[`outputs coverage report 1`] = `
"-------------------------------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
-------------------------------|----------|----------|----------|----------|----------------|
All files | 41.18 | 0 | 25 | 41.18 | |
not-required-in-test-suite.js | 0 | 0 | 0 | 0 | 9,16,17,18,20 |
other-file.js | 100 | 100 | 100 | 100 | |
sum.js | 85.71 | 100 | 50 | 85.71 | 13 |
sum_dependency.js | 0 | 0 | 0 | 0 | 9,11,12,15 |
-------------------------------|----------|----------|----------|----------|----------------|
"-------------------------------------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
-------------------------------------|----------|----------|----------|----------|----------------|
All files | 56.52 | 0 | 50 | 56.52 | |
coverage_report | 41.18 | 0 | 25 | 41.18 | |
not-required-in-test-suite.js | 0 | 0 | 0 | 0 | 9,16,17,18,20 |
other-file.js | 100 | 100 | 100 | 100 | |
sum.js | 85.71 | 100 | 50 | 85.71 | 13 |
sum_dependency.js | 0 | 0 | 0 | 0 | 9,11,12,15 |
coverage_report/cached-duplicates/a | 100 | 100 | 100 | 100 | |
identical.js | 100 | 100 | 100 | 100 | |
coverage_report/cached-duplicates/b | 100 | 100 | 100 | 100 | |
identical.js | 100 | 100 | 100 | 100 | |
-------------------------------------|----------|----------|----------|----------|----------------|
"
`;
22 changes: 22 additions & 0 deletions integration_tests/__tests__/coverage_report.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,25 @@ test('outputs coverage report as json', () => {
);
}
});

test('collects coverage from duplicate files avoiding shared cache', () => {
const args = [
'--coverage',
// Ensure the status code is non-zero if super edge case with coverage triggers
'--coverageThreshold',
'{"global": {"lines": 100}}',
'--collectCoverageOnlyFrom',
'cached-duplicates/a/identical.js',
'--collectCoverageOnlyFrom',
'cached-duplicates/b/identical.js',
'--',
'identical.test.js',
];
// Run once to prime the cache
runJest(DIR, args);

// Run for the second time
const {stdout, status} = runJest(DIR, args);
expect(stdout).toMatchSnapshot();
expect(status).toBe(0);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

const sum = require('../identical').sum;

describe('sum', () => {
it('adds numbers', () => {
expect(sum(1, 2)).toEqual(3);
});
});
13 changes: 13 additions & 0 deletions integration_tests/coverage_report/cached-duplicates/a/identical.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

const sum = (a, b) => {
return a + b;
};

module.exports = {sum};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

const sum = require('../identical').sum;

describe('sum', () => {
it('adds numbers', () => {
expect(sum(1, 2)).toEqual(3);
});
});
13 changes: 13 additions & 0 deletions integration_tests/coverage_report/cached-duplicates/b/identical.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

const sum = (a, b) => {
return a + b;
};

module.exports = {sum};
6 changes: 4 additions & 2 deletions packages/babel-jest/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

import type {Path, ProjectConfig} from 'types/Config';
import type {TransformOptions} from 'types/Transform';
import type {CacheKeyOptions, TransformOptions} from 'types/Transform';

import crypto from 'crypto';
import fs from 'fs';
Expand Down Expand Up @@ -82,14 +82,16 @@ const createTransformer = (options: any) => {
fileData: string,
filename: Path,
configString: string,
{instrument}: TransformOptions,
{instrument, rootDir}: CacheKeyOptions,
): string {
return crypto
.createHash('md5')
.update(THIS_FILE)
.update('\0', 'utf8')
.update(fileData)
.update('\0', 'utf8')
.update(path.relative(rootDir, filename))
.update('\0', 'utf8')
.update(configString)
.update('\0', 'utf8')
.update(getBabelRC(filename))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`ScriptTransformer passes expected transform options to getCacheKey 1`]
Object {
"instrument": true,
"mapCoverage": true,
"rootDir": "/",
}
`;

Expand Down
1 change: 1 addition & 0 deletions packages/jest-runtime/src/script_transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class ScriptTransformer {
transformer.getCacheKey(fileData, filename, configString, {
instrument,
mapCoverage,
rootDir: this._config.rootDir,
}),
)
.update(CACHE_VERSION)
Expand Down
1 change: 1 addition & 0 deletions types/Transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type TransformOptions = {|
export type CacheKeyOptions = {|
instrument: boolean,
mapCoverage: boolean,
rootDir: string,
|};

export type Transformer = {|
Expand Down