karma-esbuild is a specialized preprocessor for the Karma test runner designed to leverage esbuild for rapid bundling and transpilation of test files. It is particularly valued for its speed and the clear, unminified output it produces during testing, making debugging easier compared to preprocessors using slower bundlers. The package is currently at version 2.3.0 and exhibits an active release cadence, frequently updating to maintain compatibility with new esbuild versions (e.g., v2.3.0 added support for esbuild 0.17.0) and addressing bug fixes, especially concerning source maps and file handling. Its primary differentiation from alternatives like karma-webpack or karma-rollup-preprocessor is esbuild's performance. Users can extensively configure esbuild options, including define directives, custom plugins, and the singleBundle option, which controls whether all test files are merged into one bundle or processed individually. This flexibility supports various testing setups and optimization strategies within the Karma ecosystem.
npm install karma-esbuildVerified import paths — ran on the pinned version, not inferred.
Demonstrates a basic Karma configuration file (`karma.conf.js`) integrating `karma-esbuild` as a preprocessor for JavaScript test files, including optional custom `esbuild` settings like `define` variables and `singleBundle`.
Ensure your project's `esbuild` dependency is updated to `0.17.0` or higher to match the peer dependency requirement. Run `npm install esbuild@latest` or `yarn add esbuild@latest`.
Ensure you are on the latest `karma-esbuild` patch release within the `2.x` series. Test your source maps thoroughly, especially for TypeScript files, and report any lingering issues.
Consider using `babel-plugin-istanbul` with a Babel step before `esbuild` or rely on `esbuild`'s own capabilities if they evolve. The `COVERAGE` define option might be a starting point for conditional instrumentation.
Remove `karma-sourcemap-loader` from your `karma.conf.js` if it's still present, as it is no longer needed and can cause conflicts. Ensure your `esbuild` configuration correctly generates source maps.
Run `npm install --save-dev esbuild` or `yarn add --dev esbuild`.
Verify `esbuild` options related to source maps. Ensure `esbuild.sourcemap` is set correctly (e.g., `true` or `'inline'`). Update `karma-esbuild` to the latest patch release for source map bug fixes (e.g., `v2.2.2`, `v2.2.3`).
Check the `preprocessors` section in your `karma.conf.js` to ensure the glob pattern (e.g., `'test/**/*.test.js'`) correctly matches your test file paths.
Ensure `npm install --save-dev karma-esbuild` was successful and that `'esbuild'` is correctly listed in the `preprocessors` array in `karma.conf.js`. Check `node_modules` for `karma-esbuild`.