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.

referencesrouter.md

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

Router

Contents

  1. Rules
  2. Testing a Guard
  3. Components Using Route Params

Rules

  • Use the real router: provideRouter(routes) and RouterTestingHarness. Never stub or mock Router or ActivatedRoute.
  • Declare test-only routes with a tiny Dummy component that displays its title, rather than loading real pages.
  • Assert both the URL and what the user sees. Use expect.soft() for the URL so both checks are reported.
  • Arrange state through the real services (e.g. auth.signIn()), or fakes for adapters.
  • In Browser Mode, the real Location pushes state in the test page, so the URL of one test can survive into the next. If a test depends on the initial URL, add provideLocationMocks() from @angular/common/testing to keep navigation out of the browser history.

Testing a Guard

describe(isSignedInGuard, () => {
  it("allows access to the route if user is signed in", async () => {
    const { auth, getCurrentUrl, navigate } = await mountRoutes();

    auth.signIn();

    await navigate("/meal-plan");

    expect.soft(getCurrentUrl()).toBe("/meal-plan");
    await expect
      .element(page.getByRole("heading"))
      .toHaveTextContent("Meal Plan");
  });

  it("redirects to /search if user is not signed in and first visit", async () => {
    const { getCurrentUrl, navigate } = await mountRoutes();

    await navigate("/meal-plan");

    expect.soft(getCurrentUrl()).toBe("/search");
    await expect.element(page.getByRole("heading")).toHaveTextContent("Search");
  });

  async function mountRoutes() {
    TestBed.configureTestingModule({
      providers: [
        provideRouter(
          [
            { path: "landing", component: Dummy, data: { title: "Landing" } },
            { path: "search", component: Dummy, data: { title: "Search" } },
            {
              path: "meal-plan",
              component: Dummy,
              data: { title: "Meal Plan" },
              canActivate: [isSignedInGuard],
            },
          ],
          withComponentInputBinding(),
        ),
      ],
    });

    /* No initial URL: the first `navigate()` is the app's first navigation. */
    const harness = await RouterTestingHarness.create();

    return {
      auth: TestBed.inject(Auth),
      getCurrentUrl: () => TestBed.inject(Router).url,
      navigate: (url: "/landing" | "/search" | "/meal-plan") =>
        harness.navigateByUrl(url),
    };
  }

  @Component({
    template: `<h1>{{ title() }}</h1>`,
  })
  class Dummy {
    /* Bound from the route's `data` thanks to `withComponentInputBinding()`. */
    title = input.required<string>();
  }
});

Full example: examples/user/is-signed-in.guard.browser.spec.ts.

Components Using Route Params

  • If the component receives route params as inputs (withComponentInputBinding()), test it like any component: bind the params with inputBinding('recipeId', () => 'rec_burger').
  • To test navigation itself (links, redirects, re-rendering when the same route gets a new param), navigate with RouterTestingHarness and the real routes.

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