Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Lessons Learnt with Visual Testing and Snapshots

Lessons Learnt with Visual Testing and Snapshots

Sendil Kumar N

April 27, 2021
Tweet

More Decks by Sendil Kumar N

Other Decks in Technology

Transcript

  1. I am Sendi tweets @sendi kumarn mai @ he o@sendi

    kumarn.com He o @sendi kumarn
  2. Frontend Testing Unit Testing Component Testing Visua Testing # of

    tests High Medium Low Time to write Low Medium High @sendi kumarn
  3. Frontend Testing Unit Testing Component Testing Visua Testing # of

    tests High Medium Low Time to write Low Medium High Confidence to ship Low Medium High @sendi kumarn
  4. Frontend Testing Unit Testing Component Testing Visua Testing # of

    tests High Medium Low Time to write Low Medium High Confidence to ship Low Medium High Time to execute Low Medium High @sendi kumarn
  5. Component Testing Test the functiona ity of the component Manua

    y va idate the functiona ity @sendi kumarn
  6. Snapshot Testing - How it works? > npm run test

    For the first time, it renders the component and automagica y creates a text b ob in a fi e ca ed .snap. > npm run test u Wi update the snapshot @sendi kumarn
  7. Snapshot Testing • It is super easy to maintain testing

    code • It makes tests consistent (no browsers invo ved) • It is magica ✨ • It enormous y reduces time to write/update tests @sendi kumarn
  8. B oats Code base Source Code Testing Code Snapshots Con

    guration 22 28 47 3 approximate on few open source projects Based on Lines of code @sendi kumarn
  9. B oats Pu Requests more ines of code - ess

    effective the code review is @sendi kumarn
  10. Easy to ose context • Test names shou d be

    exp icit • Stricter ru es when writing • Do not b ind y update snapshots @sendi kumarn
  11. As with many other things, they are awesome too s

    when used correct y @sendi kumarn
  12. Don't over do it Do not write snapshot testing everywhere,

    use it on y for presentationa components (without much ogic). @sendi kumarn
  13. Use mount/sha ow correct y • sha ow • do

    not render chi d. • tests constructor / render • mount • renders chi d. • tests componentDidMount @sendi kumarn
  14. Snapshot is code. When reviewing code make sure the snapshots

    are given equa importance and it is code (even though it is autogenerated). @sendi kumarn
  15. Sing e Responsibi ity In genera your tests in snapshot

    shou d have a sing e responsibi ity. Do not over oad snapshot tests. @sendi kumarn
  16. Periodica y revise Revise and update tests when you add

    more features into the component. @sendi kumarn
  17. Unit Testing with snapshot • Faster and easier to write

    / maintain • Takes ess time to run • Can run oca y • Setup is fast and easy @sendi kumarn
  18. Visua Testing He o Ha o Test Suites: 1 failed,

    1 total Tests: 1 failed, 1 total Snapshots: 0 total Time: 1.782 s Ran all test suites. @sendi kumarn
  19. Puppeteer provides a high-level API to control headless Chrome or

    Chromium over the DevTools Protocol. Puppeteer @sendi kumarn
  20. Visua Testing • It provides visua confirmation • It increases

    confidence over what we ship • It is magica ✨ • It reduces time to write tests @sendi kumarn
  21. B oats Code base Source Code Testing Code Screenshots Other

    13 28 56 3 approximate va ue based on few open source projects Based on Fi e size @sendi kumarn
  22. Screenshot dependencies Rendering differs based on OS / Browser /

    aspect ratio Fixes: - Docker Image - CI on different OS - Fixing image size @sendi kumarn
  23. Page State Maintaining the state of page when taking screenshots

    Fixes: - Storybook - Components - Generates static page - Mock the response @sendi kumarn
  24. UI Changes Updating the Screenshot is tedious and time consuming.

    Fixes: - Introduce Screenshot testing on y for the re eased features @sendi kumarn
  25. Others.. Keep the screenshot in the same repo if the

    repo is sma er. Move out when the source code grows. @sendi kumarn
  26. Others.. hand e animations / b inking cursor / spinner

    / progress bar and other motion @sendi kumarn
  27. So far... • Use it for components with ogics •

    Use proper name for the tests. Unit Testing • Use it for presentationa components. • Keep it simp e • Use proper name for the tests. • Treat snapshots as a code. Snapshot Testing • Use it for user facing UI. • Test them for various form-factor / devices / Operating Systems • Use a separate repo if needed. • Run / Test them as a part of dep oyment process. Visua Testing @sendi kumarn
  28. A ways remember, tests shou d increase the confidence to

    re ease and ensure things are working. It shou d not be a burden. Testing @sendi kumarn