Emulated DOM (jsdom, happy-dom)
Use this only when the project has no Browser Mode project. If the Vitest config has one, component tests belong there (*.browser.spec.ts and page locators), including shallow tests. Everything else in this skill still applies.
Contents
Limits
vitest/browser(page,expect.element) can't be imported.- There is no layout, CSS, or actionability: a test can click a hidden or covered button. UI bugs that only show in a real browser go unnoticed.
- Some browser APIs are missing or behave differently (e.g.
requestAnimationFrameis simulated).
Mounting
Same as in Browser Mode: TestBed.createComponent(Component, { bindings }), fixture ignored, fakes provided through TestBed.
Querying and Asserting
Query with screen from @testing-library/dom, not @testing-library/angular: Angular Testing Library's async utilities (findBy*, waitFor) trigger change detection, which hides synchronization bugs that would break the app once zoneless.
import { screen } from '@testing-library/dom';
await expect
.poll(() => screen.queryAllByRole('heading').map((el) => el.textContent))
.toEqual(['Burger']);- Use the same query priority as in Browser Mode: role, label, placeholder, text, alt or title, test id.
findBy*queries from@testing-library/domalso retry without triggering change detection.toHaveTextContent(),toBeDisabled(), etc. require@testing-library/jest-domin the test setup file.- Don't use
fixture.detectChanges()orfixture.whenStable(): retry the assertion instead.
Interacting
import { userEvent } from '@testing-library/user-event';
await userEvent.type(screen.getByLabelText('Keywords'), 'Burger');
await userEvent.click(screen.getByRole('button', { name: 'ADD' }));With fake timers in manual mode, let userEvent advance them:
const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTimeAsync });
await user.type(screen.getByLabelText('Keywords'), 'Burger');Migrating to Browser Mode
Prefer Browser Mode for component tests: see setup.md. Migrate progressively:
- Run the existing tests in the browser ("partial" Browser Mode).
- Replace the
@testing-library/user-eventimport withimport { userEvent } from 'vitest/browser'to route interactions through the browser provider. - Replace DOM queries and assertions with
pagelocators andexpect.elementin new or flaky tests.