0
# Test Environment
1
2
Custom JSDOM environment optimized for Angular testing with proper global setup and Angular-specific configurations.
3
4
## Capabilities
5
6
### JestJSDOMEnvironment Class
7
8
Custom Jest environment that extends the abstract JSDOM environment with Angular-specific optimizations and JSDOM integration.
9
10
```typescript { .api }
11
/**
12
* Custom JSDOM environment for Angular testing
13
* Extends Jest's abstract JSDOM environment with Angular optimizations
14
*/
15
class JestJSDOMEnvironment extends BaseEnv {
16
/**
17
* Creates a new JestJSDOMEnvironment instance
18
* @param config - Jest environment configuration
19
* @param context - Environment context from Jest
20
*/
21
constructor(config: JestEnvironmentConfig, context: EnvironmentContext);
22
}
23
24
interface JestEnvironmentConfig {
25
projectConfig: Config.ProjectConfig;
26
globalConfig: Config.GlobalConfig;
27
}
28
29
interface EnvironmentContext {
30
console: Console;
31
docblockPragmas: Record<string, string | string[]>;
32
testPath: string;
33
}
34
35
interface BaseEnv {
36
constructor(config: JestEnvironmentConfig, context: EnvironmentContext, jsdom: any);
37
global: any;
38
moduleMocker: any;
39
fakeTimers: any;
40
setup(): Promise<void>;
41
teardown(): Promise<void>;
42
runScript(script: any): any;
43
}
44
```
45
46
**Usage Examples:**
47
48
```javascript
49
// jest.config.js - Using the custom environment
50
module.exports = {
51
testEnvironment: 'jest-preset-angular/environments/jest-jsdom-env',
52
// other configuration...
53
};
54
55
// Or in preset configuration
56
const { createCjsPreset } = require('jest-preset-angular/presets');
57
58
module.exports = createCjsPreset({
59
testEnvironment: 'jest-preset-angular/environments/jest-jsdom-env'
60
});
61
```
62
63
### Environment Features
64
65
**JSDOM Integration:**
66
- Uses the latest JSDOM version for DOM simulation
67
- Provides browser-like environment for Angular components
68
- Supports DOM manipulation and events
69
70
**Angular Optimizations:**
71
- Pre-configured for Angular testing requirements
72
- Handles Angular's zone.js integration
73
- Supports Angular's change detection in tests
74
75
**Debugging Support:**
76
- Logs JSDOM version information for debugging
77
- Integrates with ts-jest logging system
78
- Provides detailed error reporting
79
80
### Environment Types
81
82
The package provides environment type definitions for configuration:
83
84
```typescript { .api }
85
type JSDOMEnvironment = 'jsdom' | 'jest-preset-angular/environments/jest-jsdom-env';
86
```
87
88
**Available Environments:**
89
- `'jsdom'`: Standard Jest JSDOM environment
90
- `'jest-preset-angular/environments/jest-jsdom-env'`: Custom Angular-optimized environment
91
92
### Configuration in Presets
93
94
The environment is automatically configured when using preset functions:
95
96
```javascript
97
// Default environment (jsdom)
98
const config = createCjsPreset();
99
100
// Custom Angular environment
101
const config = createCjsPreset({
102
testEnvironment: 'jest-preset-angular/environments/jest-jsdom-env'
103
});
104
```
105
106
### Environment Context
107
108
The environment provides proper context for Angular testing:
109
110
**Global Objects:**
111
- `window` object with DOM APIs
112
- `document` object for DOM manipulation
113
- `navigator` object for browser simulation
114
115
**Angular Integration:**
116
- Proper `this` context for component testing
117
- Support for Angular's dependency injection
118
- Integration with Angular testing utilities
119
120
### Browser Simulation
121
122
The environment simulates browser features needed for Angular:
123
124
**DOM APIs:**
125
- Element creation and manipulation
126
- Event handling and propagation
127
- CSS selector queries
128
129
**Browser Features:**
130
- LocalStorage and SessionStorage
131
- History API for routing tests
132
- Fetch API for HTTP testing
133
134
**Performance:**
135
- Optimized for test execution speed
136
- Minimal overhead for simple tests
137
- Efficient memory usage
138
139
### Compatibility
140
141
The environment is compatible with:
142
143
**Angular Versions:**
144
- Angular 18+
145
- Angular 19+
146
- Angular 20+
147
148
**Node.js Versions:**
149
- Node.js 18.19.1+
150
- Node.js 20.11.1+
151
- Node.js 22.0.0+
152
153
**Jest Versions:**
154
- Jest 30.0.0+
155
156
### Environment Setup
157
158
The environment automatically handles setup required for Angular testing without additional configuration:
159
160
```typescript
161
// No manual setup required - environment handles:
162
// - JSDOM initialization
163
// - Angular testing utilities setup
164
// - Zone.js configuration
165
// - Error handling setup
166
```