Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Fixes

- `[jest-runtime]` Fix issue where user cannot utilize dynamic import despite specifying `--experimental-vm-modules` Node option ([#15842](https://github.com/jestjs/jest/pull/15842))
- `[jest-config]` Fix issue where user cannot toggle `collectCoverage` at project level ([#15588](https://github.com/jestjs/jest/issues/15588))
- `[jest-test-sequencer]` Fix issue where failed tests due to compilation errors not getting re-executed even with `--onlyFailures` CLI option ([#15851](https://github.com/jestjs/jest/pull/15851))

### Chore & Maintenance
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`exclude a project's code coverage if its collectCoverage is toggled off 1`] = `
"-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
package-1 | 100 | 100 | 100 | 100 |
index.js | 100 | 100 | 100 | 100 |
package-2 | 100 | 100 | 100 | 100 |
index.js | 100 | 100 | 100 | 100 |
-----------|---------|----------|---------|---------|-------------------"
`;
1 change: 0 additions & 1 deletion e2e/__tests__/__snapshots__/showConfig.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ exports[`--showConfig outputs config info and exits 1`] = `
"bail": 0,
"changedFilesWithAncestor": false,
"ci": true,
"collectCoverage": false,
"collectCoverageFrom": [],
"coverageDirectory": "<<REPLACED_ROOT_DIR>>/coverage",
"coverageProvider": "babel",
Expand Down
17 changes: 17 additions & 0 deletions e2e/__tests__/coverageReportForMultiProjects.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import * as path from 'path';
import runJest from '../runJest';

const DIR = path.resolve(__dirname, '../coverage-report-for-multi-projects');

test("exclude a project's code coverage if its collectCoverage is toggled off", () => {
const {stdout, exitCode} = runJest(DIR, ['--no-cache']);
expect(stdout).toMatchSnapshot();
expect(exitCode).toBe(0);
});
31 changes: 31 additions & 0 deletions e2e/coverage-report-for-multi-projects/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

export default {
clearMocks: true,
collectCoverage: true,
collectCoverageFrom: ['./**/index.js', '!./__tests__/**/*.js'],
coverageProvider: 'v8',
projects: [
{
displayName: 'Package 1',
rootDir: '<rootDir>/packages/package-1',
testMatch: ['<rootDir>/__tests__/**/*.e2e.js'],
},
{
displayName: 'Package 2',
rootDir: '<rootDir>/packages/package-2',
testMatch: ['<rootDir>/__tests__/**/*.e2e.js'],
},
{
collectCoverage: false,
displayName: 'Package 3',
rootDir: '<rootDir>/packages/package-3',
testMatch: ['<rootDir>/__tests__/**/*.e2e.js'],
},
],
};
6 changes: 6 additions & 0 deletions e2e/coverage-report-for-multi-projects/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "module",
"workspaces": [
"packages/*"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

const {package1} = require('../index.js');

describe('Example Package 1 Test', () => {
it('should run', () => {
expect(package1()).toBeTruthy();
});
});
11 changes: 11 additions & 0 deletions e2e/coverage-report-for-multi-projects/packages/package-1/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

module.exports = {
package1: () => true,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

const {package2} = require('../index');

describe('Example Package 2 Test', () => {
it('should run', () => {
expect(package2()).toBeTruthy();
});
});
11 changes: 11 additions & 0 deletions e2e/coverage-report-for-multi-projects/packages/package-2/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

module.exports = {
package2: () => true,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

describe('Example Package 3 Test', () => {
it('should run', () => {
expect(true).toBeTruthy();
});
});
11 changes: 11 additions & 0 deletions e2e/coverage-report-for-multi-projects/packages/package-3/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

module.exports = {
package3: () => true,
};
1 change: 1 addition & 0 deletions packages/jest-config/src/ValidConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ export const initialProjectOptions: Config.InitialProjectOptions = {
cache: true,
cacheDirectory: '/tmp/user/jest',
clearMocks: false,
collectCoverage: false,
collectCoverageFrom: ['src', '!public'],
coverageDirectory: 'coverage',
coveragePathIgnorePatterns: [NODE_MODULES_REGEXP],
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ const groupOptions = (
cache: options.cache,
cacheDirectory: options.cacheDirectory,
clearMocks: options.clearMocks,
collectCoverage: options.collectCoverage,
collectCoverageFrom: options.collectCoverageFrom,
coverageDirectory: options.coverageDirectory,
coveragePathIgnorePatterns: options.coveragePathIgnorePatterns,
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-config/src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,9 @@ export default async function normalize(
if (!options.coverageDirectory) {
options.coverageDirectory = path.resolve(options.rootDir, 'coverage');
}
if (!Object.hasOwn(options, 'collectCoverage')) {
options.collectCoverage = undefined;
}

setupBabelJest(options);
// TODO: Type this properly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ describe('ScriptTransformer', () => {
config = makeProjectConfig({
cache: true,
cacheDirectory: '/cache/',
collectCoverage: undefined,
id: 'test',
rootDir: '/',
transformIgnorePatterns: ['/node_modules/'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ exports[`ScriptTransformer in async mode, passes expected transform options to g
"cache": true,
"cacheDirectory": "/cache/",
"clearMocks": false,
"collectCoverage": undefined,
"collectCoverageFrom": Array [
"src",
"!public",
Expand Down Expand Up @@ -177,6 +178,7 @@ exports[`ScriptTransformer passes expected transform options to getCacheKey 1`]
"cache": true,
"cacheDirectory": "/cache/",
"clearMocks": false,
"collectCoverage": undefined,
"collectCoverageFrom": Array [
"src",
"!public",
Expand Down Expand Up @@ -302,6 +304,7 @@ exports[`ScriptTransformer passes expected transform options to getCacheKeyAsync
"cache": true,
"cacheDirectory": "/cache/",
"clearMocks": false,
"collectCoverage": undefined,
"collectCoverageFrom": Array [
"src",
"!public",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ describe('shouldInstrument', () => {
}),
changedFiles: undefined,
};
const defaultConfig = makeProjectConfig({rootDir: '/'});
const defaultConfig = makeProjectConfig({
collectCoverage: undefined,
rootDir: '/',
});
describe('should return true', () => {
const testShouldInstrument = (
filename = defaultFilename,
Expand Down
6 changes: 5 additions & 1 deletion packages/jest-transform/src/shouldInstrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ export default function shouldInstrument(
config: Config.ProjectConfig,
loadedFilenames?: Array<string>,
): boolean {
if (!options.collectCoverage) {
// Do not instrument when `collectCoverage` is toggled off global or project-wide
if (
!options.collectCoverage ||
(typeof config.collectCoverage === 'boolean' && !config.collectCoverage)
) {
return false;
}

Expand Down
1 change: 1 addition & 0 deletions packages/jest-types/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ export type ProjectConfig = {
cache: boolean;
cacheDirectory: string;
clearMocks: boolean;
collectCoverage?: boolean;
collectCoverageFrom: Array<string>;
coverageDirectory: string;
coveragePathIgnorePatterns: Array<string>;
Expand Down
1 change: 1 addition & 0 deletions packages/test-utils/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const DEFAULT_PROJECT_CONFIG: Config.ProjectConfig = {
cache: false,
cacheDirectory: '/test_cache_dir/',
clearMocks: false,
collectCoverage: undefined,
collectCoverageFrom: ['src', '!public'],
coverageDirectory: 'coverage',
coveragePathIgnorePatterns: [],
Expand Down
Loading