Index of every page on this site: llms.txt
All skills
marmicode avatar

/angular-testing

@fa09f8d
by marmicodemarmicode/skills17 stars
Run it yourself

Writes, fixes and reviews Angular tests following the Pragmatic Angular Testing approach — fakes over mocks, real collaborators by default, components tested through the DOM rather than through fixtures. Targets Vitest (Browser Mode preferred, jsdom and happy-dom supported) and also applies to Jest, Jasmine/Karma and Angular Testing Library projects, including migrating them to Vitest. Use whenever adding, editing, reviewing or debugging unit tests in an Angular project — *.spec.ts files, TestBed, components, services (`@Service` or `@Injectable`), signals, guards, routing, and timers — including when tests are written as part of a feature or bug fix, when replacing mocks or spies with fakes, when declaring or faking a service, and when fixing slow or flaky tests. Prefer this over general Angular skills for anything test-related. Not for Playwright or Cypress end-to-end tests.

Use this Skill: https://skilld.dev/gh/marmicode/skills/angular-testing

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencessetup.md

≈1.7k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Setup

Write tests with the setup the project already has. Change test configuration only when the user asks; otherwise, suggest the improvements below at the end of the task. Prefer Vitest with Analog over the Angular CLI @angular/build:unit-test builder.

Contents

  1. Zoneless Test Environment
  2. Browser Mode
  3. Other Test Runners
  4. Error Sensitivity
  5. Timeouts

Zoneless Test Environment

Zoneless is the default since Angular 21. Even if the app still uses Zone.js, write zoneless-ready tests: if they pass zoneless, they also pass with zones.

  • With Analog (including Nx projects that use its Vitest plugin), initialize the test environment with setupTestBed():
/* Global styles, so that visibility and actionability checks see the real layout (Browser Mode). */
import "./styles.css";
import "@angular/compiler";
import "@testing-library/jest-dom/vitest";
import { setupTestBed } from "@analogjs/vitest-angular/setup-testbed";

/* Zoneless by default. Resets TestBed between tests, including when files share a context (`isolate: false`). */
setupTestBed();
  • With the Angular CLI @angular/build:unit-test builder, add global test providers through its providersFile option.
  • provideZonelessChangeDetection() is the stable name since Angular 20. On Angular 18–19, import provideExperimentalZonelessChangeDetection() instead.
  • If the test environment is zone-based and you can't change it, turn on automatic change detection in the test instead of calling fixture.detectChanges():
TestBed.configureTestingModule({
  providers: [{ provide: ComponentFixtureAutoDetect, useValue: true }],
});

Browser Mode

Prefer Vitest with Analog over the Angular CLI @angular/build:unit-test builder. Suggest that move at the end of the task; change the runner only when asked.

Analog (including Nx projects that use its Vitest plugin): split emulated and browser tests into two Vitest projects by file name, so both can coexist during a migration:

import { playwright } from "@vitest/browser-playwright";
import { defineConfig } from "vitest/config";

export default defineConfig({
  // ... Angular plugin (e.g. `@analogjs/vite-plugin-angular`) ...
  test: {
    globals: true,
    setupFiles: ["src/test-setup.ts"],
    testTimeout: process.env.CI ? 3_000 : 500,
    /* TestBed already isolates tests: sharing the context between test files is much faster.
     * `setupTestBed()` resets TestBed between tests.
     * Vitest 5 reads this top-level option for Browser Mode too; Vitest 4 needs `browser.isolate` (below). */
    isolate: false,
    projects: [
      {
        extends: true,
        test: {
          name: "emulated",
          environment: "jsdom",
          include: ["src/**/!(*.browser).spec.ts"],
          /* Node only: `pool` doesn't apply to Browser Mode. */
          pool: "threads",
        },
      },
      {
        extends: true,
        test: {
          name: "browser",
          include: ["src/**/*.browser.spec.ts"],
          browser: {
            enabled: true,
            provider: playwright(),
            instances: [{ browser: "chromium" }],
            /* Vitest 4 only (deprecated in Vitest 5 in favor of the top-level `isolate`). */
            isolate: false,
          },
        },
      },
    ],
  },
});
npm install -D playwright vitest @vitest/browser-playwright

Browser names are provider-specific and lowercase (chromium for Playwright, chrome for WebdriverIO). Tests run headless when CI is set, or when the name ends with Headless (chromiumHeadless).

To get Playwright-specific locator options typed, add "@vitest/browser-playwright" to compilerOptions.types in tsconfig.spec.json.

Angular CLI (Angular ≥ 21, when the project already uses @angular/build:unit-test):

{
  "test": {
    "builder": "@angular/build:unit-test",
    "options": {
      "runner": "vitest",
      "browsers": ["chromium"]
    }
  }
}

The runnerConfig option points the builder to a custom Vitest config when needed.

Other Test Runners

The principles, fakes, setup functions, and assertions of this skill apply to any test runner. Only the APIs change.

Jest (emulated DOM only: also follow emulated-dom.md):

Vitest Jest
vi.fn() jest.fn()
vi.useFakeTimers(), vi.useRealTimers() jest.useFakeTimers(), jest.useRealTimers()
await vi.advanceTimersByTimeAsync(ms), await vi.runAllTimersAsync() await jest.advanceTimersByTimeAsync(ms), await jest.runAllTimersAsync()
onTestFinished(() => vi.useRealTimers()) afterEach(() => jest.useRealTimers())
await expect.poll(() => value).toEqual(expected) await waitFor(() => expect(value).toEqual(expected)) with waitFor from @testing-library/dom
toHaveBeenCalledExactlyOnceWith(value) toHaveBeenCalledTimes(1) and toHaveBeenCalledWith(value)
vi.setTimerTickMode('nextTimerAsync') not available: use dynamic timing configuration
page, expect.element not available

To migrate, run Vitestify (npx @testmex/vitestify) and set the project up with Vitest and Analog. Then replace done callbacks with promises, and fakeAsync with Vitest fake timers.

Jasmine and Karma: Karma is deprecated, so suggest migrating with Vitestify (npx @testmex/vitestify) to Vitest with Analog. Meanwhile, apply the same principles, fakes, and setup functions, and record outputs with jasmine.createSpy().

Error Sensitivity

Keep TestBed's rethrowApplicationErrors option enabled (the default): errors thrown while rendering or in listeners must fail tests instead of being logged and ignored.

Timeouts

Keep testTimeout low locally (e.g. 500ms) so slow or stuck tests surface immediately, with more room on CI.

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts11d3 checks · Risk SAFE
  • Gen Agent Trust Hub11d

    The skill provides comprehensive guidelines and examples for writing Angular tests using Vitest and other industry-standard tools. It promotes testing best practices like using fakes and browser-mode integration testing. No malicious patterns, obfuscation, or security risks were identified.

  • Socket11d

    No alerts

  • Snyk11d

    Risk: LOW · No issues

Signed by skilld at fa09f8d. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 weeks ago.

Activeupdated 2 weeks ago

README badge

README badge for marmicode/skills/angular-testing