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.

referencesvitest.md

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

Vitest

Framework-agnostic Vitest usage for this skill.

Contents

  1. Structure and Naming
  2. Assertions
  3. Errors
  4. Retrying Assertions
  5. Spies
  6. Fake Timers
  7. Browser Mode: Locators, Actions and Assertions
  8. Browser Mode vs. Emulated DOM
  9. Timeouts

Structure and Naming

  • Name suites after the SUT so they follow renames: describe(MealPlanner, ...).
  • Name tests after the behavior, lowercase, present tense: it('adds recipes', ...), not it('should work', ...).
  • Plan tests with it.todo('...') before implementing them.
  • Put the spec next to the code under test. Follow the project's file patterns (e.g. *.browser.spec.ts for Browser Mode).
  • Keep setup functions inside the describe they serve. Move them to file level only when several suites in the file share them.
  • The samples in this skill assume globals: true. If the project doesn't enable it, import describe, it, expect, vi and onTestFinished from 'vitest'; page always comes from 'vitest/browser'.

Assertions

Need Use
Primitive toBe(42)
Whole structure toEqual([...])
Some properties of an object toMatchObject({ name: 'Burger' }) or expect.objectContaining({ name: 'Burger' })
Some items of an array, any order expect.arrayContaining([...])
Part of a string expect.stringContaining('Burger')
Typed partial expectation expect.objectContaining({ keywords: 'Burger' } satisfies Partial<Filter>)
Several related checks reported together expect.soft(...)

Errors

expect(() => cart.add(burger)).toThrow('Item already in cart.'); // pass a function, not its result
await expect(cart.checkout()).rejects.toThrow('Cart is empty.');
expect(() => parse('💥')).toThrow(ParseError); // when the error type is part of the contract

Never use try/catch in tests: the test passes when nothing throws.

Retrying Assertions

API Use for
await expect.element(locator).toHaveTextContent('Burger') DOM in Browser Mode
await expect.poll(() => cart.getItems()).toEqual([...]) any other asynchronous state
const el = await vi.waitFor(() => locator.element()) waiting for something to exist before acting
  • Always await them: the test can finish before a non-awaited assertion fails.
  • Never wait with setTimeout or arbitrary sleeps.
  • Polling stops at the first match:
    • Negative or unchanged expectations (not.toBeInTheDocument(), toEqual([]), not.toHaveBeenCalled()) can pass before the system reacts. Pair them with a positive expectation that proves the update happened (e.g. "only Burger is displayed" rather than "Salad is not displayed"), or let the system settle first (e.g. advance fake timers).
    • If a value is only valid for a moment, polling can miss a regression that happens right after.

Spies

const onChange = vi.fn<(value: string) => void>();

// ... pass `onChange` as a callback, then act ...

expect(onChange).toHaveBeenCalledExactlyOnceWith('Burger');
expect(onChange).toHaveBeenLastCalledWith('Burger');
expect(onChange).toHaveBeenNthCalledWith(1, 'Burger');
expect(onChange).not.toHaveBeenCalled();
  • Use spies to record outputs and callbacks.
  • If the setup already triggered the spy, call onChange.mockClear() before acting instead of re-asserting the setup.
  • Don't program return values (mockReturnValue, mockImplementation): that's a stub. Use a fake.
  • vi.spyOn(fake, 'method') keeps the real behavior. Reserve it for the rare indirect output that the fake's state can't show, or for retrofitting tests on legacy code you are still trying to understand.
  • Never vi.mock() modules:
    • it is less readable,
    • it is misleading: calls between functions of the same module still reach the real implementation,
    • it turns into spaghetti when tests stub different things,
    • it couples tests to Vitest.

Fake Timers

function setUpFakeTimers() {
  vi.useFakeTimers(); // manual mode: time stands still until the test advances it
  vi.setTimerTickMode('nextTimerAsync'); // optional fast-forward mode (Vitest ≥ 4.1): delays are skipped automatically
  onTestFinished(() => {
    vi.useRealTimers();
  });
}

Which mode to pick, how to flush Angular after mounting, and the full list of rules (install before anything schedules a timer, *Async APIs only, approximate time, one mode per test, vi.setSystemTime) live in time.md.

Browser Mode: Locators, Actions and Assertions

import { page } from 'vitest/browser';

Locators are lazy recipes to find elements. Creating one queries nothing; actions and expect.element resolve it, with retries.

Priority — use the first one that works:

  1. page.getByRole('button', { name: 'Add' })
  2. page.getByLabelText('Keywords')
  3. page.getByPlaceholder('Search')
  4. page.getByText('No recipes found')
  5. page.getByAltText(...), page.getByTitle(...)
  6. page.getByTestId('recipe-preview') — last resort

Locators are strict: an action on a locator matching several elements fails. Narrow it down to what the user would look at:

page.getByRole('article').filter({ hasText: 'Burger' }).getByRole('button', { name: 'Add' });
page.getByRole('article').filter({ has: page.getByRole('heading', { name: 'Burger' }) });

Use nth(i), first() and last() only when the position is the behavior under test.

Actions go through the provider (e.g. Playwright), with actionability checks — the element must be visible, stable, enabled, and receive the event:

await page.getByRole('button', { name: 'Add' }).click();
await page.getByLabelText('Keywords').fill('Burger');

Assertions retry until they pass or time out:

await expect.element(page.getByRole('heading')).toHaveTextContent('Burger'); // exact text (Vitest ≥ 5), partial on Vitest 4
await expect.element(page.getByRole('heading')).toMatchTextContent(/burger/i); // partial or regex (Vitest ≥ 5)
await expect.element(page.getByRole('heading')).toHaveLength(2); // number of matching elements
await expect.element(page.getByRole('button', { name: 'Add' })).toBeDisabled();
await expect.element(page.getByLabelText('Keywords')).toHaveValue('Burger');
await expect.element(page.getByRole('alert')).toBeVisible();

Pick toHaveTextContent when the whole text is the behavior, toMatchTextContent when only a fragment matters. On Vitest 4, toHaveTextContent is partial and toMatchTextContent doesn't exist: check the Vitest major version first.

Raw elements, when an API needs a DOM node: locator.element() (exactly one, throws otherwise), locator.elements() (all current matches, no waiting), locator.query() (one or null).

"Partial" vs. "Full" Browser Mode: DOM APIs, fireEvent, and Testing Library's userEvent run as JavaScript inside the page and skip actionability checks — a test can click a button that real users can't reach. The page API is "Full" Browser Mode. When migrating existing Testing Library tests, importing userEvent from vitest/browser instead of @testing-library/user-event routes interactions through the provider.

Browser Mode vs. Emulated DOM

vitest/browser can only be imported in Browser Mode; in jsdom or happy-dom it throws vitest/browser can be imported only inside the Browser Mode. When a project runs both, component tests (including shallow tests and guards) go in the browser project's file pattern (e.g. *.browser.spec.ts). Services, fakes, and contract tests stay in the emulated project. What emulated environments can't do, and how to test there when there is no browser project, is in emulated-dom.md.

Timeouts

  • Keep testTimeout low (e.g. 500ms locally) so that tests that are not Narrow, or that wait for nothing, surface immediately.
  • Never raise a timeout to make a test pass. Look for a missing await, an assertion that can't pass, or a dependency that should be faked.

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