test: bring CLI coverage to 100% and enforce it - #916
Open
Tony133 wants to merge 1 commit into
Open
Conversation
Tony133
marked this pull request as ready for review
September 8, 2026 20:50
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposal:
Follow-up to #913. Now that
suite-runner.jsactually runs the tests, c8 reports real numbers for the CLI sources, and they were around 87%. This brings every file under*.jsandlib/**to 100% statements / branches / functions / lines, and adds a.c8rc.jsonwithcheck-coverage: truesonpm testfails if it drops.The c8 report is also scoped to the shipped sources:
test/,templates/,examples/andsuite-runner.jsare excluded, since they are fixtures and test infrastructure rather than CLI code.New and extended tests:
generate-readme,generate-swagger,print-plugins,print-routes,generate,generate-plugin,eject,cli: thecli()paths and therequire.main === moduleblocks are exercised by spawning the scripts as child processes (c8 follows children viaNODE_V8_COVERAGE), soprocess.exitpaths are tested for real. Error paths ingenerate()useproxyquireto failgenerifyandnode:fs.util.test.js:exit,requireModule*,requireFastifyForModulefailure,showHelpForCommandwith an unknown command,isKubernetes.start-extra.test.js: missing file parameter, fastify not loadable,--address,--trust-proxy-enabled(verified throughrequest.ip), theerrbranch of theclose-with-gracehandler. The twois-dockermocks instart.test.jsnow cover both module shapes (CJS function and ESM{ default }).watch-unit.test.js:lib/watch/index.jsdriven with fakechokidar/child_process.fork(restart on change, verbose logging, ready emitted once, error → stop, uncaughtException → restart).watch-fork.test.js:lib/watch/fork.jsforked for real (app crash,EADDRINUSE, graceful shutdown, forced exit after the close timeout, shutdown before the server is up). Each test has a timeout and kills its child on teardown, so a stuck child fails the test instead of hanging the suite.print-plugins/print-routes: theskipunder coverage is gone. It was needed with nyc, which instrumented the sources and changed the printed function bodies; c8 uses native V8 coverage and does not touch the code (verified withNODE_V8_COVERAGEset).Bugs found on the way:
node eject.js --esmcrashed: the standalone entry point calledcli()without arguments. Nowcli(process.argv.slice(2)).generate.js, a failingnpm init -ythrew synchronously inside the generify callback and escaped the promise: an uncaught exception instead oflog('error')+ exit 1. It is now caught and rejected.Dead code removed:
util.js: themoduleSupportcheck (Node < 14).semveris no longer needed at runtime and moves todevDependencies(tests still use it).start.js: the "debug mode not compatible with Node < 6" branch.lib/watch/index.js: thechilds.length !== 0branch in the change handler — at most one child is ever in the array.lib/watch/fork.js: thetry/catcharoundfastify.ready()afterlisten()already succeeded; a rejection there is handled bymain().catch, with the same exit code.generate-readme.js:stop(error)was only ever called without an argument.args.js:commandLineArguments['--'] || []—'--'is always populated withpopulateRest: true.lib/parse-args.js:token.originalis never set on positional tokens.pkg.scripts = Object.assign(pkg.scripts || {}, …)→ object spread, same semantics without the branch.Based on #913 — merge that one first.
Screenshot Test Coverage 100%
see also here: https://github.com/fastify/fastify-cli/actions/runs/34277129667/job/102232798581?pr=916#step:5:589