The `karma-static-server` package is a plugin for the Karma test runner, enabling it to serve static files alongside its primary function of running tests in browsers. It is currently at stable version 2.0.0, which was last published approximately four years ago, indicating a slow or halted release cadence. This utility integrates the `serve-static` middleware directly into the Karma server, providing a convenient way to serve application assets such as HTML templates, images, CSS, or JSON data that tests might depend on, without requiring a separate web server. Its key differentiator lies in its seamless configuration within the `karma.conf.js` file, allowing developers to define a custom `root` directory for served files or default to Karma's `basePath`. It also provides options for logging static file requests through Karma's standard logging mechanism. Given that Karma itself has been officially deprecated since July 2024, this plugin is effectively in an unmaintained state, with no new features or general bug fixes anticipated.
npm install karma-static-serverVerified import paths — ran on the pinned version, not inferred.
This configuration demonstrates how to enable and configure `karma-static-server` in a `karma.conf.js` file. It sets up the static server to serve content from a 'public' directory, logs requests, and adjusts Karma's `urlRoot` to prevent URL conflicts, which is a crucial step for proper operation. It also includes patterns for files to be served statically without being included in the test runner.
Evaluate migration to modern browser-based test runners like Web Test Runner, jasmine-browser-runner, or Node-based alternatives like Jest/Vitest for projects that previously relied on Karma.
Always set `urlRoot` in your `karma.conf.js` to a non-root path (e.g., `/__karma__/`) when using `karma-static-server`.
If your static files are not in Karma's `basePath`, add `staticServer: { root: '/path/to/your/static/files' }` to your `karma.conf.js`. Use `path.join(__dirname, 'your-folder')` for robust path resolution.For static assets, use `{ pattern: 'path/to/asset.json', served: true, included: false, watched: false }` to prevent Karma from loading them as test dependencies.Verify that `staticServer` is included in the `middleware` array in `karma.conf.js`. Check the `staticServer.root` path to ensure it correctly points to the directory containing your static assets. Also, ensure `urlRoot` is set correctly.
Run `npm install --save-dev karma-static-server`. If manually specifying `plugins`, ensure `require('karma-static-server')` is included in the `plugins` array in `karma.conf.js`, or verify that `karma-*` is present if relying on automatic loading.