Skip to content

Commit 7ada46b

Browse files
Fix code coverage for monorepos, facebook#4649
1 parent 2322d0e commit 7ada46b

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

packages/react-dev-utils/workspaceUtils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ const findPkgs = (rootPath, globPatterns) => {
3131

3232
const findMonorepo = appDir => {
3333
const monoPkgPath = findPkg.sync(path.resolve(appDir, '..'));
34+
const monoRootPath = monoPkgPath && path.dirname(monoPkgPath);
3435
const monoPkg = monoPkgPath && require(monoPkgPath);
3536
const patterns = monoPkg && monoPkg.workspaces;
3637
const isYarnWs = Boolean(patterns);
37-
const allPkgs = patterns && findPkgs(path.dirname(monoPkgPath), patterns);
38+
const allPkgs = patterns && findPkgs(monoRootPath, patterns);
3839
const isIncluded = dir => allPkgs && allPkgs.indexOf(dir) !== -1;
3940
const isAppIncluded = isIncluded(appDir);
4041
const pkgs = allPkgs
@@ -45,6 +46,7 @@ const findMonorepo = appDir => {
4546
isAppIncluded,
4647
isYarnWs,
4748
pkgs,
49+
rootPath: monoRootPath,
4850
};
4951
};
5052

packages/react-scripts/config/paths.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ if (checkForMonorepo) {
168168
const mono = findMonorepo(appDirectory);
169169
if (mono.isAppIncluded) {
170170
Array.prototype.push.apply(module.exports.srcPaths, mono.pkgs);
171+
module.exports.isMonorepo = true;
172+
module.exports.monorepoRoot = mono.rootPath;
171173
}
172174
module.exports.useYarn = module.exports.useYarn || mono.isYarnWs;
173175
}

packages/react-scripts/scripts/eject.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ inquirer
107107

108108
// Prepare Jest config early in case it throws
109109
const jestConfig = createJestConfig(
110-
filePath => path.posix.join('<rootDir>', filePath),
110+
filePath => path.posix.join('.', filePath),
111111
null,
112112
paths.srcPaths
113113
);

packages/react-scripts/scripts/utils/createJestConfig.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,31 @@ const paths = require('../../config/paths');
1515
module.exports = (resolve, rootDir, srcRoots) => {
1616
// Use this instead of `paths.testsSetup` to avoid putting
1717
// an absolute filename into configuration after ejecting.
18+
if (paths.isMonorepo) {
19+
rootDir = paths.monorepoRoot;
20+
}
21+
22+
const toRelRootDir = f => '<rootDir>/' + path.relative(rootDir || '', f);
23+
1824
const setupTestsMatches = paths.testsSetup.match(/src\/setupTests\.(.+)/);
1925
const setupTestsFileExtension =
2026
(setupTestsMatches && setupTestsMatches[1]) || 'js';
2127
const setupTestsFile = fs.existsSync(paths.testsSetup)
22-
? `<rootDir>/src/setupTests.${setupTestsFileExtension}`
28+
? toRelRootDir(`src/setupTests.${setupTestsFileExtension}`)
2329
: undefined;
2430

25-
const toRelRootDir = f => '<rootDir>/' + path.relative(rootDir || '', f);
26-
2731
// TODO: I don't know if it's safe or not to just use / as path separator
2832
// in Jest configs. We need help from somebody with Windows to determine this.
2933
const config = {
30-
collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}', '!src/**/*.d.ts'],
31-
34+
collectCoverageFrom: srcRoots
35+
.map(root => path.join(toRelRootDir(root), '**/*.{js,jsx,ts,tsx}'))
36+
.concat(['!**/build*/**', '!**/coverage*/**', '!**/*.d.ts']),
3237
// TODO: this breaks Yarn PnP on eject.
3338
// But we can't simply emit this because it'll be an absolute path.
3439
// The proper fix is to write jest.config.js on eject instead of a package.json key.
3540
// Then these can always stay as require.resolve()s.
3641
resolver: require.resolve('jest-pnp-resolver'),
3742
setupFiles: [require.resolve('react-app-polyfill/jsdom')],
38-
3943
setupTestFrameworkScriptFile: setupTestsFile,
4044
testMatch: [
4145
'**/__tests__/**/*.{js,jsx,ts,tsx}',

0 commit comments

Comments
 (0)