Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

karma-vite

credred2.1kMIT1.0.5TypeScript support: included

A karma plugin. Transform es module by using vite.

karma-plugin, karma-vite, vite

readme

karma-vite

A karma plugin. Transform es module by using vite.

Installation

npm install -D karma karma-vite

Example

Basic

// karma.conf.js
module.exports = (config) => {
  config.set({
    plugins: ['karma-vite', 'karma-jasmine'],
    frameworks: ['vite', 'jasmine'],
    files: [
      {
        pattern: 'test/**/*.spec.ts',
        type: 'module',
        watched: false,
        served: false,
      },
    ],
  });
};

full solution

Edit on stackblitz

more examples see in github

Configuration

The plugin works out of the box. But you may need to customize some configuration.

// karma.conf.js
module.exports = (config) => {
  config.set({
    vite: {
      /**
       * @description auto config vite middleware
       * @default true
       */
      autoInit: true;
      /**
       * vite server configuration
       * @see https://vitejs.dev/config/
       */
      config: {
      },
      /**
       * @description The config will only take effect after using karma coverage reporter like karma-coverage.
       * see the typescript declaration below for more detail.
       */
      coverage: {
      },
    },
  });
};

Typescript Declaration

export interface KarmaViteConfig {
  /**
   * @description auto config vite middleware
   * @default true
   */
  autoInit?: boolean;
  /**
   * vite server configuration
   * @see https://vitejs.dev/config/
   */
  config: UserConfigExport;
  /**
   * @description The plugin can reporte coverage,
   * but it will only take effect after using karma coverage reporter like karma-coverage
   *
   * power by vite-plugin-istanbul.
   * @see https://github.com/ifaxity/vite-plugin-istanbul
   */
  coverage?: {
    /**
     * By default, it's true if the reporter of karma config contains 'coverage', otherwise false.
     */
    enable?: boolean;
    include?: string | string[];
    exclude?: string | string[];
    extension?: string | string[];
    cwd?: string;
  };
}

changelog

Changelog

1.0.4 (2023-01-11)

Features

  • supports vite4

1.0.3 (2022-10-26)

Fixes

  • viteClientMiddleware always call next function caused an error #2

1.0.2 (2022-10-23)

Features

  • supports vite3

1.0.1 (2022-05-06)

Fixes

  • karma testing never start if urlRoot is default value('/')
  • vite define variable config not working