Sub topic 14
Running tests in a real browser
This chapter assumes you can already run ng test and write a component test with TestBed, as covered in the earlier chapters. You should also know where the test target lives in angular.json.
Why a real browser
By default, the Vitest runner in Angular's unit-test builder runs your specs in Node.js. Node has no DOM of its own, so a simulated DOM (jsdom) stands in for it. This is fast and needs no browser install. It also has limits, because jsdom does not do real layout or rendering.
Things jsdom cannot report faithfully:
- Element sizes and positions, such as
getBoundingClientRect(). jsdom returns zeros. - Computed styles that depend on a real stylesheet cascade or media queries.
- Focus behavior, scrolling, and browser-only APIs such as
ResizeObserverorIntersectionObserver.
Browser mode runs the same specs inside an actual browser, so these checks reflect what users see.
Install a browser provider
Vitest needs a provider package that can start and control a browser. Playwright is a common choice. Install it as a dev dependency, then download the browser binaries.
npm install --save-dev @vitest/browser-playwright playwright
npx playwright install chromiumCheck the Angular documentation for the provider packages your CLI version supports.
Select a browser
Pass the browser on the command line:
ng test --browsers=chromiumFor CI machines without a display, use a headless variant:
ng test --browsers=ChromiumHeadlessTo make the choice permanent, add browsers to the target options. Your test files stay the same.
{
"test": {
"builder": "@angular/build:unit-test",
"options": {
"runner": "vitest",
"browsers": ["ChromiumHeadless"]
}
}
}A test that needs a browser
This spec measures a rendered button from the earlier counter component. Under jsdom the width is 0 and the test fails. In a real browser the button has a measurable size.
import { TestBed } from '@angular/core/testing';
import { describe, it, expect } from 'vitest';
import { CounterComponent } from './counter.component';
describe('CounterComponent layout', () => {
it('renders the button with a real size', () => {
const fixture = TestBed.createComponent(CounterComponent);
fixture.detectChanges();
const button: HTMLButtonElement = fixture.nativeElement.querySelector('button');
const rect = button.getBoundingClientRect();
expect(rect.width).toBeGreaterThan(0);
expect(rect.height).toBeGreaterThan(0);
});
});Choosing between the two modes
| Concern | jsdom (default) | Real browser |
|---|---|---|
| Startup and run speed | Fast | Slower, because a browser must launch |
| Setup | None | Provider package and browser binaries |
| Layout, styles, focus | Not reliable | Accurate |
| CI requirements | Node only | Browser installed on the runner |
Most component logic, such as inputs, outputs, and rendered text, behaves the same in both modes. A reasonable approach is to keep jsdom for everyday runs and use a browser for the few specs that depend on layout or rendering. You can also run the whole suite in a browser in CI as an extra check.
Common errors
- The provider package is missing. The run stops with an error about the browser provider. Install the provider package and the browser it controls.
- A spec passes in jsdom but fails in the browser. The spec probably relied on jsdom's simplified behavior. Treat the browser result as the accurate one.
- A headed browser opens on a server. Use the headless variant when no display is available.
Exercise
A spec asserts that a sidebar's getBoundingClientRect().width is greater than 0. It fails with ng test by default but you believe the component is correct. What do you change, and why does the failure happen?
Show answer
Run the tests in a real browser, for example with ng test --browsers=ChromiumHeadless after installing a browser provider and its browser. The default jsdom environment does no real layout, so getBoundingClientRect() returns zeros regardless of the component's CSS.