Registry / testing / karma-static-server

karma-static-server

JSON →
library2.0.0jsnpmunverified

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-server
INSTALL
IMPORT
SIG · KARMA-STATIC-SERVE
K
karma-static-server
testingjavascriptv2.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

staticServer
// In karma.conf.js module.exports = function(config) { config.set({ middleware: ['staticServer'], plugins: [ 'karma-*', require('karma-static-server') ], // ... other config }); };
import { staticServer } from 'karma-static-server';
Karma plugins are configured via string names in `karma.conf.js`, not direct JavaScript imports for runtime. The `require()` call is often implicitly handled by Karma if `plugins` array contains 'karma-*'.
urlRoot
// In karma.conf.js module.exports = function(config) { config.set({ urlRoot: '/test/', // Recommended to avoid conflicts // ... other config }); };
config.set({ rootUrl: '/' });
This is a core Karma configuration option, not specific to `karma-static-server`, but crucial for its correct operation. Setting it to a non-root path prevents Karma's own test runner from conflicting with the static server at the root URL.
staticServer options
// In karma.conf.js module.exports = function(config) { config.set({ staticServer: { root: require('path').join(__dirname, 'public'), log: true }, // ... other config }); };
config.set({ serverOptions: { root: './public' } });
Configuration for `karma-static-server` is done via the `staticServer` property in the main Karma config object. Options like `root` and `log` are passed directly to the underlying `serve-static` middleware, with `root` defaulting to Karma's `basePath`.

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.

const path = require('path'); module.exports = function(config) { config.set({ basePath: './', frameworks: ['jasmine'], files: [ // Your test files 'test/**/*.spec.js', // Files to be served statically, but not included in test runner { pattern: 'public/**/*.*', served: true, watched: false, included: false }, { pattern: 'assets/**/*.*', served: true, watched: false, included: false } ], exclude: [], // Enable the static server middleware middleware: ['staticServer'], // Explicitly add the plugin if you're not using 'karma-*' plugins: [ 'karma-jasmine', 'karma-chrome-launcher', require('karma-static-server') ], // Configure the static server staticServer: { root: path.join(__dirname, 'public'), // Serve files from './public' relative to karma.conf.js log: true // Log static file requests to Karma's console }, // Important: Change Karma's URL root to avoid conflicts with static server urlRoot: '/__karma__/', port: 9876, colors: true, logLevel: config.LOG_INFO, autoWatch: true, browsers: ['Chrome'], singleRun: false, concurrency: Infinity }); };
Debug
Known issues
deprecatedThe upstream Karma test runner itself has been officially deprecated since July 2024. While critical security fixes for Karma may continue for a period, active development, new features, or general bug fixes are no longer being accepted. Users should consider migrating to alternative test runners.
fix
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.
affects: >=2.0.0
gotchaFailing to configure Karma's `urlRoot` can lead to conflicts where the static server attempts to handle requests intended for Karma's internal assets or test runner, resulting in unexpected behavior or 404 errors for test files.
fix
Always set `urlRoot` in your `karma.conf.js` to a non-root path (e.g., `/__karma__/`) when using `karma-static-server`.
affects: >=1.0.0
gotchaBy default, `karma-static-server` serves files relative to Karma's `basePath`. If you need to serve files from a different location, you must explicitly define the `root` option within the `staticServer` configuration block. Forgetting this can lead to files not being found.
fix
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.
affects: >=1.0.0
gotchaWhen defining files to be served statically, ensure they are configured with `served: true`, `included: false`, and often `watched: false` in the Karma `files` array. If `included: true`, Karma will attempt to inject these files into the test runner's HTML page, which is usually not desired for static assets like JSON or images.
fix
For static assets, use `{ pattern: 'path/to/asset.json', served: true, included: false, watched: false }` to prevent Karma from loading them as test dependencies.
affects: >=1.0.0
Errors
Common errors & fixes
GET /your-asset.json 404 (Not Found)
The static server is either not enabled, incorrectly configured, or the requested file is not within its configured `root` directory.
fix
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.
Error: Cannot find module 'karma-static-server' (or similar plugin loading error)
The `karma-static-server` package is not correctly installed or not included in Karma's `plugins` array.
fix
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.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies
karmarequiredPeer dependency as it's a Karma plugin. The plugin integrates with the Karma test runner's middleware system.
serve-staticrequiredInternal dependency for serving static files. The plugin wraps this middleware.
Agent activity
4 hits · last 30 days
node
4
Resources
karma-static-server — npm install karma-static-server · libregistry