@GregOnNet
Gregor Woiwode, CTO
Find me at the conference and say hi!
Slide 10
Slide 10 text
β π€ What is it about?
β 𧬠Harness Anatomy API
β π§π»βπ» Live Coding
β π Learnings
Agenda
Slide 11
Slide 11 text
π€ What is it about?
β Helps testing complex controls
β Makes test more robust
β against internal changes to
DOM-Elements, Styles, Properties
Slide 12
Slide 12 text
π€ Code vs. Runtime
Slide 13
Slide 13 text
π€ Code vs. Runtime
is rendered outside
the .
Slide 14
Slide 14 text
Thinking of a new test strategy for
each component is time costly.
Slide 15
Slide 15 text
π€ Wouldnβt this be better β¦?
Reliable, comprehensible test APIs.
Slide 16
Slide 16 text
π₯³ Introducing Component Harness
https://material.angular.io/cdk/test-harnesses/overview
https://martinfowler.com/bliki/PageObject.html
β Every Material Component comes with its own Test API.
β Component harness is inspired by PageObject-Pattern.
Slide 17
Slide 17 text
π€ Where to find it?
https://material.angular.io/components/select/api#material-select-testing-classes
Slide 18
Slide 18 text
𧬠Set up | ComponentFixture
Slide 19
Slide 19 text
𧬠Set up | Loader
Slide 20
Slide 20 text
𧬠Set up | Get Harness
Slide 21
Slide 21 text
𧬠Set up | Use Harness
Slide 22
Slide 22 text
𧬠Component Harness
β’ ComponentFixture is feeded into a Loader
β’ Loader provides respective ComponentHarness
β’ ComponentHarness
β’ interacts with Material Component
β’ reads state from Material Component
β’ has asynchronous API.
π Component Harness
β’ Less code compared to vanilla approach
β’ Readability of the test has improved
β’ Testing the Material Component got a no brainer
β’ e.g., No need to trigger change detection manually
Slide 26
Slide 26 text
π Component Harness
with(β¦) gives you
the possibility for fine
grained harness-
selection.
Be careful putting the
data-test-Attribute
to the right element.
Slide 27
Slide 27 text
𧬠ComponentHarnesses are similar
Slide 28
Slide 28 text
DEMO
MatTableHarness
Slide 29
Slide 29 text
Testing Components in
isolation might reveal issues
you do not spot
in the Browser.
Slide 30
Slide 30 text
@angular-extensions/pretty-html-log is a handy
tool
beautifying test logs.
https://github.com/angular-extensions/pretty-html-log
Slide 31
Slide 31 text
𧬠Optimize ChangeDetection
Slide 32
Slide 32 text
𧬠Optimize ChangeDetection
β’ Is it worth doing it? β Itβs worth measuring it.
β’ In the repository to this talk you can see that tests using
parallel are around 20ms faster than executing each
operation sequentially.
Slide 33
Slide 33 text
𧬠Not everything can be parallized
Slide 34
Slide 34 text
𧬠Not everything can be parallized
Slide 35
Slide 35 text
DEMO
parallel
Slide 36
Slide 36 text
𧬠Make ComponentHarness your own
Slide 37
Slide 37 text
𧬠Make ComponentHarness your own
Please remember, if
you introduce
logic/branching in
test-abstractions you
need to test these,
too.
@testing-library/angular
β’ ComponentHarness relies on ComponentFixture.
β’ Render gives us the ComponentFixture.
β’ Thatβs why the integration is easy.
β’ Testing Library simplifies Component initialization.
Slide 42
Slide 42 text
DEMO
Testing Library
Slide 43
Slide 43 text
Cypress
β’ ComponentHarness can be implemented for various platform.
β’ The respective test platform needs to implement its version of
EnvironmentLoader.
β’ Cypressβ integration is brought to us by the community.
β’ @jscutlery/cypress-harness
https://github.com/jscutlery/devkit/tree/main/packages/cypress-harness
Slide 44
Slide 44 text
Cypress
Slide 45
Slide 45 text
DEMO
Cypress
Slide 46
Slide 46 text
π Component Harness
β’ β οΈ Component Testing is quiet in an early stage.
β’ π The Developer Experience looks promising.
Slide 47
Slide 47 text
Pros & Cons
β’ π Typed API
β’ π Consistant API
β’ π Robust Tests
β’ π Platform Agnostic
β’ π€ Cypress Integration flaky
β’ π€ Sometimes API is unsafe to use
β’ π€ Test-Errors confuse sometimes
Slide 48
Slide 48 text
Summary
- APIs of ComponentHarness make me feel safer.
- Writing Tests for complex components is quicker.
- Platform agnostic architecture is a door opener.
βThe Angular-Team introduced an additional tool allowing
us to write more robust applications.
Slide 49
Slide 49 text
The idea of Test Harness can
be leveraged for other
UI-libraries or in
backend projects.
Slide 50
Slide 50 text
Gregor Woiwode, CTO
https://github.com/co-IT/talk-dwx-2023-angular-test-harness
Slides & Code
Slide 51
Slide 51 text
Attributions
β Photo by Aren Nagulyan on Unsplash
β Photo by Dawid on Unsplash
β Photo by Khara Woods on Unsplash
β Photo by Stephen Hocking on Unsplash
β Photo by Chris Liverani on Unsplash