Meetup React Sanca - 29/11/18 - React Testing

AugustoLzaro 234 views 19 slides Dec 02, 2018
Slide 1
Slide 1 of 19
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19

About This Presentation

Talk a respeito de testes automatizados no frontend (E2E, integração, unitários e visual), dando destaque a aplicações React. A talk foca na utilização da biblioteca react-testing-library desenvolvida por Kent C. Dodds em 2018. Nela, explico o porquê de optarmos por ela, em quais casos ela m...


Slide Content

REACT TESTING
react-testing-library

AGENDA
●What to test
●Use cases
●Shallow rendering vs. DOM
●react-testing-library (RTL)
●Examples
●Pros
●Cons
●Visual regression tests

AUGUSTO
LAZARO
Software Engineer @ Entria
React, React Native, GraphQL, Relay,
MongoDB, Koa, TypeScript...


GitHub: @augustolazaro
Twitter: @augustoeml
Email: [email protected]

WHAT TO TEST*
e2e
Integration
Unit
enzyme ( )
react-testing-library

Shallow
rendering
DOM
VS.

Shallow
rendering
DOM
VS.
Coder User

react-testing-library

●https://github.com/kentcdodds/react-testing-library
●Kent C. Dodds
●Utility functions on top of react-dom and react-dom/test-utils
●DOM testing

“The more your tests resemble the way your software is used, the more
confidence they can give you.”

API
●getByText: Fetches a node by the node’s text content
●getByLabelText: Fetches a node by its corresponding HTML <label/>text.
●getByAltText: Fetches an image node by its alt text
●getByPlaceholderText: Fetches a text input by its placeholder text.
●getByTextId: An “escape hatch” for fetching nodes by its
data-testidattribute in cases where there is no text to fetch by.

dom-testing-library API
●wait
●waitForElement
●fireEvent: Fire DOM events

UTILS
●rerender: this function can be used to update props of the rendered
component
●unmount: This will cause the rendered component to be unmounted
●debug: console.log(prettyDOM(baseElement))

PROS
●Built-in wait command
●API focuses on fetching by visible text on the page
●Also available to React Native

CONS
●Mock is not that simple
●So as test different variations of a page

*Kent C. Dodds appreciation slide*

*WHAT TO TEST
Visual regression testing
●Based in snapshots
●Test what users see
●Code changes doesn’t matter
●Loki, BackstopJS, Diferencify

Thanks!

Any feedback?

entria.feedback.house/augusto.lazaro