Karma TypeScript Preprocessor and Reporter
karma-typescript is a Karma plugin designed to simplify running unit tests for TypeScript projects. It seamlessly integrates TypeScript compilation, type checking, and test execution within the Karma test runner without requiring separate build steps. It also provides remapped test coverage reports via Istanbul, ensuring accurate coverage data even after transpilation. The current stable version is 5.5.4, with recent releases focusing on bug fixes and compatibility updates for newer versions of Karma and TypeScript. Its key differentiators include built-in type checking, automatic source map generation for debugging, and robust remapped coverage reporting, making it a comprehensive solution for testing TypeScript code.
Common errors
-
Error: Cannot find module 'acorn'
cause A dependency issue where the 'acorn' package, used for parsing, is not correctly resolved or installed.fixUpgrade `karma-typescript` to the latest patch release (e.g., `npm install karma-typescript@latest --save-dev`) or explicitly install `acorn`: `npm install acorn --save-dev`. -
TypeError: Cannot read property 'karmaTypescript' of undefined
cause The `karma-typescript` plugin is not correctly recognized or initialized by Karma, often due to missing `frameworks` entry.fixEnsure `karma-typescript` is listed in the `frameworks` array in your `karma.conf.js`: `frameworks: ["jasmine", "karma-typescript"],` -
Coverage threshold not met, exiting with error code 1.
cause The configured code coverage percentage was not reached by the tests, causing the process to terminate. This is a deliberate feature.fixAdjust the coverage thresholds in your `karma.conf.js` under `karmaTypescript.coverageOptions.threshold`, or set `karmaTypescript.coverageOptions.failOnThresholdNotMet: false` if you do not want failures on unmet thresholds. -
TSxxxx: [TypeScript error message]
cause A TypeScript compilation error occurred during the test run, indicating issues in your source or test files.fixReview the accompanying TypeScript error message and fix the code. Ensure your `tsconfig.json` or `karmaTypescript.compilerOptions` are correctly configured for your project.
Warnings
- breaking karma-typescript versions 5.x require Karma 5.x or 6.x. Earlier versions of Karma may not be fully supported, leading to unexpected behavior or compilation issues.
- gotcha Version 5.3.0 of karma-typescript was noted as 'messed up' by the maintainer and was quickly followed by 5.4.0. Users on 5.3.0 might encounter unresolved bugs that were fixed in 5.4.0.
- gotcha Missing or incompatible `acorn` dependency can cause the test run to lock or fail. This has been a recurring bug fix in recent patch versions.
- gotcha When `singleRun` is set to `false`, karma-typescript might still kill the process if coverage thresholds aren't met. This behavior can be unexpected during watch mode.
Install
-
npm install karma-typescript -
yarn add karma-typescript -
pnpm add karma-typescript
Imports
- framework
require('karma-typescript')config.set({ frameworks: ["jasmine", "karma-typescript"], // ... }); - preprocessor
preprocessors: { '**/*.ts': require('karma-typescript') }config.set({ preprocessors: { "**/*.ts": "karma-typescript" // or '*.tsx' for React }, // ... }); - reporter
reporters: ["karma-typescript-reporter"]
config.set({ reporters: ["progress", "karma-typescript"], // ... });
Quickstart
module.exports = function(config) {
config.set({
basePath: './',
frameworks: ["jasmine", "karma-typescript"],
files: [
"src/**/*.ts" // Use '*.tsx' for React projects
],
preprocessors: {
"**/*.ts": "karma-typescript" // Use '*.tsx' for React projects
},
reporters: ["progress", "karma-typescript"],
browsers: ["ChromeHeadless"], // Use ChromeHeadless for CI environments
karmaTypescript: {
compilerOptions: {
esModuleInterop: true,
module: "CommonJS",
target: "ES5"
},
include: ["src/**/*.ts"],
exclude: ["node_modules"]
}
});
};