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 2 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};
2 changes: 2 additions & 0 deletions packages/babel-jest/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ const createTransformer = (options: any) => {
.update('\0', 'utf8')
.update(fileData)
.update('\0', 'utf8')
.update(filename)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this relative to the rootDir (if it isn't already)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not, checking 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine with me.

Copy link
Contributor Author

@stipsan stipsan Sep 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright I'm on it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed it, I also changed a flowtype hint as it appeared to be incorrect: 69456ca#diff-066bbe3aae5baf14d3b62017f0dc86d4R85

According to the definition here: https://github.com/facebook/jest/blob/0cb03f6474f52964ed74eadcde2ae3e219b4d04e/types/Transform.js#L37-L41

getCacheKey gets CacheKeyOptions not TransformOptions .

.update('\0', 'utf8')
.update(configString)
.update('\0', 'utf8')
.update(getBabelRC(filename))
Expand Down