React Tests

Unit Testing

recap

Goals / why write tests

  • find bugs?
  • prevent bugs
  • help to understand code
  • keep jr. devs busy :)

What is unit?

  • function? class? module?
  • entire app?
  • entire app + environment?

Terms

  • mocks / stubs
  • code coverage
  • unclear scope
  • no assertions
  • fail randomly
  • hardly readable
  • adapt to the code
  • excessive mocks
  • it's clear what unit is
  • one assertion*
  • fail only when something is broken
  • look like documentation
  • shape the code
  • no mocks

React Testing

1. Read the docs

2. Test a real project

let unit = 'function';
  1. extract complex pieces of logic
  2. test them
let unit = 'component';
  1. Create a <Component with='props' />
  2. Run asserts on it...
  • via DOM API with jsdom
  • make html snapshot
  • compare screenshots
let unit = 'story';

backstopjs + storybook = a match made in heaven

let unit = 'application';

Out of today's scope.

When needed - take a look at cypress.io

https://youtu.be/gK5I7bq72Fw?t=3814 - react2020q1

React Tests

By Artem Sinicyn

React Tests

  • 42