Karma Testing Skill
Core Patterns
karma.conf.js
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
'src/**/*.js',
'test/**/*.spec.js'
],
preprocessors: {
'src/**/*.js': ['coverage']
},
reporters: ['progress', 'coverage'],
coverageReporter: {
type: 'html',
dir: 'coverage/'
},
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome', 'Firefox'],
singleRun: false,
concurrency: Infinity,
// LambdaTest cloud browsers
customLaunchers: {
ChromeLT: {
base: 'WebDriver',
config: {
hostname: 'hub.lambdatest.com',
port: 80
},
browserName: 'Chrome',
version: 'latest',
name: 'Karma Test',
tunnel: true,
user: process.env.LT_USERNAME,
accessKey: process.env.LT_ACCESS_KEY
}
}
});
};
Test with Jasmine Framework
describe('StringUtils', () => {
it('should capitalize first letter', () => {
expect(StringUtils.capitalize('hello')).toBe('Hello');
});
it('should handle empty string', () => {
expect(StringUtils.capitalize('')).toBe('');
});
});
Angular Integration
// karma.conf.js for Angular
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-coverage'),
require('@angular-devkit/build-angular/plugins/karma')
],
Setup: npm install karma karma-jasmine karma-chrome-launcher karma-coverage --save-dev
Run: npx karma start or npx karma start --single-run
Init: npx karma init karma.conf.js
Deep Patterns
See reference/playbook.md for production-grade patterns:
| Section |
What You Get |
| §1 Production Configuration |
Full karma.conf.js with coverage thresholds, reporters, CI launchers |
| §2 Component Testing |
Service mocking, DOM interaction, form validation patterns |
| §3 HTTP Service Testing |
HttpTestingController, error handling, retry testing |
| §4 Directive & Pipe Testing |
Host component pattern, custom pipes with edge cases |
| §5 RxJS & Async Patterns |
debounceTime, switchMap cancellation, subscription cleanup |
| §6 Router & NgRx Testing |
RouterTestingModule, MockStore, selector overrides |
| §7 LambdaTest Integration |
Cloud browser configuration for cross-browser testing |
| §8 CI/CD Integration |
GitHub Actions with coverage, test reporting |
| §9 Debugging Table |
12 common problems with causes and fixes |
| §10 Best Practices |
14-item checklist for production Angular testing |
1---
2name: karma-skill
3description: >
4 Generates Karma test runner configurations for browser-based JavaScript testing.
5 Works with Jasmine, Mocha, or QUnit. Use when user mentions "Karma",
6 "karma.conf.js", "browser test runner". Triggers on: "Karma", "karma.conf",
7 "karma test runner", "browser-based JS test".
8languages:
9 - JavaScript
10 - TypeScript
11category: unit-testing
12license: MIT
13metadata:
14 source: https://skillmd.com/skills/lambdatest/karma-skill
15 author: TestMu AI
16 version: "1.0"
17---
18
19# Karma Testing Skill
20
21## Core Patterns
22
23### karma.conf.js
24
25```javascript
26module.exports = function(config) {
27 config.set({
28 basePath: '',
29 frameworks: ['jasmine'],
30 files: [
31 'src/**/*.js',
32 'test/**/*.spec.js'
33 ],
34 preprocessors: {
35 'src/**/*.js': ['coverage']
36 },
37 reporters: ['progress', 'coverage'],
38 coverageReporter: {
39 type: 'html',
40 dir: 'coverage/'
41 },
42 port: 9876,
43 colors: true,
44 logLevel: config.LOG_INFO,
45 autoWatch: true,
46 browsers: ['Chrome', 'Firefox'],
47 singleRun: false,
48 concurrency: Infinity,
49
50 // LambdaTest cloud browsers
51 customLaunchers: {
52 ChromeLT: {
53 base: 'WebDriver',
54 config: {
55 hostname: 'hub.lambdatest.com',
56 port: 80
57 },
58 browserName: 'Chrome',
59 version: 'latest',
60 name: 'Karma Test',
61 tunnel: true,
62 user: process.env.LT_USERNAME,
63 accessKey: process.env.LT_ACCESS_KEY
64 }
65 }
66 });
67};
68```
69
70### Test with Jasmine Framework
71
72```javascript
73describe('StringUtils', () => {
74 it('should capitalize first letter', () => {
75 expect(StringUtils.capitalize('hello')).toBe('Hello');
76 });
77
78 it('should handle empty string', () => {
79 expect(StringUtils.capitalize('')).toBe('');
80 });
81});
82```
83
84### Angular Integration
85
86```javascript
87// karma.conf.js for Angular
88frameworks: ['jasmine', '@angular-devkit/build-angular'],
89plugins: [
90 require('karma-jasmine'),
91 require('karma-chrome-launcher'),
92 require('karma-coverage'),
93 require('@angular-devkit/build-angular/plugins/karma')
94],
95```
96
97## Setup: `npm install karma karma-jasmine karma-chrome-launcher karma-coverage --save-dev`
98## Run: `npx karma start` or `npx karma start --single-run`
99## Init: `npx karma init karma.conf.js`
100
101## Deep Patterns
102
103See `reference/playbook.md` for production-grade patterns:
104
105| Section | What You Get |
106|---------|-------------|
107| §1 Production Configuration | Full karma.conf.js with coverage thresholds, reporters, CI launchers |
108| §2 Component Testing | Service mocking, DOM interaction, form validation patterns |
109| §3 HTTP Service Testing | HttpTestingController, error handling, retry testing |
110| §4 Directive & Pipe Testing | Host component pattern, custom pipes with edge cases |
111| §5 RxJS & Async Patterns | debounceTime, switchMap cancellation, subscription cleanup |
112| §6 Router & NgRx Testing | RouterTestingModule, MockStore, selector overrides |
113| §7 LambdaTest Integration | Cloud browser configuration for cross-browser testing |
114| §8 CI/CD Integration | GitHub Actions with coverage, test reporting |
115| §9 Debugging Table | 12 common problems with causes and fixes |
116| §10 Best Practices | 14-item checklist for production Angular testing |