KEMBAR78
Intro to front-end testing | PDF
Intro to
front-end testing
SpaceStation
2018
by @kangax
Current stack
React
Redux
redux-thunk
CSS components
Radium
Chrome-only
Majority of tests
in SpaceStation
* React
components

* Basic structural
correctness

* Simple behaviour
correctness

* Non-visual style
correctness
Assertions library
Assertions library
Test runner
Assertions library
Test runner
React test utils
Assertions library
Test runner
React test utils
Mocking utils
Assertions library
Test runner
React test utils
Mocking utils
Assertions library
Test runner
React test utils
Mocking utils
Assertions library
Test runner
React test utils
Mocking utils
Assertions library
Test runner
React test utils
Mocking utils
Structural testing
minMaxInput.jsx
Structural testing
minMaxInput-test.jsx
Enzyme renderer
Source file
Fake props
Render component
Structural testing
minMaxInput-test.jsx
Test case (jasmine)
Expectation (jasmine)
Test content (enzyme)
Behavioural testing
minMaxInput.jsx
Validates value
Ensures min < max
Calls onChange prop
Behavioural testing
minMaxInput-test.jsx
Find input
Simulate change event
with a specific value
Check onChange prop was called
Check correct values were set
Jest snapshots
Avatar component
Jest snapshots
Check text contains
user initials
Before
Jest snapshots
Check text contains
user initials
Before
After
Jest snapshots
Structural correctness
avatar-test.jsx.snap
Jest snapshots
Style correctness
avatar-test.jsx.snap
Jest snapshots
Props correctness
avatar-test.jsx.snap
Choose your poison
Renders in virtual DOM Renders in a real browser
Faster (~30sec) Slower (~100sec)
No virtual display req. Needs virtual display
Snapshot support No snapshot support
Testing Redux
Testing Redux
Abstraction.

Connects props to store
via selectors
Abstraction.

Connects props to actions
(potentially thunks)
Abstraction.

Checks auth in store, uses
router
Problem
Testing Redux
Our abstraction for testing
connected components
Middleware
Fake props w. spies
Actual mounting of
component w. middleware,
props, and store
Solution
Testing Redux
Mount component w.
specific "store" values
Ensure deeply nested
component is rendered
Check prop value
Mini redux store
Solution
Testing Redux
Passes in mocked store
A sprinkle of Formsy
Creates wrappers for
enzyme methods and
injects the above
concoction
Some localisation "magic"
A bit more router "magic",
for good measure
Solution
Unit test coverage
Istanbul
coveralls.io
Jest + lcov
Acceptance tests
Acceptance tests
* Actual browser launched
* Can test entire workflows
* Naturally slow
* Flakey tests; data inconsistency
* Currently non-maintained
* Very important to have
Acceptance tests
Acceptance tests
Acceptance tests
Visual regression testing
screener.io
Visual regression testing
Percy
Visual regression testing
Percy
Later instalments
* Testing connected components vs. reducers & actions
independently
* Working w. spies, mocks, stubs (Sinon, Jest)
* Working w. Faker for generating random data
* Making snapshots of connected components
TBD
* Faking timers and promises w. Sinon & Jest
Further reading
Testing in Spacestation
https://github.com/WeConnect/spacestation-v2/blob/master/TESTING.md
Getting started with Jest
https://facebook.github.io/jest/docs/en/getting-started.html
Puppeteer
https://github.com/GoogleChrome/puppeteer
Cypress
https://github.com/cypress-io/cypress
Unit testing redux connected components
https://hackernoon.com/unit-testing-redux-connected-components-692fa3c4441c
Sinon (mocks, stubs, spies)
https://github.com/sinonjs/sinon
Enzyme
http://airbnb.io/enzyme/

Intro to front-end testing