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 @@ -10,6 +10,7 @@
- `[jest-reporters]` Fix issue where console output not displayed for GHA reporter even with `silent: false` option ([#15864](https://github.com/jestjs/jest/pull/15864))
- `[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-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))
- `[jest-runner, jest-transform]` Fix coverage report doesn't show correct code coverage when using `projects` config option ([#15880](https://github.com/jestjs/jest/pull/15880)), fixes ([#5417](https://github.com/jestjs/jest/issues/5417))

### Chore & Maintenance

Expand Down
12 changes: 12 additions & 0 deletions e2e/__tests__/__snapshots__/coverageReport.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,15 @@ Functions : 50% ( 3/6 )
Lines : 60% ( 12/20 )
================================================================================"
`;
exports[`outputs coverage report with projects option 1`] = `
"------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
------------|---------|----------|---------|---------|-------------------
All files | 66.66 | 100 | 50 | 66.66 |
client | 66.66 | 100 | 50 | 66.66 |
client.js | 66.66 | 100 | 50 | 66.66 | 13
server | 66.66 | 100 | 50 | 66.66 |
server.js | 66.66 | 100 | 50 | 66.66 | 13
------------|---------|----------|---------|---------|-------------------"
`;
10 changes: 10 additions & 0 deletions e2e/__tests__/coverageReport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,13 @@ test('generates coverage when using the testRegex config param ', () => {
expect(stdout).toMatchSnapshot();
expect(exitCode).toBe(0);
});

test('outputs coverage report with projects option', () => {
const projectDir = path.resolve(__dirname, '../coverage-with-projects');
const {stdout, exitCode} = runJest(projectDir, ['--no-cache', '--coverage'], {
stripAnsi: true,
});

expect(stdout).toMatchSnapshot();
expect(exitCode).toBe(0);
});
13 changes: 13 additions & 0 deletions e2e/coverage-with-projects/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"jest": {
"projects": [
"<rootDir>/packages/client",
"<rootDir>/packages/server"
],
"collectCoverageFrom": [
"packages/client/**/*.js",
"packages/server/**/*.js"
],
"testEnvironment": "node"
}
}
16 changes: 16 additions & 0 deletions e2e/coverage-with-projects/packages/client/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* 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.
*/

function add(a, b) {
return a + b;
}

function subtract(a, b) {
return a - b;
}

module.exports = {add, subtract};
12 changes: 12 additions & 0 deletions e2e/coverage-with-projects/packages/client/client.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* 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.
*/

const {add} = require('./client');

test('add function', () => {
expect(add(1, 2)).toBe(3);
});
5 changes: 5 additions & 0 deletions e2e/coverage-with-projects/packages/client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
5 changes: 5 additions & 0 deletions e2e/coverage-with-projects/packages/server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
16 changes: 16 additions & 0 deletions e2e/coverage-with-projects/packages/server/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* 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.
*/

function multiply(a, b) {
return a * b;
}

function divide(a, b) {
return a / b;
}

module.exports = {divide, multiply};
12 changes: 12 additions & 0 deletions e2e/coverage-with-projects/packages/server/server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* 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.
*/

const {multiply} = require('./server');

test('multiply function', () => {
expect(multiply(2, 3)).toBe(6);
});
1 change: 1 addition & 0 deletions packages/jest-runner/src/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ async function runTestInternal(
collectCoverage: globalConfig.collectCoverage,
collectCoverageFrom: globalConfig.collectCoverageFrom,
coverageProvider: globalConfig.coverageProvider,
globalRootDir: globalConfig.rootDir,
sourcesRelatedToTestsInChangedFiles:
context.sourcesRelatedToTestsInChangedFiles,
},
Expand Down
33 changes: 33 additions & 0 deletions packages/jest-transform/src/__tests__/shouldInstrument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,28 @@ describe('shouldInstrument', () => {
forceCoverageMatch: ['**/do/**/*.json'],
});
});

it('when using projects with globalRootDir and file matches collectCoverageFrom', () => {
testShouldInstrument(
'/root/packages/server/server.js',
{
collectCoverageFrom: ['server/**/*.js'],
globalRootDir: '/root/packages',
},
{rootDir: '/root/packages/server'},
);
});

it('when using projects with globalRootDir and file matches collectCoverageFrom with multiple patterns', () => {
testShouldInstrument(
'/root/packages/client/client.js',
{
collectCoverageFrom: ['client/**/*.js', 'server/**/*.js'],
globalRootDir: '/root/packages',
},
{rootDir: '/root/packages/client'},
);
});
});

describe('should return false', () => {
Expand Down Expand Up @@ -259,5 +281,16 @@ describe('shouldInstrument', () => {
defaultConfig,
);
});

it('when using projects with globalRootDir and file does not match collectCoverageFrom', () => {
testShouldInstrument(
'/root/packages/utils/utils.js',
{
collectCoverageFrom: ['client/**/*.js', 'server/**/*.js'],
globalRootDir: '/root/packages',
},
{rootDir: '/root/packages/utils'},
);
});
});
});
4 changes: 3 additions & 1 deletion packages/jest-transform/src/shouldInstrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ export default function shouldInstrument(
// still cover if `only` is specified
options.collectCoverageFrom.length > 0 &&
!globsToMatcher(options.collectCoverageFrom)(
replacePathSepForGlob(path.relative(config.rootDir, filename)),
replacePathSepForGlob(
path.relative(options.globalRootDir ?? config.rootDir, filename),
),
)
) {
return false;
Expand Down
1 change: 1 addition & 0 deletions packages/jest-transform/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface ShouldInstrumentOptions
> {
changedFiles?: Set<string>;
sourcesRelatedToTestsInChangedFiles?: Set<string>;
globalRootDir?: string;
}

export interface Options
Expand Down
Loading