-
-
Notifications
You must be signed in to change notification settings - Fork 320
Accessing the test runner config in a plugin #2615
-
I have a number of old, legacy side-projects that use:
- webpack for bundling
- Karma as the test runner for testing in real browsers, and
- karma-weback to preprocess source code and test files.
With Karma now officially deprecated I've been looking at alternatives, but many of the modern choices (Jest, Vitest etc.) push you towards emulated DOM environments (jsdom, happy-dom etc.).
Although slower, I place a high value on the confidence I get from running my test suite in the actual browsers that my app runs in. So I'm looking into @web/test-runner as a possible Karma replacement.
As most of these projects are legacy, the current webpack setup does a lot of things that isn't readily or easily transferable to esbuild/rollup (at least not without some migration effort), so I'm looking for an easy way to keep most of the existing setup unchanged and simply swap out the test runner.
I've started exploring the idea of creating a @web/test-runner plugin that is a port of karma-webpack.
(I'm aware this runs counter to the Modern Web ethos of "going buildless" by leveraging browser support for ESM-first testing).
The plan
In essence, the plugin's serverStart() hook would inspect the files pattern(s) in the test runner config, resolve them to a list of actual files, and convert that list of files into webpack "entries" (one per test file); before running the webpack process.
Then, the plugin's serve() hook would intercept requests from the browser for each test file, and respond with the bundled content of the corresponding webpack asset.
Usage
// web-test-runner.config.mjs import { webpackPlugin } from "wtr-webpack-plugin"; export default { files: "**/*.test.js", plugins: [webpackPlugin({ /* any custom webpack config here */ })] }
Issues
The first stumbling block I've hit is that the serverStart() hook receives an argument of type ServerStartParams which contains a config: DevServerCoreConfig property.
This is the @web/dev-server config. What I need though is access to the @web/test-runner config, where the files patterns are specified.
When writing a test runner plugin, is there a way to access the test runner config in the serverStart() hook?
Beta Was this translation helpful? Give feedback.