For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /api/testing-library/render.md.

render()

Renders a component, or a directive on a Lynx element, as a Lynx app in a unit test, and waits for Angular to update it. import { render, renderDirective, waitForUpdate } from '@blotch/angular-lynx-testing-library';

render(component, options?)

function render<Q extends Queries = typeof queries, C = unknown>(
  component: Type<C>,
  options?: RenderOptions<Q, C>,
): Promise<RenderResult<Q, C>>;

Bootstraps component as a Lynx app and renders it into the test's DOM. It resolves once the first render is done.

const { getByText, rerender } = await render(Greeting, {
  inputs: { name: 'Lynx' },
});
expect(getByText('Hello, Lynx')).toBeTruthy();

await rerender({ inputs: { name: 'Angular' } });
expect(getByText('Hello, Angular')).toBeTruthy();
  • It starts clean. It first destroys the app a previous render() left, and clears document.body.
  • Providers: the app gets provideRenderer() and your options.providers. Change detection is zoneless, Angular's default, as in your app.
  • The tree: elements render under a <page> element, the result's container.
  • The splash screen: like bootstrapApplication(), it hides the splash screen once the component is rendered.
    • It doesn't when options.providers has provideSplashScreen().
    • A spec reads LynxSplashScreen.visible() to check when the app hides it.

options.inputs

inputs are set before the first change detection, as a parent template binds them.

  • A required input has its value when the template first reads it.
  • ngOnInit sees them all.
  • render() rejects a name that isn't one of the component's inputs, and lists the ones it has.
  • Change them later with rerender({ inputs }).

options.bindings

bindings are Angular's own, as TestBed.createComponent() and createComponent() take them. Use them to hear an output or to bind a model() to a signal.

import { outputBinding, signal, twoWayBinding } from '@angular/core';

const picked: string[] = [];
const value = signal(2);
await render(Counter, {
  bindings: [
    outputBinding<string>('picked', (label) => picked.push(label)),
    twoWayBinding('value', value),
  ],
});
  • outputBinding() listens from the moment Angular creates the component. It hears an output emitted in ngOnInit, which subscribing on componentRef.instance after render() would miss.
  • twoWayBinding() sets the input from the signal, and sets the signal when the component emits its Change output. Set the signal, then await waitForUpdate(), to change the component.
  • inputBinding() works too. Prefer inputs unless the value comes from a signal: render() checks each name in inputs, but it can't read a binding's name.
  • A wrong name: an output the component doesn't have fails render() with Angular's error (NG0316). A wrong input name in a binding goes only to the ErrorHandler.
  • componentRef.setInput() fails after an inputBinding() or twoWayBinding(), as after inputs (NG0317). Output bindings leave it working.

options.initialURL

initialURL is the link that started the app, such as myapp://orders/42.

  • LynxLinking.getInitialURL() returns it.
  • With provideRouter(), the app's first screen is the link's route, with nothing under it.
  • A link the router has no route for starts the app at /, as on a phone.
  • Left out, getInitialURL() is null.
  • A link that arrives while the app is open is openLink()'s.

renderDirective(directive, options?)

function renderDirective<Q extends Queries = typeof queries, D = unknown>(
  directive: Type<D>,
  options?: RenderDirectiveOptions<Q, D>,
): Promise<RenderDirectiveResult<Q, D>>;

Renders a standalone directive on a Lynx element, with no host component to write. It's the Lynx counterpart of Angular's TestBed.createDirective(), as render() is of TestBed.createComponent().

@Directive({
  selector: '[appHighlight]',
  host: { '[style.background-color]': 'color()' },
})
class Highlight {
  readonly color = input('yellow');
}

const { container, directiveInstance, rerender } = await renderDirective(
  Highlight,
  { tagName: 'view', inputs: { color: 'red' } },
);
const view = container.firstElementChild!;
expect(view.getAttribute('style')).toContain('background-color: red');

await rerender({ inputs: { color: 'blue' } });
expect(directiveInstance.color()).toBe('blue');
  • The element: options.tagName, such as 'view'. Left out, it's the element the selector names: text[appBold] goes on a <text>.
  • A selector with no element or several needs tagName. [appHighlight] names none, and view[appX], text[appX] names two. Without tagName, it throws Angular's error for each.
  • The tree: the element is the only child of the <page>, the result's container.
  • The rest is render()'s. inputs, bindings, providers, queries and initialURL work as they do there, and so do rerender({ inputs }), fireEvent, waitForUpdate() and cleanup().
  • It rejects a component, pointing to render(), and render() rejects a directive, pointing here.

Unlike TestBed.createDirective(), the directive's element isn't the root. On the web, the fixture's nativeElement is the directive's element. A Lynx app's root is always a <page>, which can't be a <view> or a <text>, so the element sits inside it. Read it as container.firstElementChild.

Unlike TestBed.createDirective(), it takes only standalone directives. TestBed applies one declared in an NgModule through an option Angular keeps private. renderDirective() uses Angular's public createComponent() instead, so an Angular update can't break it, and that applies only standalone directives (NG0907). Directives are standalone unless they say standalone: false, so remove that, as render() asks of components.

It takes inputs next to bindings. As with render(), you key input values by name, and it binds each with inputBinding(). Pass outputBinding() and twoWayBinding() in bindings, as for TestBed.createDirective().

cleanup()

function cleanup(): void;

Destroys the rendered app.

  • It runs after every test, so you rarely call it.
  • To turn that off, set the environment variable ATL_SKIP_AUTO_CLEANUP=true.

waitForUpdate()

function waitForUpdate(): Promise<void>;

Waits one task (a setTimeout of 0), so the change detection Angular scheduled can run. Then it runs change detection once more.

Await it after fireEvent or after setting a signal, before you check the rendered output.

Types

RenderOptions

type RenderOptions<Q extends Queries = typeof queries, C = unknown> = {
  providers?: (Provider | EnvironmentProviders)[];
  queries?: Q;
  inputs?: RenderInputs<C>;
  bindings?: Binding[];
  initialURL?: string;
};
PropertyDescription
providersExtra providers for the test app, such as provideRouter(routes)
queriesCustom queries to bind to container, next to the library's queries
inputsValues for the component's inputs, keyed by the name a template binds. An aliased input takes its alias
bindingsAngular's outputBinding(), twoWayBinding() and inputBinding(). See options.bindings
initialURLThe link that started the app. See options.initialURL

RenderInputs

type RenderInputs<C = unknown> = {
  [K in keyof C]?: InputValue<C[K]>;
} & Record<string, unknown>;

Each of the component's properties takes the value its input does.

  • For readonly count = input(0), inputs: { count: 'three' } doesn't compile.
  • Other names are allowed too, such as class for input('', { alias: 'class' }).
  • render() checks every name when the test runs.

RenderResult

PropertyTypeDescription
containerElementThe <page> element that holds everything rendered
componentRefComponentRef<C>The root component. Use componentRef.injector.get(Token) to reach a service
unmount() => voidDestroys the app, like cleanup()
rerender({ inputs }) => Promise<RenderResult>Changes inputs render() set, keeping the component. See below
rerender(component, options?) => Promise<RenderResult>Renders another component in its place
asFragment() => DocumentFragmentA copy of container, for snapshot tests
getByText, …Query functionsEvery query in queries, bound to container, plus your custom ones

rerender({ inputs }) sets the inputs you pass, runs change detection, and resolves when the tree shows them. The component keeps its state.

  • Inputs you leave out keep their values. Angular has no way to put an input back to its initial value.
  • It only changes inputs render() set. Angular sets those through input bindings, and a component with input bindings takes no other inputs. To change an input later, give render() its starting value.
  • componentRef.setInput() fails after render() sets inputs, for the same reason (NG0317). Use rerender({ inputs }).
Plain Vitest can't set signal inputs

ng test compiles components ahead of time, so inputs, bindings, rerender({ inputs }) and componentRef.setInput() all reach input(), model() and output().

Plain Vitest compiles components at runtime, where Angular sees only @Input(), @Output() and inputs and outputs metadata. Set a writable signal on componentRef.instance there instead.

RenderDirectiveOptions

type RenderDirectiveOptions<
  Q extends Queries = typeof queries,
  D = unknown,
> = RenderOptions<Q, D> & {
  tagName?: string;
};

render()'s options, plus:

PropertyDescription
tagNameThe element the directive goes on, such as 'view'. Left out, the one its selector names, as for createDirective()

RenderDirectiveResult

PropertyTypeDescription
containerElementThe <page> element. The directive's element is its firstElementChild
directiveInstanceDThe directive, as on Angular's DirectiveFixture
injectorInjectorThe injector of the directive's element. Use injector.get(Token) to reach a service
unmount() => voidDestroys the app and the directive, like cleanup()
rerender({ inputs }) => Promise<RenderDirectiveResult>Changes inputs renderDirective() set, keeping the directive, as render()'s does
asFragment() => DocumentFragmentA copy of container, for snapshot tests
getByText, …Query functionsEvery query in queries, bound to container