Jest preset configuration for Angular projects
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Functions for setting up Angular testing environments with proper TestBed configuration and zone handling for different Angular testing scenarios.
Sets up Angular testing environment with Zone.js support for traditional Angular applications.
/**
* Sets up Angular testing environment with Zone.js support
* @param options - Optional TestBed environment configuration options
*/
function setupZoneTestEnv(options?: TestEnvironmentOptions): void;
interface TestEnvironmentOptions {
[key: string]: any;
}Path: 'jest-preset-angular/setup-env/zone'
Usage Examples:
// setup-jest.ts
import 'jest-preset-angular/setup-env/zone';
// Or with custom options
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone';
// Basic setup (called automatically when importing)
// No manual call needed when using import statement
// Manual setup with options
setupZoneTestEnv({
// Custom TestBed configuration options
});Configuration:
// jest.config.js
module.exports = {
preset: 'jest-preset-angular',
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts']
};Features:
Sets up Angular testing environment for zoneless Angular applications (Angular 18+ experimental feature).
/**
* Sets up Angular testing environment without Zone.js for zoneless applications
* @param options - Optional TestBed environment configuration options
*/
function setupZonelessTestEnv(options?: TestEnvironmentOptions): void;Path: 'jest-preset-angular/setup-env/zoneless'
Usage Examples:
// setup-jest.ts for zoneless Angular app
import 'jest-preset-angular/setup-env/zoneless';
// Or with custom options
import { setupZonelessTestEnv } from 'jest-preset-angular/setup-env/zoneless';
// Manual setup with options
setupZonelessTestEnv({
// Custom TestBed configuration options for zoneless environment
});Configuration:
// jest.config.js for zoneless Angular app
module.exports = {
preset: 'jest-preset-angular',
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts']
};Features:
Shared utilities used by both zone and zoneless setup functions.
/**
* Applies text encoding polyfills for Node.js environment compatibility
*/
function polyfillEncoder(): void;
/**
* Resolves and validates test environment options
* @param options - Raw test environment options
* @returns Processed test environment options
*/
function resolveTestEnvOptions(options?: TestEnvironmentOptions): TestEnvironmentOptions;Internal Usage: These utilities are automatically called by the setup functions and typically don't need direct usage:
// Used internally by setup functions
polyfillEncoder(); // Ensures TextEncoder/TextDecoder are available
const processedOptions = resolveTestEnvOptions(userOptions);The setup environment functions support global configuration through the ngJest global variable.
declare global {
var ngJest: {
skipNgcc?: boolean;
tsconfig?: string;
testEnvironmentOptions?: TestEnvironmentOptions;
} | undefined;
}Usage Examples:
// In test setup or jest configuration
globalThis.ngJest = {
skipNgcc: true, // Skip Angular Ivy compilation cache
tsconfig: '<rootDir>/tsconfig.spec.json',
testEnvironmentOptions: {
// Custom Angular TestBed options
}
};Zone-based Applications (Default):
// setup-jest.ts
import 'jest-preset-angular/setup-env/zone';
// Jest will automatically call setupZoneTestEnv()Zoneless Applications:
// setup-jest.ts
import 'jest-preset-angular/setup-env/zoneless';
// Jest will automatically call setupZonelessTestEnv()Manual Setup with Options:
// setup-jest.ts
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone';
setupZoneTestEnv({
// Custom TestBed configuration
});Both setup functions configure Angular's TestBed with appropriate modules and platforms:
Zone Setup:
zone.js and zone.js/testingBrowserDynamicTestingModuleplatformBrowserDynamicTesting() with zone supportZoneless Setup:
BrowserDynamicTestingModule without zone patchesplatformBrowserDynamicTesting() for zoneless operationThe setup functions automatically handle Node.js environment compatibility:
// Automatic polyfills applied:
// - TextEncoder/TextDecoder for Angular's text processing
// - Proper module resolution for Angular testing utilities
// - Browser API simulation through JSDOM integrationSupported Angular Versions:
Supported Node.js Versions:
Basic Zone-based Setup:
// jest.config.js
module.exports = {
preset: 'jest-preset-angular',
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts']
};// setup-jest.ts
import 'jest-preset-angular/setup-env/zone';Advanced Configuration:
// setup-jest.ts
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone';
// Configure global settings
globalThis.ngJest = {
testEnvironmentOptions: {
// Custom TestBed options
}
};
// Setup with custom options
setupZoneTestEnv({
// Environment-specific options
});The setup functions include proper error handling and logging:
// Setup functions will log warnings for:
// - Missing Angular dependencies
// - Incompatible zone configurations
// - Invalid test environment optionsAll setup is handled automatically when importing the respective modules, making it easy to configure Angular testing environments with minimal boilerplate code.