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
Custom Jest transformer that handles Angular-specific compilation including component templates, styles, decorators, and TypeScript compilation with Angular compiler integration.
Main transformer class that extends ts-jest's TsJestTransformer to provide Angular-specific file processing and compilation.
/**
* Angular-specific Jest transformer extending ts-jest functionality
*/
class NgJestTransformer extends TsJestTransformer {
/**
* Creates a new NgJestTransformer instance
* @param tsJestConfig - Optional ts-jest configuration options
*/
constructor(tsJestConfig?: TsJestTransformerOptions);
/**
* Processes source files for Jest transformation
* @param fileContent - The content of the file to transform
* @param filePath - The path to the file being transformed
* @param transformOptions - Jest transformation options
* @returns Transformed source code with source maps
*/
process(
fileContent: string,
filePath: string,
transformOptions: TsJestTransformOptions
): TransformedSource;
/**
* Gets the current package version
* @returns The version string from package.json
*/
readonly version: string;
}
interface TransformedSource {
code: string;
map?: string | object;
}Usage Examples:
import { NgJestTransformer } from 'jest-preset-angular';
// Create transformer instance
const transformer = new NgJestTransformer();
// With custom ts-jest configuration
const transformer = new NgJestTransformer({
tsconfig: '<rootDir>/tsconfig.spec.json',
isolatedModules: true
});
// Use in Jest configuration
module.exports = {
transform: {
'^.+\\.(ts|js|html|svg)$': ['jest-preset-angular', {
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.(html|svg)$'
}]
}
};Factory function for creating NgJestTransformer instances, providing the main export of the package.
/**
* Factory function to create NgJestTransformer instances
* @param tsJestConfig - Optional ts-jest configuration options
* @returns New NgJestTransformer instance
*/
function createTransformer(tsJestConfig?: TsJestTransformerOptions): NgJestTransformer;Usage Examples:
import jestPresetAngular from 'jest-preset-angular';
// Create transformer using factory
const transformer = jestPresetAngular.createTransformer();
// With configuration
const transformer = jestPresetAngular.createTransformer({
tsconfig: '<rootDir>/tsconfig.spec.json'
});Angular-specific compiler that handles TypeScript compilation with Angular transformations.
/**
* Angular-specific Jest compiler extending ts-jest's TsCompiler
*/
class NgJestCompiler extends TsCompiler {
/**
* Creates a new NgJestCompiler instance
* @param configSet - Configuration set for the compiler
* @param jestCacheFS - Jest cache file system map
*/
constructor(
readonly configSet: ConfigSet,
readonly jestCacheFS: Map<string, string>
);
}Angular-specific configuration management extending ts-jest's ConfigSet.
/**
* Angular-specific Jest configuration extending ts-jest ConfigSet
*/
class NgJestConfig extends ConfigSet {
/** Matcher for determining which files to process with esbuild */
readonly processWithEsbuild: ReturnType<typeof globsToMatcher>;
/**
* Creates a new NgJestConfig instance
* @param jestConfig - Jest configuration object
* @param parentLogger - Optional parent logger instance
*/
constructor(
jestConfig: TsJestTransformOptions['config'] | undefined,
parentLogger?: Logger | undefined
);
}
interface Logger {
debug(message: string, ...args: any[]): void;
info(message: string, ...args: any[]): void;
warn(message: string, ...args: any[]): void;
error(message: string, ...args: any[]): void;
}
type globsToMatcher = (patterns: string[]) => (filePath: string) => boolean;The NgJestTransformer handles different file types with optimized processing:
esbuild Processing:
.mjs files)processWithEsbuild patternsTypeScript API Processing:
File Type Support:
.ts).js).mjs).html).svg)Template and Style Processing:
templateUrl and styleUrls propertiesDecorator Processing:
JIT Compilation Support:
angularJitApplicationTransformreplaceResources transformer for template and style inliningCustom AST Transformers:
replaceResources: Processes templateUrl and styleUrls to inline contentangularJitApplicationTransform: Handles Angular-specific JIT transformationsPerformance Optimizations:
Generates cache keys for transformed files to optimize Jest's caching mechanism.
/**
* Generates a cache key for the transformed file
* @param fileContent - The content of the file
* @param filePath - The path to the file
* @param transformOptions - Jest transformation options
* @returns SHA1 hash for cache identification
*/
getCacheKey(
fileContent: string,
filePath: string,
transformOptions: TsJestTransformOptions
): string;Usage Examples:
// Automatically used by Jest for caching
const transformer = new NgJestTransformer();
const cacheKey = transformer.getCacheKey(content, filePath, options);
// Cache key includes:
// - File content hash
// - Transformer version
// - Configuration options
// - TypeScript compiler optionsProvides the current version of jest-preset-angular for debugging and cache invalidation.
/**
* Gets the current package version
* @returns The version string from package.json
*/
readonly version: string;The transformer accepts all standard ts-jest options plus Angular-specific configurations:
interface TsJestTransformerOptions {
/** TypeScript configuration file path or inline options */
tsconfig?: string | object;
/** Custom AST transformers */
astTransformers?: object;
/** Babel configuration for additional processing */
babelConfig?: object;
/** TypeScript diagnostic options */
diagnostics?: object;
/** Enable isolated modules compilation */
isolatedModules?: boolean;
/** String content path regex for inlining */
stringifyContentPathRegex?: string;
/** Enable ESM support */
useESM?: boolean;
}
interface TsJestAstTransformer {
before?: ts.TransformerFactory<ts.SourceFile>[];
after?: ts.TransformerFactory<ts.SourceFile>[];
afterDeclarations?: ts.TransformerFactory<ts.SourceFile>[];
}
interface ConfigSet {
compilerModule: typeof ts;
parsedTsConfig: ts.ParsedCommandLine;
useESM: boolean;
resolvedTransformers: TsJestAstTransformer;
}
interface TsJestTransformer {
constructor(tsJestConfig?: TsJestTransformerOptions);
process(fileContent: string, filePath: string, transformOptions: TsJestTransformOptions): TransformedSource;
getCacheKey(fileContent: string, filePath: string, transformOptions: TsJestTransformOptions): string;
}
interface TsCompiler {
constructor(configSet: ConfigSet, jestCacheFS: Map<string, string>);
program?: ts.Program;
}