Jest preset configuration for Angular projects
npx @tessl/cli install tessl/npm-jest-preset-angular@15.0.0Jest Preset Angular is a comprehensive Jest preset configuration specifically designed for Angular projects, enabling developers to efficiently set up and run unit tests in Angular applications. It provides seamless integration with TypeScript, Angular-specific transformations, and optimized test configurations that handle Angular's unique compilation and module loading requirements.
npm install --save-dev jest-preset-angularimport { createCjsPreset, createEsmPreset } from 'jest-preset-angular/presets';For the transformer:
import jestPresetAngular from 'jest-preset-angular';
const transformer = jestPresetAngular.createTransformer();For CommonJS (jest.config.js):
const { createCjsPreset } = require('jest-preset-angular/presets');// jest.config.js
const { createCjsPreset } = require('jest-preset-angular/presets');
module.exports = createCjsPreset({
tsconfig: '<rootDir>/tsconfig.spec.json'
});// jest.config.mjs
import { createEsmPreset } from 'jest-preset-angular/presets';
export default createEsmPreset({
tsconfig: '<rootDir>/tsconfig.spec.json'
});// jest.config.js
module.exports = {
preset: 'jest-preset-angular',
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts']
};Jest Preset Angular is built around several key components:
createCjsPreset, createEsmPreset) that generate complete Jest configurations for different module systemsNgJestTransformer class that extends ts-jest to handle Angular-specific compilation needsNgJestCompiler that processes Angular decorators, templates, and stylesNgJestConfig that handles Angular-specific TypeScript and Jest settingsCore preset creation functions that generate complete Jest configurations optimized for Angular projects with different module systems and environments.
function createCjsPreset(options?: CjsPresetOptionsType): CjsPresetType;
function createEsmPreset(options?: EsmPresetOptionsType): EsmPresetType;
interface CjsPresetOptionsType {
tsconfig?: TsJestTransformerOptions['tsconfig'];
astTransformers?: TsJestTransformerOptions['astTransformers'];
babelConfig?: TsJestTransformerOptions['babelConfig'];
diagnostics?: TsJestTransformerOptions['diagnostics'];
testEnvironment?: JSDOMEnvironment;
}
interface EsmPresetOptionsType {
tsconfig?: TsJestTransformerOptions['tsconfig'];
astTransformers?: TsJestTransformerOptions['astTransformers'];
babelConfig?: TsJestTransformerOptions['babelConfig'];
diagnostics?: TsJestTransformerOptions['diagnostics'];
testEnvironment?: JSDOMEnvironment;
}
type JSDOMEnvironment = 'jsdom' | 'jest-preset-angular/environments/jest-jsdom-env';Custom Jest transformer that handles Angular-specific compilation including component templates, styles, decorators, and TypeScript compilation with Angular compiler integration.
class NgJestTransformer extends TsJestTransformer {
constructor(tsJestConfig?: TsJestTransformerOptions);
process(
fileContent: string,
filePath: string,
transformOptions: TsJestTransformOptions
): TransformedSource;
}
function createTransformer(tsJestConfig?: TsJestTransformerOptions): NgJestTransformer;Custom JSDOM environment optimized for Angular testing with proper global setup and Angular-specific configurations.
class JestJSDOMEnvironment extends BaseEnv {
constructor(config: JestEnvironmentConfig, context: EnvironmentContext);
}Functions for setting up Angular testing environments with proper TestBed configuration and zone handling.
function setupZoneTestEnv(options?: TestEnvironmentOptions): void;
function setupZonelessTestEnv(options?: TestEnvironmentOptions): void;Built-in snapshot serializers for clean, readable Angular component test snapshots with Angular-specific attribute handling.
interface NgSnapshotOptions {
omitAllCompAttrs?: boolean;
}interface TsJestTransformOptions {
config?: {
[key: string]: any;
globals?: {
ngJest?: NgJestGlobalConfig;
};
};
supportsStaticESM?: boolean;
}
interface NgJestGlobalConfig {
skipNgcc?: boolean;
tsconfig?: string;
testEnvironmentOptions?: TestEnvironmentOptions;
processWithEsbuild?: string[];
}
interface TestEnvironmentOptions {
[key: string]: any;
}
interface TransformedSource {
code: string;
map?: string | object;
}
interface SnapshotSerializer {
serialize(val: any, config: any, indentation: string, depth: number, refs: any, printer: any): string;
test(val: any): boolean;
}
type TsJestTransformerOptions = {
tsconfig?: string | object;
astTransformers?: object;
babelConfig?: object;
diagnostics?: object;
isolatedModules?: boolean;
stringifyContentPathRegex?: string;
useESM?: boolean;
};
type CjsPresetType = Config & {
transformIgnorePatterns: string[];
transform: Record<string, any>;
};
type EsmPresetType = Config & {
extensionsToTreatAsEsm: string[];
moduleNameMapper: Record<string, string>;
transformIgnorePatterns: string[];
transform: Record<string, any>;
};
interface Config {
testEnvironment?: string;
moduleFileExtensions?: string[];
snapshotSerializers?: string[];
preset?: string;
setupFilesAfterEnv?: string[];
}
interface BaseEnv {
constructor(config: JestEnvironmentConfig, context: EnvironmentContext, jsdom: any);
global: any;
moduleMocker: any;
fakeTimers: any;
setup(): Promise<void>;
teardown(): Promise<void>;
runScript(script: any): any;
}
interface JestEnvironmentConfig {
projectConfig: any;
globalConfig: any;
}
interface EnvironmentContext {
console: Console;
docblockPragmas: Record<string, string | string[]>;
testPath: string;
}
interface TsJestTransformer {
constructor(tsJestConfig?: TsJestTransformerOptions);
process(fileContent: string, filePath: string, transformOptions: TsJestTransformOptions): TransformedSource;
getCacheKey(fileContent: string, filePath: string, transformOptions: TsJestTransformOptions): string;
}