Skip to content

Storybook stories with auto-spies (Angular) ​

Use provideAutoSpy to replace a service in an Angular story. Pass the provider to applicationConfig, set the spy's responses in beforeEach, and check the result in play.

ts
// cart.test.stories.ts
import { type Meta, type StoryObj, applicationConfig } from '@storybook/angular-vite';
import { expect } from 'vitest';
import { resetAutoSpy } from 'vitest-auto-spy';
import { provideAutoSpy } from 'vitest-auto-spy/angular';

import { CartComponent } from './cart.component';
import { CartService } from './cart.service';

const cartProvider = provideAutoSpy(CartService);
const cart = cartProvider.useValue; // Spy<CartService>

const meta = {
  title: 'Cart/Interactions',
  component: CartComponent,
  tags: ['!dev', '!autodocs'],
  decorators: [applicationConfig({ providers: [cartProvider] })],
  beforeEach: () => {
    resetAutoSpy(cart);
    cart.total.mockReturnValue(42);
  },
} satisfies Meta<CartComponent>;

export default meta;

type Story = StoryObj<typeof meta>;

export const ShowsTheTotal: Story = {
  play: async ({ canvas }) => {
    expect(canvas.getByText('Total: 42')).toBeTruthy();
  },
};

export const ChecksOut: Story = {
  beforeEach: () => {
    cart.checkout.calledWith('tok_abc').resolveWith({ orderId: 'ord_42' });
  },
  play: async ({ canvas, userEvent }) => {
    await userEvent.click(canvas.getByRole('button', { name: 'Check out' }));

    expect((await canvas.findByRole('status')).textContent).toContain('Order ord_42');
    expect(cart.checkout).toHaveBeenCalledWith('tok_abc');
  },
};

With the Storybook Vitest addon, play runs as a Vitest test in the browser. Angular builds the component through dependency injection, just as it does in a TestBed test.

Checked with Storybook 10.6 (@storybook/angular-vite, @storybook/addon-vitest), Angular 22 and Vitest 4.1 in browser mode with Playwright's Chromium.

Vitest 4 for now

@storybook/addon-vitest 10.6 supports vitest ^3.0.0 || ^4.0.0 only. On Vitest 5 you cannot run stories through it yet. This library itself works on both.

The component ​

The stories above test this component. It reads total() in a field initializer, while it is being built, and calls checkout() on a click.

ts
@Injectable({ providedIn: 'root' })
export class CartService {
  total(): number {
    /* reads the real cart */
  }

  checkout(token: string): Promise<Order> {
    /* charges the real card */
  }
}

@Component({
  selector: 'app-cart',
  template: `
    <p>Total: {{ total }}</p>
    <button type="button" (click)="checkout()">Check out</button>
    @if (orderId()) {
      <p role="status">Order {{ orderId() }}</p>
    }
  `,
})
export class CartComponent {
  readonly #cart = inject(CartService);

  readonly total = this.#cart.total();
  readonly orderId = signal<string | null>(null);

  async checkout(): Promise<void> {
    const order = await this.#cart.checkout('tok_abc');
    this.orderId.set(order.orderId);
  }
}

The stories ​

What each part of the stories file does:

  • provideAutoSpy(CartService) returns a plain { provide, useValue } provider. useValue is the typed spy, Spy<CartService>. The provider goes to Angular through applicationConfig; the spy is kept in the cart constant, where the stories use it.
  • One spy per file, reset before each story. provideAutoSpy runs once, when the file loads, so every story shares the same spy. That is why the meta's beforeEach calls resetAutoSpy(cart): it clears the previous story's calls and answers. After the reset, a method returns undefined until you give it an answer again.
  • A void method needs no answer after the reset. The same holds for Promise<void> when the component only awaits it: await undefined resolves at once. If the code calls .then() on the result, give the method resolveWith().
  • Configure in beforeEach, not in play. play runs after the component has rendered, and this component calls total() while it is being built. Storybook runs the meta's beforeEach (it sets up total), then the story's (it sets up checkout), and only then renders the component.
  • cart.checkout.resolveWith(order) answers every call with order. With calledWith('tok_abc') in front, only a call with that token gets the answer; any other call returns undefined.
  • calledWith sets up an answer; it does not check anything. It compares arguments by value, like toHaveBeenCalledWith, so an equal object matches too. To check the call, use expect in play.
  • findByRole waits until the element appears, so it also covers data the component loads asynchronously, for example in ngOnInit. Use getBy… only for what is there on the first render.
  • expect comes from vitest here, because the file imports vitest anyway. The expect from storybook/test works too. All the usual spy matchers work: toHaveBeenCalledWith, toHaveBeenCalledTimes, toHaveBeenCalled. See the next section.
  • tags: ['!dev', '!autodocs'] hides these stories from the Storybook sidebar and docs page but keeps them in the test run. They import vitest, which does not load in storybook dev, so without these tags they would break the Storybook UI. Keep them in their own file, such as *.test.stories.ts, so the stories people browse stay clean.

Why expect comes from vitest ​

The example imports expect from vitest because these stories run only as tests and import vitest already. The expect from storybook/test accepts this library's spies too, and each of its assertions shows up in the Interactions panel:

ts
import { expect } from 'storybook/test';

expect(cart.checkout).toHaveBeenCalledWith('tok_abc');

Storybook's expect wraps every function argument in a new function, unless the function has its own enumerable keys. Every spy has one, _isMockFunction, so the matcher receives the spy itself. That is also the one key Object.keys(spy) and { ...spy } show; a vi.fn() shows it among its mock* keys.

What this is not ​

  • Not a Storybook adapter. There is no vitest-auto-spy/storybook to install, and Storybook's own fn() from storybook/test works as before. You use vitest-auto-spy/angular exactly as in a spec.
  • Not a module mock. sb.mock() in .storybook/preview.ts replaces a module for the whole Storybook build. A provider replaces one service for one file's stories.
  • Angular — provideAutoSpy, injectSpy and the TestBed side of the same providers.
  • Control helpers — calledWith, resolveWith, resetAutoSpy.
  • Spec patterns — conventions for large Angular test suites; most apply to stories too.
Older versions

In vitest-auto-spy 5.72.0 and earlier, a spy has no own enumerable keys. Storybook's expect then fails with:

text
TypeError: [Function] is not a spy or a call to a spy!

On those versions, import expect from vitest. Another option is setSpyEngine('runner') from vitest-auto-spy/setup, called at the top of the stories file, before provideAutoSpy. It is a workaround for those versions only; newer versions do not need it.

Released under the MIT License.