{"id":13418,"library":"karma-static-server","title":"Karma Static Server Plugin","description":"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.","status":"abandoned","version":"2.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/brandonocasey/karma-static-server","tags":["javascript"],"install":[{"cmd":"npm install karma-static-server","lang":"bash","label":"npm"},{"cmd":"yarn add karma-static-server","lang":"bash","label":"yarn"},{"cmd":"pnpm add karma-static-server","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency as it's a Karma plugin. The plugin integrates with the Karma test runner's middleware system.","package":"karma","optional":false},{"reason":"Internal dependency for serving static files. The plugin wraps this middleware.","package":"serve-static","optional":false}],"imports":[{"note":"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-*'.","wrong":"import { staticServer } from 'karma-static-server';","symbol":"staticServer","correct":"// In karma.conf.js\nmodule.exports = function(config) {\n  config.set({\n    middleware: ['staticServer'],\n    plugins: [\n      'karma-*',\n      require('karma-static-server')\n    ],\n    // ... other config\n  });\n};"},{"note":"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.","wrong":"config.set({ rootUrl: '/' });","symbol":"urlRoot","correct":"// In karma.conf.js\nmodule.exports = function(config) {\n  config.set({\n    urlRoot: '/test/', // Recommended to avoid conflicts\n    // ... other config\n  });\n};"},{"note":"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`.","wrong":"config.set({ serverOptions: { root: './public' } });","symbol":"staticServer options","correct":"// In karma.conf.js\nmodule.exports = function(config) {\n  config.set({\n    staticServer: {\n      root: require('path').join(__dirname, 'public'),\n      log: true\n    },\n    // ... other config\n  });\n};"}],"quickstart":{"code":"const path = require('path');\n\nmodule.exports = function(config) {\n  config.set({\n    basePath: './',\n    frameworks: ['jasmine'],\n    files: [\n      // Your test files\n      'test/**/*.spec.js',\n      // Files to be served statically, but not included in test runner\n      { pattern: 'public/**/*.*', served: true, watched: false, included: false },\n      { pattern: 'assets/**/*.*', served: true, watched: false, included: false }\n    ],\n    exclude: [],\n\n    // Enable the static server middleware\n    middleware: ['staticServer'],\n\n    // Explicitly add the plugin if you're not using 'karma-*'\n    plugins: [\n      'karma-jasmine',\n      'karma-chrome-launcher',\n      require('karma-static-server')\n    ],\n\n    // Configure the static server\n    staticServer: {\n      root: path.join(__dirname, 'public'), // Serve files from './public' relative to karma.conf.js\n      log: true // Log static file requests to Karma's console\n    },\n\n    // Important: Change Karma's URL root to avoid conflicts with static server\n    urlRoot: '/__karma__/',\n\n    port: 9876,\n    colors: true,\n    logLevel: config.LOG_INFO,\n    autoWatch: true,\n    browsers: ['Chrome'],\n    singleRun: false,\n    concurrency: Infinity\n  });\n};","lang":"javascript","description":"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."},"warnings":[{"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.","message":"The 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.","severity":"deprecated","affected_versions":">=2.0.0"},{"fix":"Always set `urlRoot` in your `karma.conf.js` to a non-root path (e.g., `/__karma__/`) when using `karma-static-server`.","message":"Failing 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"By 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"When 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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","cause":"The static server is either not enabled, incorrectly configured, or the requested file is not within its configured `root` directory.","error":"GET /your-asset.json 404 (Not Found)"},{"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.","cause":"The `karma-static-server` package is not correctly installed or not included in Karma's `plugins` array.","error":"Error: Cannot find module 'karma-static-server' (or similar plugin loading error)"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null}