Skip to content

Commit 2e563b4

Browse files
authored
feat: adds coverageThreshold as a config, uses non-workspace dir for tests (#533)
This pull request updates how the `node:test` test runner handles the coverage threshold configuration and improves the generated `package.json` scripts. The main changes make the coverage threshold customizable (defaulting to 100 if not set), ensure that test scripts explicitly target test files, and update tests to cover these behaviors.
1 parent adf1df0 commit 2e563b4

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

lib/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ const getFullConfig = async ({
131131

132132
// test runner configuration
133133
const isNodeTest = pkgConfig.testRunner === 'node:test'
134+
const coverageThreshold = pkgConfig.coverageThreshold ?? 100
134135

135136
// all derived keys
136137
const derived = {
@@ -173,6 +174,7 @@ const getFullConfig = async ({
173174
deleteJsExt: esm ? 'js' : 'cjs',
174175
// test runner
175176
isNodeTest,
177+
coverageThreshold,
176178
// tap
177179
// 18 and up doesn't like nyc-arg
178180
tap18: !isNodeTest && semver.coerce(pkg.pkgJson?.devDependencies?.tap)?.major >= 18,

lib/content/package-json.hbs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
"postlint": "template-oss-check",
1919
"template-oss-apply": "template-oss-apply --force",
2020
{{#if isNodeTest}}
21-
"snap": "node --test --test-update-snapshots",
22-
"test": "node --test",
23-
"test:cover": "node --test --experimental-test-coverage --test-timeout=3000 --test-coverage-lines=100 --test-coverage-functions=100 --test-coverage-branches=100",
21+
"snap": "node --test --test-update-snapshots ./test/**/*.js",
22+
"test": "node --test ./test/**/*.js",
23+
"test:cover": "node --test --experimental-test-coverage --test-timeout=3000{{#if coverageThreshold}} --test-coverage-lines={{coverageThreshold}} --test-coverage-functions={{coverageThreshold}} --test-coverage-branches={{coverageThreshold}}{{/if}} ./test/**/*.js",
2424
{{else}}
2525
"snap": "{{#if typescript}}{{#if tap16}}c8 {{/if}}{{/if}}tap",
2626
"test": "{{#if typescript}}{{#if tap16}}c8 {{/if}}{{/if}}tap",

test/apply/node-test.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ t.test('node:test runner', async t => {
1515
const pkg = await s.readJson('package.json')
1616

1717
// Verify test scripts are for node:test
18-
t.equal(pkg.scripts.test, 'node --test')
18+
t.equal(pkg.scripts.test, 'node --test ./test/**/*.js')
1919
t.equal(
2020
pkg.scripts['test:cover'],
21-
'node --test --experimental-test-coverage --test-timeout=3000 --test-coverage-lines=100 --test-coverage-functions=100 --test-coverage-branches=100',
21+
'node --test --experimental-test-coverage --test-timeout=3000 --test-coverage-lines=100 --test-coverage-functions=100 --test-coverage-branches=100 ./test/**/*.js',
2222
)
23-
t.equal(pkg.scripts.snap, 'node --test --test-update-snapshots')
23+
t.equal(pkg.scripts.snap, 'node --test --test-update-snapshots ./test/**/*.js')
2424

2525
// Verify tap section is removed
2626
t.notOk(pkg.tap, 'tap section should not be present')
@@ -36,6 +36,30 @@ t.test('node:test runner', async t => {
3636
}
3737
})
3838

39+
t.test('node:test runner with incomplete coverage', async t => {
40+
const s = await setup(t, {
41+
ok: true,
42+
package: {
43+
templateOSS: {
44+
testRunner: 'node:test',
45+
coverageThreshold: 0,
46+
},
47+
},
48+
})
49+
50+
await s.apply()
51+
const pkg = await s.readJson('package.json')
52+
53+
// Verify test scripts are for node:test
54+
t.equal(pkg.scripts.test, 'node --test ./test/**/*.js')
55+
t.equal(
56+
pkg.scripts['test:cover'],
57+
'node --test --experimental-test-coverage --test-timeout=3000 ./test/**/*.js',
58+
'test:cover should not include coverage thresholds when coverageThreshold is 0',
59+
)
60+
t.equal(pkg.scripts.snap, 'node --test --test-update-snapshots ./test/**/*.js')
61+
})
62+
3963
t.test('tap runner (default)', async t => {
4064
const s = await setup(t, {
4165
ok: true,

0 commit comments

Comments
 (0)