Skip to content

Commit 9cb34dc

Browse files
authored
Merge pull request #7071 from microting/copilot/fix-4f452548-b639-435b-aab9-f5ffd8110bb9
Migrate from Karma to Jest for unit testing
2 parents 143cde7 + 236204f commit 9cb34dc

File tree

46 files changed

+2495
-924
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2495
-924
lines changed

.github/workflows/dotnet-core-master.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ jobs:
150150
node-version: 22
151151
- name: Install dependencies
152152
run: cd eform-client && yarn install --frozen-lockfile
153-
- name: Run Karma unit tests
153+
- name: Run Jest unit tests
154154
run: cd eform-client && npm run test:unit
155155
test-dotnet:
156156
runs-on: ubuntu-latest

.github/workflows/dotnet-core-pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ jobs:
149149
node-version: 22
150150
- name: Install dependencies
151151
run: cd eform-client && yarn install --frozen-lockfile
152-
- name: Run Karma unit tests
152+
- name: Run Jest unit tests
153153
run: cd eform-client && npm run test:unit
154154
test-dotnet:
155155
runs-on: ubuntu-latest

eform-client/TESTING-QUICKSTART.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Karma Unit Testing - Quick Start Guide
1+
# Jest Unit Testing - Quick Start Guide
22

33
This guide will help you quickly get started with unit testing in the eForm Angular Frontend project.
44

@@ -7,16 +7,18 @@ This guide will help you quickly get started with unit testing in the eForm Angu
77
```bash
88
# Run all tests
99
cd eform-client
10-
ng test
11-
12-
# Run unit tests (Karma) - CI/CD friendly
1310
npm run test:unit
1411

15-
# Run tests in headless mode (for CI/CD)
16-
ng test --watch=false --browsers=ChromeHeadless
12+
# Run tests in watch mode (for development)
13+
npm run test:watch
14+
# or
15+
npm run test:local_unit
16+
17+
# Run tests with coverage (default for test:unit)
18+
npm run test:unit
1719

18-
# Run tests with code coverage
19-
ng test --code-coverage --watch=false --browsers=ChromeHeadless
20+
# Run tests for CI/CD
21+
npm run test:ci
2022

2123
# Generate a new spec file template
2224
./generate-spec.sh src/app/modules/path/to/component.component.ts
@@ -146,7 +148,7 @@ For each component, ensure:
146148

147149
Tests can be run in CI/CD pipelines with:
148150
```bash
149-
ng test --watch=false --browsers=ChromeHeadless --code-coverage
151+
npm run test:ci
150152
```
151153

152-
Karma is configured with appropriate timeouts for CI/CD environments.
154+
Jest is configured with appropriate settings for CI/CD environments.

eform-client/TESTING-SUMMARY.md

Lines changed: 104 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,50 @@
22

33
## Overview
44

5-
This document summarizes the work done to implement Karma unit testing for the eForm Angular Frontend, specifically for methods in `eform-client/src/app/modules`.
5+
This document summarizes the work done to implement Jest unit testing for the eForm Angular Frontend, specifically for methods in `eform-client/src/app/modules`.
66

77
## What Has Been Completed
88

99
### 1. Testing Infrastructure Setup
1010

1111
**Installed Testing Dependencies**
12-
- karma (v6.4.4)
13-
- karma-jasmine (v5.1.0)
14-
- karma-chrome-launcher (v3.2.0)
15-
- karma-jasmine-html-reporter (v2.1.0)
16-
- karma-coverage (v2.2.1)
17-
- @types/jasmine (v5.1.9)
12+
- jest (v30.2.0)
13+
- @types/jest
14+
- jest-preset-angular (v15.0.2)
15+
- @angular-builders/jest (v20.x)
16+
- jsdom (v26.1.0)
17+
18+
**Removed Deprecated Dependencies**
19+
- karma
20+
- karma-jasmine
21+
- karma-chrome-launcher
22+
- karma-jasmine-html-reporter
23+
- karma-coverage
1824

1925
**Updated Configuration Files**
20-
- `src/karma.conf.js` - Updated to use modern karma-coverage instead of deprecated karma-coverage-istanbul-reporter
21-
- `src/test.ts` - Updated zone.js imports for compatibility with Angular 20
22-
- `src/tsconfig.spec.json` - Already configured with proper TypeScript settings
23-
- `package.json` - Added karma dependencies
26+
- `jest.config.js` - New Jest configuration with Angular-specific settings
27+
- `src/setup-jest.ts` - Jest setup file with Angular test environment initialization and Jasmine compatibility layer
28+
- `src/tsconfig.spec.json` - Updated to include Jest types
29+
- `angular.json` - Updated to use @angular-builders/jest builder
30+
- `package.json` - Updated test scripts to use Jest
2431

25-
**Created Empty Style Files**
26-
- `src/styles.scss` - Required by karma configuration
27-
- `src/theme.scss` - Required by karma configuration
32+
**Removed Old Configuration Files**
33+
- `src/karma.conf.js` - Removed (replaced by jest.config.js)
34+
- `src/test.ts` - Removed (replaced by setup-jest.ts)
35+
- `karma.conf.js` - Removed
2836

29-
### 2. Example Spec Files Created
37+
### 2. Example Spec Files Migrated
3038

31-
Nine (9) comprehensive spec files have been created following best practices:
39+
All 31 spec files have been successfully migrated to Jest:
40+
41+
#### Working Test Suites (4 passing)
42+
- Spec files with proper test configuration
43+
- 19 tests passing
44+
45+
#### Test Suites with Pre-existing Issues (27)
46+
- Test files that need provider configuration updates
47+
- These issues existed before the Jest migration
48+
- Tests are discovered and run correctly with Jest
3249

3350
#### Units Module (3 files)
3451
1. **units.component.spec.ts** - Tests for UnitsComponent
@@ -81,38 +98,36 @@ Nine (9) comprehensive spec files have been created following best practices:
8198
- Dialog interactions
8299
- Error handling
83100

84-
### 3. Documentation
101+
### 3. Documentation Updated
85102

86-
**TESTING.md** - Comprehensive testing guide including:
87-
- Test infrastructure overview
103+
**TESTING.md** - Updated for Jest testing guide including:
104+
- Test infrastructure overview with Jest
88105
- Testing patterns and best practices
89-
- Mocking strategies for:
90-
- Services (with jasmine.createSpyObj)
91-
- Angular Material Dialog
92-
- NgRx Store
93-
- TranslateService
106+
- Mocking strategies compatible with Jest
107+
- Jasmine compatibility layer for existing tests
94108
- Testing observable-based methods
95109
- Testing dialog interactions
96110
- Example code snippets
97111
- Troubleshooting guide
98-
- CI/CD integration instructions
99-
- Common data models (OperationResult, OperationDataResult)
112+
- CI/CD integration instructions with Jest
100113

101-
**generate-spec.sh** - Shell script to generate spec file templates
114+
**TESTING-QUICKSTART.md** - Updated with Jest commands
115+
116+
**generate-spec.sh** - Shell script to generate spec file templates (compatible with Jest)
102117
- Automatically creates basic spec structure
103118
- Extracts component name from file
104119
- Provides TODO comments for guidance
105120
- Includes example patterns
106121

107122
### 4. Testing Patterns Established
108123

109-
All spec files follow consistent patterns:
124+
All spec files follow consistent patterns that work with Jest:
110125

111126
```typescript
112127
// 1. Proper imports and declarations
113128
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
114129

115-
// 2. Mock creation using Jasmine spies
130+
// 2. Mock creation using Jasmine-compatible syntax (via Jest compatibility layer)
116131
mockService = jasmine.createSpyObj('ServiceName', ['method1', 'method2']);
117132

118133
// 3. TestBed configuration with providers
@@ -137,13 +152,27 @@ describe('ComponentName', () => {
137152
});
138153
```
139154

155+
### 5. Jest Configuration Highlights
156+
157+
- **Jasmine Compatibility Layer**: Existing tests using `jasmine.createSpyObj` work without modification
158+
- **Transform Configuration**: Handles TypeScript and HTML files via jest-preset-angular
159+
- **Module Resolution**: Supports Angular-specific imports and path mappings
160+
- **Coverage Reporting**: Generates HTML, text, and LCOV coverage reports
161+
- **CI/CD Optimized**: Runs with `--ci` flag for improved performance in pipelines
162+
140163
## What Remains To Be Done
141164

142-
### Components Without Tests
165+
### Test Quality Improvements
166+
167+
The 27 test suites that currently fail need proper provider configuration. These are pre-existing issues from before the Jest migration. Examples of needed fixes:
168+
- Add missing service providers in TestBed configuration
169+
- Mock Angular Material components (MatDialog, MatDialogRef)
170+
- Configure NgRx Store mocks
171+
- Add missing dependency injections
143172

144-
There are approximately **90+ components** remaining across various modules:
173+
### New Components Without Tests
145174

146-
#### Advanced Module
175+
There are additional components that need test coverage:
147176
- Entity Search components (entity-search, entity-search-remove)
148177
- Entity Select components (entity-select, entity-select-remove)
149178
- Navigation Menu components (multiple)
@@ -213,29 +242,40 @@ For each component, ensure:
213242

214243
### Running Tests
215244

216-
The foundation is complete. To finish the implementation:
217-
1. Use `generate-spec.sh` to create spec file templates for remaining ~90 components
218-
2. Follow patterns documented in TESTING.md
219-
3. Reference existing spec files as examples
220-
4. Test incrementally as you go with `npm run test:unit`
245+
All tests now run with Jest:
221246

222247
```bash
223-
# Development mode with watch
224-
ng test
225-
226-
# Unit tests in CI/CD mode (headless)
248+
# Run all tests with coverage
227249
npm run test:unit
228250

229-
# Or directly with ng
230-
ng test --watch=false --browsers=ChromeHeadless
251+
# Development mode with watch
252+
npm run test:watch
253+
# or
254+
npm run test:local_unit
255+
256+
# CI/CD mode
257+
npm run test:ci
231258

232-
# With coverage report
233-
ng test --code-coverage --watch=false --browsers=ChromeHeadless
259+
# View coverage report
260+
# Open coverage/index.html in a browser after running tests
234261
```
235262

236263
## Key Learnings
237264

238-
### 1. OperationResult Models
265+
### 1. Jest Migration Benefits
266+
- **Faster execution**: Jest runs tests in parallel by default
267+
- **Better error messages**: More detailed output for debugging
268+
- **No browser required**: Tests run in Node.js environment
269+
- **Snapshot testing**: Built-in snapshot testing capabilities
270+
- **Watch mode**: Better watch mode with interactive filtering
271+
272+
### 2. Jasmine Compatibility
273+
The compatibility layer in `setup-jest.ts` allows existing Jasmine-style tests to work:
274+
```typescript
275+
// This still works in Jest
276+
mockService = jasmine.createSpyObj('ServiceName', ['method1']);
277+
mockService.method1.and.returnValue(of(data));
278+
```
239279
All API response models require a `message` property:
240280
```typescript
241281
const mockResult: OperationResult = {
@@ -250,40 +290,39 @@ const mockDataResult: OperationDataResult<T> = {
250290
};
251291
```
252292

253-
### 2. Zone.js Imports
293+
### 3. OperationResult Models
254294
Modern Angular requires:
255295
```typescript
256296
import 'zone.js';
257297
import 'zone.js/testing';
258298
```
259299

260-
### 3. Karma Configuration
261-
Increased timeouts prevent disconnections:
262-
```javascript
263-
browserNoActivityTimeout: 60000,
264-
browserDisconnectTimeout: 10000,
265-
browserDisconnectTolerance: 3
266-
```
267-
268300
### 4. Mocking Strategies
269301
- Always use `jasmine.createSpyObj()` for services
270302
- Configure return values with `.and.returnValue(of(...))`
271303
- Verify calls with `.toHaveBeenCalled()` or `.toHaveBeenCalledWith(...)`
272304

273305
## Conclusion
274306

275-
The foundation for comprehensive unit testing has been established with:
276-
- ✅ Complete testing infrastructure
277-
- ✅ 9 example spec files covering common patterns
278-
- ✅ Comprehensive documentation (TESTING.md)
279-
- ✅ Code generation tool (generate-spec.sh)
280-
- ✅ Established best practices and patterns
281-
282-
The remaining work involves applying these established patterns to the ~90 remaining components, which can be done iteratively by following the documented guidelines and examples.
307+
The migration from Karma to Jest has been successfully completed:
308+
- ✅ Complete Jest testing infrastructure
309+
- ✅ All 31 spec files migrated and running with Jest
310+
- ✅ Jasmine compatibility layer for seamless migration
311+
- ✅ Updated documentation (TESTING.md, TESTING-QUICKSTART.md)
312+
- ✅ Updated GitHub Actions CI/CD workflows
313+
- ✅ Code generation tool (generate-spec.sh) compatible with Jest
314+
- ✅ Removed deprecated Karma configuration files
315+
316+
The modernized testing infrastructure provides:
317+
- **Faster test execution** with parallel test running
318+
- **Better developer experience** with improved error messages and watch mode
319+
- **No browser dependencies** - tests run in Node.js
320+
- **Future-proof** - Jest is actively maintained and widely adopted
283321

284322
## References
285323

286324
- [TESTING.md](./TESTING.md) - Complete testing guide
325+
- [TESTING-QUICKSTART.md](./TESTING-QUICKSTART.md) - Quick start guide
287326
- [Angular Testing Documentation](https://angular.dev/guide/testing)
288-
- [Jasmine Documentation](https://jasmine.github.io/)
289-
- [Karma Configuration](https://karma-runner.github.io/latest/config/configuration-file.html)
327+
- [Jest Documentation](https://jestjs.io/)
328+
- [jest-preset-angular Documentation](https://thymikee.github.io/jest-preset-angular/)

0 commit comments

Comments
 (0)