Testing
Test your components with ng test. @blotch/angular-lynx-testing-library renders a component through AngularLynx's real renderer into jsdom, a browser-like environment that runs in Node, and gives you Testing Library queries to check the result.
Set up
- What it does. It installs the testing library, plus Vitest and jsdom if the project doesn't have them. It also makes sure
ng testruns through the AngularLynx test builder. - No config files.
ng testloads the library's setup by itself whenever the project has the library. You don't write a Vitest config or a setup file. - New components come with a test. From then on,
ng generate componentadds a*.test.tsthat renders the component withrender().
To install the library by hand instead:
Run the tests
- The builder.
ng addpoints thetesttarget inangular.jsonat@blotch/angular-lynx-build:unit-test. It's Angular's own unit-test builder (Vitest), set up for Lynx, so it takes the same options:include,coverage,setupFiles,providersFileand the rest. - Test files. It runs every
*.spec.tsand*.test.tsfile. Angular'sTestBedworks too, so the specs thatng generate serviceorng generate pipewrites run as they are. A spec that uses onlyTestBedruns in jsdom, as a web app's does, which has noEventSourceorIntersectionObserver: the runtime gives those to a spec that imports@blotch/angular-lynx-testing-library, or the spec stubs them. - Strict templates. Tests compile ahead of time, like the app, and their templates are checked strictly. A component that uses Lynx elements must import
LYNX_ELEMENTSfrom@blotch/angular-lynx. Generated components already do.
Render a component
render() starts a standalone component the way your app starts, and returns queries bound to what it rendered:
render()is async. Await it. The first render has finished when it resolves.- Lynx elements are real elements in the test. A
<view>renders as a<view>element and a<text>as a<text>element, soquerySelector('text')works. - Change detection and the renderer are set up for you. You don't add
provideRenderer(), and change detection is zoneless, as in your app. - To test a directive on its own, render it on a Lynx element with
renderDirective(), asTestBed.createDirective()does on the web. See Test a directive.
Pass providers to swap in test values for your app's dependencies:
Testing patterns uses this to fake native APIs.
Providers every spec needs, such as the app's provideIcons(), go in the file the test target names as providersFile. ng test gives them to every render(), as to every TestBed test, and a spec's own providers override them:
Query what rendered
Use getBy* when the element must exist, and queryBy* when it may be absent:
The library also exports screen, which runs queries against the whole document.
Find elements as a screen reader does
Lynx elements describe themselves to screen readers with accessibility-* attributes, not ARIA. The queries read them:
getByRolereadsaccessibility-traits:button,link,imageasimg,headerasheading. The API reference has the full list.nameis theaccessibility-label, or a<text>'s own text.- Screen readers must reach the element. A
<view>needsaccessibility-element="true", so the test fails when people using a screen reader couldn't find the button. getByAccessibilityLabelfinds any element by itsaccessibility-label.
render() also returns:
container: the root<page>element that holds everything rendered.componentRef: the component'sComponentRef, for reading its instance or its injector.rerender({ inputs }): changes the inputsrender(Component, { inputs })set, keeping the component. See Set inputs.rerender(Component, options?): renders another component in its place.asFragment(): a copy of the tree, for snapshot tests.unmount(): destroys the app. The library also cleans up after every test by itself.
The API reference lists every option and type.
What jsdom tests can't cover
The tests run your app in jsdom, not on a phone. Some things only exist on a device:
- Native APIs. A test has none of your app's native modules, just as Lynx Explorer hasn't.
LynxNativeModule.call()resolvesundefined, a native module's methods reject, andLynxTextMeasure.measure()throws.LynxLocalStorageandLynxSecureStoragekeep their values in memory. Provide fakes, as in Fake native APIs. - Data from the host app. No host app runs, so InitData and GlobalData hold nothing unless you provide them. See Fake host data.
- Background-thread APIs. The test's background thread answers the calls AngularLynx sends there, but it has few of Lynx's APIs, so a call that needs one it lacks, such as
LynxAccessibility.announce(), rejects. Your background functions run there, sorunOnBackground()resolves with what the function returns, or rejects with what it throws, as on a device. Like any code on that thread, a background function sees none of your app's native modules. See Threads. - Main-thread scripts and gestures. jsdom has no native elements to move, so test dragging and gestures on a device.
- Layout and native rendering. jsdom computes no layout, so sizes and positions are unknown, and layout events don't fire by themselves.
Check these by running the app in Lynx Explorer, or on a simulator with Run on iOS and Android.
Related
- Testing patterns: signals, events, snapshots and fakes.
- Event reference: every event
fireEventcan send. - Testing library API
