karma-esbuild-next

raw JSON →
1.1.0 verified Fri May 01 auth: no javascript

An esbuild preprocessor for the Karma test runner, forked from karma-esbuild. It leverages esbuild for fast compilation and readable output, supporting custom esbuild configurations, plugin integration, and single-bundle mode to merge test files into one bundle. Current version 1.1.0 requires esbuild >=0.17.0. Compared to alternatives like karma-webpack or karma-rollup, it offers significantly faster build times due to esbuild's speed, while maintaining compatibility with Karma's standard preprocessor interface.

error Error: No provider for "framework:esbuild"! (Resolving: esbuild)
cause Missing 'esbuild' in preprocessors array incorrectly, or plugin not installed
fix
Ensure you have added 'karma-esbuild-next' to plugins array in karma.conf.js
error Error: Cannot find module 'karma-esbuild-next'
cause Package not installed or not listed in package.json devDependencies
fix
Run 'npm install --save-dev karma-esbuild-next'
error Error: esbuild: Unexpected option: bundle
cause Using esbuild 'bundle' option inside esbuild config block in karma.conf.js
fix
Use 'singleBundle' instead of 'bundle' for Karma-specific bundling
gotcha Preprocessor name must be 'esbuild', not the package name
fix Use 'esbuild' in preprocessors array, not 'karma-esbuild-next'
breaking Requires esbuild >=0.17.0; older versions may fail
fix Update esbuild to version 0.17.0 or later
gotcha singleBundle defaults to true; all test files are bundled into one. This can cause issues if tests rely on separate scopes for globals.
fix Set singleBundle: false if you need separate bundles per test file
deprecated The original karma-esbuild package is unmaintained; this fork is the recommended alternative
fix Switch to karma-esbuild-next
npm install karma-esbuild-next
yarn add karma-esbuild-next
pnpm add karma-esbuild-next

Configure Karma to use esbuild preprocessor with Mocha, a define variable, and single bundle mode.

// karma.conf.js
module.exports = function (config) {
  config.set({
    frameworks: ['mocha'],
    files: ['test/**/*.test.js'],
    preprocessors: {
      'test/**/*.test.js': ['esbuild']
    },
    plugins: [
      'karma-esbuild-next'
    ],
    esbuild: {
      define: {
        'process.env.NODE_ENV': JSON.stringify('test')
      },
      singleBundle: true
    },
    browsers: ['ChromeHeadless'],
    singleRun: true
  });
};