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.

referencesfakes.md

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

Fakes in Angular

Why fakes beat stubs, spies and mocks: principles.md.

Contents

  1. Anatomy of a Fake
  2. Providing and Configuring a Fake
  3. Arranging Before Instantiation
  4. Simulating Errors
  5. Wrapping What You Don't Own
  6. Contract Tests
  7. Testing the Adapters Themselves
  8. When There Is No Repository to Fake
  9. Where Fakes Live

Anatomy of a Fake

  1. Interface: extract the public API of the real service into an interface (RecipeRepositoryDef). The real service and the fake both implement it, so the fake doesn't have to mimic private members or depend on infrastructure.
  2. Fake class: @Service({ autoProvided: false }) — not @Service(). Forgetting to provide it then fails loudly instead of silently using the real service. Before Angular 22.1, that is @Injectable() with no providedIn.
  3. In-memory state, shared by all methods so they stay consistent.
  4. Lazy observables: wrap the work in defer(...) so nothing happens until the caller subscribes, like a real HTTP call. A SUT that forgets to subscribe then fails the test instead of passing by accident.
  5. Testing helpers: methods to arrange (setRecipes(), configure()) and to inspect (getMealsSync()). Add them only when a test needs them.
  6. Explicit failures: methods or arguments the fake doesn't support throw instead of returning undefined.
export interface RecipeRepositoryDef {
  search(filter?: RecipeFilterCriteria): Observable<Recipe[]>;
}

@Service()
export class RecipeRepository implements RecipeRepositoryDef {
  private _httpClient = inject(HttpClient);
  ...
}
@Service({ autoProvided: false })
export class RecipeRepositoryFake implements RecipeRepositoryDef {
  private _recipes: Recipe[] = [];

  search(
    {
      keywords,
      maxIngredientCount,
      maxStepCount,
    }: RecipeFilterCriteria = createDefaultRecipeFilterCriteria(),
  ): Observable<Recipe[]> {
    return defer(() => {
      const recipes = this._recipes.filter((recipe) => {
        const conditions = [
          () => (keywords ? recipe.name.includes(keywords) : true),
          () =>
            maxIngredientCount != null
              ? recipe.ingredients.length <= maxIngredientCount
              : true,
          () =>
            maxStepCount != null ? recipe.steps.length <= maxStepCount : true,
        ];
        return conditions.every((condition) => condition());
      });
      return of(recipes);
    });
  }

  setRecipes(recipes: Recipe[]) {
    this._recipes = recipes;
  }
}

export function provideRecipeRepositoryFake(): EnvironmentProviders {
  return makeEnvironmentProviders([
    RecipeRepositoryFake,
    { provide: RecipeRepository, useExisting: RecipeRepositoryFake },
  ]);
}

Unsupported methods and arguments fail explicitly. E.g., if RecipeRepositoryDef had an updateRecipe() method that no test needs yet:

updateRecipe(recipeId: string, data: Partial<Recipe>): Observable<void> {
  throw new Error('🚧 RecipeRepositoryFake#updateRecipe is not implemented yet');
}

Providing and Configuring a Fake

TestBed.configureTestingModule({
  providers: [provideRecipeRepositoryFake()],
});

TestBed.inject(RecipeRepositoryFake).setRecipes([
  recipeMother.withBasicInfo("Burger").build(),
]);
  • useExisting provides the same instance as both RecipeRepository and RecipeRepositoryFake: inject the fake by its own class, never TestBed.inject(RecipeRepository) as RecipeRepositoryFake.

  • Configure the fake before the SUT reads from it: before TestBed.createComponent(...), or before injecting a service that fetches in its constructor.

  • Assert side effects on the fake's state, not on calls:

    expect(mealRepoFake.getMealsSync()).toEqual([
      expect.objectContaining({ name: "Burger" }),
    ]);

Arranging Before Instantiation

When the service reads its dependencies at construction time, return a getter so the test can arrange first. Arrange through the fake's testing helpers (setMeals()): its lazy API (addMeal()) does nothing until someone subscribes.

it("fetches recipes from meal repository", async () => {
  const { getMealPlanner, mealRepoFake, burger } = setUpMealPlanner();

  mealRepoFake.setMeals([burger]);

  const mealPlanner = getMealPlanner();
  expect(mealPlanner.recipes()).toEqual([
    expect.objectContaining({ name: "Burger" }),
  ]);
});

function createMealPlanner() {
  const { getMealPlanner, ...utils } = setUpMealPlanner();
  return { mealPlanner: getMealPlanner(), ...utils };
}

function setUpMealPlanner() {
  TestBed.configureTestingModule({
    providers: [provideMealRepositoryFake()],
  });

  return {
    getMealPlanner: () => TestBed.inject(MealPlanner),
    mealRepoFake: TestBed.inject(MealRepositoryFake),
    burger: recipeMother.withBasicInfo("Burger").build(),
  };
}

Simulating Errors

Give the fake a simple, documented trigger instead of reprogramming it per test:

@Service({ autoProvided: false })
export class CookbookRepositoryFake implements CookbookRepositoryDef {
  /**
   * Searching with keywords containing this token fails.
   */
  static readonly INVALID_TOKEN = '💥';

  searchCookbooks(keywords: string | null): Observable<Cookbook[]> {
    return defer(() => {
      if (keywords?.includes(CookbookRepositoryFake.INVALID_TOKEN)) {
        throw new Error('CookbookRepositoryFake: invalid token in keywords.');
      }
      ...
    });
  }
}
await typeKeywords(`Ottolenghi ${CookbookRepositoryFake.INVALID_TOKEN}`);

Wrapping What You Don't Own

Never fake Angular Material, CDK, Router, HttpClient or third-party SDKs directly. In order of preference:

  1. Collaborate with the real thing: open the real MatDialog, interact with it through page, and assert the outcome. This is usually the widest Narrow test.
  2. Wrap it in your own abstraction when the real thing makes the test too complex or slow, then fake your abstraction. Keep the abstraction tailored to your needs, not a copy of the library's API. When the code calls HttpClient and there is no such abstraction yet, put a fake server in front of the real client — see When There Is No Repository to Fake.
export interface DialogRef<RESULT> {
  closed$: Observable<RESULT | undefined>;
}

export interface DialogDef {
  open<RESULT>(component: Type<unknown>): DialogRef<RESULT>;
}

@Service()
export class Dialog implements DialogDef {
  private _matDialog = inject(MatDialog);

  open<RESULT>(component: Type<unknown>): DialogRef<RESULT> {
    const ref = this._matDialog.open<unknown, unknown, RESULT>(component);
    return { closed$: ref.afterClosed() };
  }
}
@Service({ autoProvided: false })
export class DialogFake implements DialogDef {
  private _openDialog?: {
    component: Type<unknown>;
    close: (result: unknown) => void;
  };

  open<RESULT>(component: Type<unknown>): DialogRef<RESULT> {
    if (this._openDialog) {
      throw new Error("DialogFake: a dialog is already open.");
    }
    const closed$ = new Subject<RESULT | undefined>();
    this._openDialog = {
      component,
      close: (result) => {
        closed$.next(result as RESULT | undefined);
        closed$.complete();
      },
    };
    return { closed$ };
  }

  getOpenDialogSync(): Type<unknown> | undefined {
    return this._openDialog?.component;
  }

  close(result?: unknown) {
    if (!this._openDialog) {
      throw new Error("DialogFake: no dialog is open.");
    }
    const { close } = this._openDialog;
    this._openDialog = undefined;
    close(result);
  }
}

export function provideDialogFake(): EnvironmentProviders {
  return makeEnvironmentProviders([
    DialogFake,
    { provide: Dialog, useExisting: DialogFake },
  ]);
}
it("deletes recipe once confirmed", async () => {
  const { dialogFake } = mountDeleteRecipeButton();

  await page.getByRole("button", { name: "Delete" }).click();

  expect(dialogFake.getOpenDialogSync()).toBe(ConfirmDeletion);

  dialogFake.close(true);

  await expect
    .element(page.getByRole("paragraph"))
    .toHaveTextContent("Recipe deleted.");
});

Keep fakes self-contained: a fake never injects another fake. If two fakes need shared state, reconsider the boundaries.

Contract Tests

A contract test runs the same suite against the real implementation and the fake, so the fake can't drift from reality.

export function verifyMealRepositoryContract(
  createMealRepository: () => { mealRepo: MealRepositoryDef },
) {
  const burger = recipeMother.withBasicInfo("Burger").build();
  const salad = recipeMother.withBasicInfo("Salad").build();

  it("adds recipes", async () => {
    const { mealRepo } = createMealRepository();

    await lastValueFrom(mealRepo.addMeal(burger));
    await lastValueFrom(mealRepo.addMeal(salad));

    expect(await lastValueFrom(mealRepo.getMeals())).toEqual([
      expect.objectContaining({ name: "Burger" }),
      expect.objectContaining({ name: "Salad" }),
    ]);
  });

  it("returns empty array initially", async () => {
    const { mealRepo } = createMealRepository();

    expect(await lastValueFrom(mealRepo.getMeals())).toEqual([]);
  });
}
describe(MealRepositoryFake, () => {
  verifyMealRepositoryContract(() => {
    TestBed.configureTestingModule({ providers: [MealRepositoryFake] });
    return { mealRepo: TestBed.inject(MealRepositoryFake) };
  });
});
describe(MealRepository, () => {
  verifyMealRepositoryContract(createMealRepository);

  /* Implementation-specific behavior stays here. */
  it('returns empty array when storage value is invalid', async () => {
    ...
  });
});

Testing the Adapters Themselves

Most tests use the fake. The real adapter still needs coverage:

  • Run the contract tests against the real implementation when its infrastructure is fast and deterministic (e.g. a repository over LocalStorage, itself replaced by LocalStorageFake).
  • For HTTP adapters, prefer a contract test or a Wide test against a real backend (kept in a separate project or suite if it's slow), or rely on end-to-end tests.
  • Avoid HttpTestingController for a repository: it couples tests to request details of an API you don't own, and keeps passing when the backend changes. Don't rewrite existing HttpTestingController suites unless asked.

When There Is No Repository to Fake

Brownfield code often calls HttpClient (or httpResource() / resource() with a fetcher) straight from a service or component, so there is no boundary you own to fake. In order of preference:

  1. Extract a repository (XRepository + XRepositoryDef) around the HTTP calls and fake that, if the task allows touching production code. Otherwise, suggest it at the end.

  2. Stand up a fake server and keep the real HttpClient (the same provideHttpClient() options the application uses). The fake answers the request the application actually sends, so HttpClient, httpResource(), and a resource() loader that calls fetch all hit it. MSW is the usual way; a real fake server — a small HTTP server the test owns — is the other. Keep the test about the behavior the SUT exposes, and arrange the response before the SUT runs.

    A fake server still names the method and the URL, which is why step 1 comes first. HttpTestingController is the wrong tool here: it replaces HttpClient's backend instead of answering the network.

With httpResource() or resource(), the boundary is the same: a repository that returns the resource, or the fake server when no repository exists. The component or service that reads the resource is then tested through the DOM or expect.poll as usual.

Where Fakes Live

Put the fake next to the real implementation (recipe-repository.fake.ts beside recipe-repository.ts). In a library of a monorepo, export it from a testing entry point (e.g. @scope/recipes/testing) so other projects can reuse it without importing test code from the main entry.

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