jest-preset-angular

repository·main·Indexed 21 days ago

https://github.com/thymikee/jest-preset-angular

A pre-configured Jest setup for Angular projects that handles TypeScript transformation and Angular-specific requirements. Version 17.0.0 provides support for CommonJS (CJS) and ECMAScript Modules (ESM) transpilation, Babel integration, and custom JSDOM environments. The preset includes configurations for handling .ts, .mjs, .js, and .html files, with options for esbuild processing and stringifying content for assets like SVG and HTML.

Tokens
30.4K
Snippets
100
Records
112
Agent score
75%

What's inside jest-preset-angular

  1. Understand Jest configuration keys

    main

    When configuring jest-preset-angular, the following keys serve these purposes:

    • transform: Used to pass configuration to ts-jest for code compilation and to run files through a Jest transformer so Jest can understand non-JS syntax (like TS, HTML, or SVG).
    • moduleFileExtensions: Defines the file types your modules use (e.g., ts, html, js, json, mjs).
    • moduleNameMapper: Maps absolute imports to specific paths using RegExp.
    • snapshotSerializers: An array of serializers applied when performing snapshot testing.
    • testEnvironment: The environment used to run tests (typically jsdom).
    • transformIgnorePatterns: Instructs Jest which files in node_modules should still be transformed (e.g., .mjs files or Angular locale files).
  2. Configure Jest to support TypeScript path mappings

    main

    If you use TypeScript path mappings (defined in the paths property of your tsconfig.json), Jest will not automatically resolve them. You must create corresponding mappings in your Jest configuration using the moduleNameMapper property.

    This ensures that when your code imports a module via an absolute path (e.g., @app/services/...), Jest knows how to locate the actual file on disk.

    For detailed implementation details on how to format these mappings, refer to the ts-jest paths mapping documentation.

  3. Generate code for the user library

    main

    To generate new Angular schematics (components, directives, pipes, etc.) specifically for the user project, use the Angular CLI with the --project user flag. This ensures the generated files are placed in the correct project directory rather than the default project defined in angular.json.

    Supported types include:

    • component
    • directive
    • pipe
    • service
    • class
    • guard
    • interface
    • enum
    • module
    ng generate component component-name --project user
    ng generate directive|pipe|service|class|guard|interface|enum|module --project user
  4. Add global browser mocks for JSDOM

    main

    Since jest-preset-angular uses JSDOM, which lacks some browser behaviors, you may need to provide global mocks.

    1. Create a jest-global-mocks.ts file in your project root.
    2. Import this file in your setup-jest.ts file before calling setupZoneTestEnv().

    Example jest-global-mocks.ts:

    Object.defineProperty(document, 'doctype', {
      value: '<!DOCTYPE html>',
    });
    Object.defineProperty(window, 'getComputedStyle', {
      value: () => {
        return {
          display: 'none',
          appearance: ['-webkit-appearance'],
        };
      },
    });
    /**
     * ISSUE: https://github.com/angular/material2/issues/7101
     * Workaround for JSDOM missing transform property
     */
    Object.defineProperty(document.body.style, 'transform', {
      value: () => {
        return {
          enumerable: true,
          configurable: true,
        };
      },
    });
    import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone';
    import './jest-global-mocks';
    
    setupZoneTestEnv();