Skip to content

Commit

Permalink
coverage: Switch to raw coverage files (#40013)
Browse files Browse the repository at this point in the history
It turns out that there really aren't any really good generic coverage
reporting tools, the best you can get is something that highlights
covered lines without function/method stats, branch info, or anything
else fancy.

To provide useful coverage output, we're going to look instead at
generating separate coverage dashboards for PHP (PHPUnit) and JS
(jest/istanbul), each generated by those tools so we can get all the
useful info possible. To do that, we'll need all the coverage commands
to generate the tools' raw data formats rather than a lossy format like
clover.

As part of this conversion, we're adding `jest.config.cjs` everywhere so
we can consistently set `collectCoverageFrom`. By default the base
config will include non-test files under `src/`. Various packages need
additional dirs such as `components/`, and a root-level `index.jsx` or
`global.d.ts` seems common too. Then, since we have config files
everywhere, we can move `coverageDirectory` and `coverageReporters` into
the configs too.
  • Loading branch information
anomiex authored Nov 2, 2024
1 parent 5586df9 commit 161c7a6
Show file tree
Hide file tree
Showing 271 changed files with 1,611 additions and 910 deletions.
16 changes: 8 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion docs/monorepo.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,9 @@ If a project contains PHP or JavaScript tests, it should also define `.scripts.t

Output should be written to the path specified via the `COVERAGE_DIR` environment variable. Subdirectories of that path may be used as desired.

For PHP tests, you'll probably run PHPUnit as `php -dpcov.directory=. "$(command -v phpunit)" --coverage-clover "$COVERAGE_DIR/php/clover.xml"`.
For PHP tests, you'll probably run PHPUnit as `php -dpcov.directory=. "$(command -v phpunit)" --coverage-php "$COVERAGE_DIR/php.cov"`. If you have multiple runs (e.g. unit and integration), be sure to write the `php.cov` files to separate subdirectories of `$COVERAGE_DIR`.

For JS tests, you'll probably have a `test` script in package.json that runs `jest` with any needed options, and then a `test-coverage` script that does `pnpm run test --coverage`. If you have multiple runs (e.g. unit and integration), be sure each run writes to a different subdirectory of `$COVERAGE_DIR`.

There's no need to be concerned about collisions with other projects' coverage files, a separate directory is used per project. The coverage files are also automatically copied to `ARTIFACTS_DIR`.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Generate raw phpunit and/or jest coverage data instead of clover.


2 changes: 1 addition & 1 deletion projects/github-actions/test-results-to-slack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
"scripts": {
"build": "ncc build src/index.js -o dist --source-map --license licenses.txt",
"test": "jest --config=tests/jest.config.js --verbose --runInBand",
"test-coverage": "pnpm run test --coverage --coverageDirectory=\"$COVERAGE_DIR/js\" --coverageReporters=clover"
"test-coverage": "pnpm run test --coverage"
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const path = require( 'path' );
const coverageConfig = require( 'jetpack-js-tools/jest/config.coverage.js' );

module.exports = {
...coverageConfig,
rootDir: path.resolve( __dirname, '..' ),
roots: [ '<rootDir>/tests/' ],
collectCoverageFrom: [ '<rootDir>/src/**/*.js' ],
resolver: require.resolve( 'jetpack-js-tools/jest/jest-resolver.js' ),
clearMocks: true,
resetModules: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Generate raw phpunit and/or jest coverage data instead of clover.


2 changes: 1 addition & 1 deletion projects/js-packages/ai-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"clean": "rm -rf build/",
"compile-ts": "tsc --pretty",
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
"test-coverage": "pnpm run test --coverage --coverageDirectory=\"$COVERAGE_DIR/js\" --coverageReporters=clover",
"test-coverage": "pnpm run test --coverage",
"watch": "tsc --watch --pretty"
},
"type": "module",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Generate raw phpunit and/or jest coverage data instead of clover.


2 changes: 1 addition & 1 deletion projects/js-packages/analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
},
"scripts": {
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
"test-coverage": "pnpm run test --coverageDirectory=\"$COVERAGE_DIR/js\" --coverage --coverageReporters=clover"
"test-coverage": "pnpm run test --coverage"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Generate raw phpunit and/or jest coverage data instead of clover.


2 changes: 1 addition & 1 deletion projects/js-packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
},
"scripts": {
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
"test-coverage": "pnpm run test --coverageDirectory=\"$COVERAGE_DIR/js\" --coverage --coverageReporters=clover"
"test-coverage": "pnpm run test --coverage"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Generate raw phpunit and/or jest coverage data instead of clover.


Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"license": "GPL-2.0-or-later",
"author": "Automattic",
"scripts": {
"test": "jest tests",
"test-coverage": "pnpm run test --coverage --collectCoverageFrom='src/**/*.js' --coverageDirectory=\"$COVERAGE_DIR/js\" --coverageReporters=clover"
"test": "jest --config=tests/jest.config.cjs",
"test-coverage": "pnpm run test --coverage"
},
"dependencies": {
"debug": "^4.3.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const path = require( 'path' );
const coverageConfig = require( 'jetpack-js-tools/jest/config.coverage.js' );

module.exports = {
...coverageConfig,
rootDir: path.join( __dirname, '..' ),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Generate raw phpunit and/or jest coverage data instead of clover.


4 changes: 2 additions & 2 deletions projects/js-packages/boost-score-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"build": "pnpm run clean && webpack",
"clean": "rm -rf build/",
"watch": "pnpm run build && pnpm webpack watch",
"test": "jest tests",
"test-coverage": "pnpm run test --coverage --coverageDirectory=\"$COVERAGE_DIR/js\" --coverageReporters=clover"
"test": "jest --config=tests/jest.config.cjs",
"test-coverage": "pnpm run test --coverage"
},
"dependencies": {
"@wordpress/i18n": "5.9.0",
Expand Down
7 changes: 7 additions & 0 deletions projects/js-packages/boost-score-api/tests/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const path = require( 'path' );
const coverageConfig = require( 'jetpack-js-tools/jest/config.coverage.js' );

module.exports = {
...coverageConfig,
rootDir: path.join( __dirname, '..' ),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Generate raw phpunit and/or jest coverage data instead of clover.


6 changes: 6 additions & 0 deletions projects/js-packages/components/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ const baseConfig = require( 'jetpack-js-tools/jest/config.base.js' );

module.exports = {
...baseConfig,
collectCoverageFrom: [
'<rootDir>/components/**/*.{js,mjs,cjs,jsx,ts,tsx,mts,cts}',
'<rootDir>/lib/**/*.{js,mjs,cjs,jsx,ts,tsx,mts,cts}',
'<rootDir>/tools/**/*.{js,mjs,cjs,jsx,ts,tsx,mts,cts}',
...baseConfig.collectCoverageFrom,
],
};
2 changes: 1 addition & 1 deletion projects/js-packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@
"build": "pnpm run compile-ts",
"compile-ts": "tsc --pretty",
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
"test-coverage": "pnpm run test --coverageDirectory=\"$COVERAGE_DIR/js\" --coverage --coverageReporters=clover"
"test-coverage": "pnpm run test --coverage"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Generate raw phpunit and/or jest coverage data instead of clover.


3 changes: 3 additions & 0 deletions projects/js-packages/config/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Note we intentionally don't use jetpack-js-tools/jest/config.base.js here.
// This doesn't need jsdom or any of the other fancy stuff, but it does need to avoid the
// standard jest-jetpack-config.js mocking.
const coverageConfig = require( 'jetpack-js-tools/jest/config.coverage.js' );

module.exports = {
...coverageConfig,
testMatch: [ '<rootDir>/**/test/*.[jt]s' ],
resolver: require.resolve( 'jetpack-js-tools/jest/jest-resolver.js' ),
};
2 changes: 1 addition & 1 deletion projects/js-packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"author": "Automattic",
"scripts": {
"test": "jest",
"test-coverage": "pnpm run test --coverageDirectory=\"$COVERAGE_DIR/js\" --coverage --coverageReporters=clover"
"test-coverage": "pnpm run test --coverage"
},
"devDependencies": {
"jest": "29.7.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Generate raw phpunit and/or jest coverage data instead of clover.


7 changes: 7 additions & 0 deletions projects/js-packages/connection/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@ const baseConfig = require( 'jetpack-js-tools/jest/config.base.js' );
module.exports = {
...baseConfig,
setupFilesAfterEnv: [ ...baseConfig.setupFilesAfterEnv, '<rootDir>/jest.setup.js' ],
collectCoverageFrom: [
'<rootDir>/components/**/*.{js,mjs,cjs,jsx,ts,tsx,mts,cts}',
'<rootDir>/helpers/**/*.{js,mjs,cjs,jsx,ts,tsx,mts,cts}',
'<rootDir>/hooks/**/*.{js,mjs,cjs,jsx,ts,tsx,mts,cts}',
'<rootDir>/state/**/*.{js,mjs,cjs,jsx,ts,tsx,mts,cts}',
...baseConfig.collectCoverageFrom,
],
};
2 changes: 1 addition & 1 deletion projects/js-packages/connection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@
],
"scripts": {
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
"test-coverage": "pnpm run test --coverageDirectory=\"$COVERAGE_DIR/js\" --coverage --coverageReporters=clover"
"test-coverage": "pnpm run test --coverage"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Generate raw phpunit and/or jest coverage data instead of clover.


2 changes: 1 addition & 1 deletion projects/js-packages/critical-css-gen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"clean": "rm -rf build/",
"clean:test": "rm -rf tests/build/",
"test": "pnpm build && pnpm build:test && NODE_ENV=test NODE_PATH=./node_modules jest --forceExit --config=tests/config/jest.config.js",
"test-coverage": "pnpm run test --coverage --coverageDirectory=\"$COVERAGE_DIR/js\" --coverageReporters=clover"
"test-coverage": "pnpm run test --coverage"
},
"main": "./build/browser.js",
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import coverageConfig from 'jetpack-js-tools/jest/config.coverage.js';

export default {
...coverageConfig,
rootDir: '../',
testEnvironment: 'jest-environment-node',
testMatch: [ '**/?(*.)+(spec|test).js' ],
setupFilesAfterEnv: [ './config/jest-setup.js' ],
collectCoverageFrom: [ '../build-node/*.js' ],
testPathIgnorePatterns: [ '/node_modules/', 'config/jest-setup.js', 'build-node/*' ],
moduleDirectories: [ 'build-node', 'node_modules' ],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Generate raw phpunit and/or jest coverage data instead of clover.


2 changes: 1 addition & 1 deletion projects/js-packages/eslint-changed/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"scripts": {
"test": "NODE_OPTIONS=--experimental-vm-modules jest --config=tests/jest.config.cjs",
"test-coverage": "pnpm run test --coverage --coverageDirectory=\"$COVERAGE_DIR/js\" --coverageReporters=clover"
"test-coverage": "pnpm run test --coverage"
},
"dependencies": {
"chalk": "5.0.1",
Expand Down
7 changes: 6 additions & 1 deletion projects/js-packages/eslint-changed/tests/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
const path = require( 'path' );
const coverageConfig = require( 'jetpack-js-tools/jest/config.coverage.js' );

module.exports = {
...coverageConfig,
rootDir: path.resolve( __dirname, '..' ),
roots: [ '<rootDir>/tests/' ],
collectCoverageFrom: [ '<rootDir>/src/**/*.js' ],
resolver: require.resolve( 'jetpack-js-tools/jest/jest-resolver.js' ),
collectCoverageFrom: [
'<rootDir>/bin/**/*.{js,mjs,cjs,jsx,ts,tsx,mts,cts}',
...coverageConfig.collectCoverageFrom,
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Generate raw phpunit and/or jest coverage data instead of clover.


4 changes: 2 additions & 2 deletions projects/js-packages/eslint-config-target-es/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"license": "GPL-2.0-or-later",
"author": "Automattic",
"scripts": {
"test": "jest tests",
"test-coverage": "pnpm run test --coverage --collectCoverageFrom='src/**/*.js' --coverageDirectory=\"$COVERAGE_DIR/js\" --coverageReporters=clover"
"test": "jest --config=tests/jest.config.cjs",
"test-coverage": "pnpm run test --coverage"
},
"dependencies": {
"@mdn/browser-compat-data": "5.5.49",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const path = require( 'path' );
const coverageConfig = require( 'jetpack-js-tools/jest/config.coverage.js' );

module.exports = {
...coverageConfig,
rootDir: path.join( __dirname, '..' ),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Generate raw phpunit and/or jest coverage data instead of clover.


4 changes: 2 additions & 2 deletions projects/js-packages/i18n-check-webpack-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"license": "GPL-2.0-or-later",
"author": "Automattic",
"scripts": {
"test": "jest tests/",
"test-coverage": "pnpm run test --coverage --collectCoverageFrom='src/**/*.js' --coverageDirectory=\"$COVERAGE_DIR/js\" --coverageReporters=clover"
"test": "jest --config=tests/jest.config.cjs",
"test-coverage": "pnpm run test --coverage"
},
"dependencies": {
"debug": "^4.3.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const path = require( 'path' );
const coverageConfig = require( 'jetpack-js-tools/jest/config.coverage.js' );

module.exports = {
...coverageConfig,
rootDir: path.join( __dirname, '..' ),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Generate raw phpunit and/or jest coverage data instead of clover.


Loading

0 comments on commit 161c7a6

Please sign in to comment.