Skip to content

Add Web Test Runner plugin - #2295

Merged
webpro merged 5 commits into
webpro-nl:mainfrom
clean99:contrib/knip-web-test-runner
Nov 9, 2026
Merged

webpro merged 5 commits into
webpro-nl:mainfrom
clean99:contrib/knip-web-test-runner

Conversation

@clean99

@clean99 clean99 commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Recognize Web Test Runner configs so their imported plugins, browser launchers and helpers are kept. On Chai's config, this removes unused reports for @web/dev-server-rollup and @rollup/plugin-commonjs.

Test files are picked up from the config too: the top-level files and each inline groups[].files, ! patterns included, plus files passed after -- on either binary. A files key inside a plugin's options is not read. On htmx, that clears 50 reported test files; on Turbo, 5 files and @open-wc/testing.

The plugin follows web-test-runner.config.{js,cjs,mjs}, --config on both binaries, and explicit test paths relative to the command's working directory, including yarn --cwd.

Verified with the real runner, base/branch comparisons on public projects, pnpm run ci, and the full Node test suite (1,681 tests).

@pkg-pr-new

pkg-pr-new Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/knip@2295
npm i https://pkg.pr.new/@knip/language-server@2295
npm i https://pkg.pr.new/@knip/mcp@2295

commit: b658f5f

@webpro webpro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good to have Web Test Runner covered! In Chai, for example, the config file and the dependencies it imports are no longer reported. Two things are still missing: the test files listed in the config, and test files passed after --. Details inline.


const isEnabled: IsPluginEnabled = ({ dependencies }) => hasDependency(dependencies, enablers);

const entry = ['web-test-runner.config.{js,cjs,mjs}'];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description leaves files and groups out of scope, but Web Test Runner has no default for them and errors out without either, so tests are typically listed in the config. In tab-container-element, turbo and htmx, the test files listed there are still reported as unused (1, 7 and 50 files), and so are dependencies like @open-wc/testing.

Could this become config, with a resolveFromAST that reads the top-level files and each inline groups[].files, mapped like --files including ! patterns? Please keep the read limited to those two locations: a files key inside a plugin's options would otherwise be picked up too, which can hide unused files or exclude real tests.

A non-default files pattern in the fixture, with the test asserting that file is used, would cover it.

'watch',
],
resolveInputs: (parsed, { cwd }) =>
[...parsed._, ...[parsed.files ?? []].flat()].map(id => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test files after --, as in wtr -- alpha.js beta.js, end up in parsed['--']. Web Test Runner runs them, but Knip reports them and their dependencies as unused:

Suggested change
[...parsed._, ...[parsed.files ?? []].flat()].map(id => {
[...parsed._, ...(parsed['--'] ?? []), ...[parsed.files ?? []].flat()].map(id => {

Knip reported the test files a Web Test Runner config lists in `files` and
in inline `groups[].files` as unused, and the same for files passed after
`--` on the command line, which the runner executes.

The plugin now parses the config with `resolveFromAST`, reading only those
two locations, and maps both to entries relative to the config file. Its
argument parser appends `parsed['--']`. Both keep `!` patterns.

The fixture config lists one file, excludes another and names a group file,
a plugin option carries a `files` key that must not count, and a script
passes a file after `--`.
@clean99

clean99 commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

The plugin now reads the top-level files and each inline groups[].files, ! patterns included, and nothing else, so a files key inside a plugin's options stays ignored; files after -- are picked up too, both resolved from the command's working directory like --files.

The fixture covers a listed file, an excluded one, a group file, a nested key that must not count, and a script passing a file after --.

Against turbo and htmx at their current commits, 5 and 50 test files stop being reported, with no new file or dependency findings; helpers those tests import are now reachable, so their exports get checked.

@webpro webpro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Knip now reads Web Test Runner configs and the test files they run.

@webpro
webpro merged commit b2dd8be into webpro-nl:main Nov 9, 2026
32 checks passed
@webpro

webpro commented Nov 10, 2026

Copy link
Copy Markdown
Member

🚀 This pull request is included in v6.45.0. See Release 6.45.0 for release notes.

Using Knip in a commercial project? Please consider becoming a sponsor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants