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

TDD In React Using Enzyme

r31gN_
July 25, 2016

TDD In React Using Enzyme

r31gN_

July 25, 2016
Tweet

More Decks by r31gN_

Other Decks in Technology

Transcript

  1. The “Oh, sh*t” summer collection 2016 • Fixing a bug

    that caused numerous issues in other parts of the programme • Not knowing that your code broke stuff • Fear of changing code • Fear of removing obsolete/unused code • Realising you have designed a bad API/interface for others to consume • Realising you have a pile of hard to read spaghetti code
  2. TDD • Write new code only if you have an

    automated failing test • Eliminate duplication
  3. RED Write a test that doesn’t work GREEN Make the

    test pass (sins allowed) REFACTOR Eliminate all duplication
  4. TDD • It’s about the mindset of continuous improvement •

    Tests are a means of achieving that, of accommodating your brain with this flow
  5. Enzyme • It’s a JS testing utility for React •

    Easy to work with, jQuery like traversal • Easy to integrate with test runners, assertion libraries and build process (Karma, Mocha, Chai, Browserify, Webpack, etc.) • Popularity, good maintainance & support, good docs, community, backed by AirBnB
  6. The stack • Babel (React preset and all…) • React

    latest • Mocha, Sinon, Chai • Test utils • Jsdom (needed for mounting components) • Enzyme • https://github.com/r31gN/tdd-react-enzyme
  7. Shallow rendering • It considers your component as a unit

    • It does not render children components (renders “one level deep”) • It does not require a DOM (no mounting, no lifecycle components, etc.) • Fast • Use it for making markup related asserts
  8. Full rendering (mounting) • It renders child components as well

    • Requires a DOM API to be present (use jsdom for that) • Slow • Ideal for use cases when you have components that interact with the DOM API, use lifecycle hooks (eg: componentDidMount), etc.
  9. Static rendering • It renders React components to static HTML

    for analysis • Uses Cheerio under the hood • Work with the resulting HTML - find(), text(), etc.
  10. TDD with Enzyme • Remember the TDD cycle - Red/Green/Refactor

    • Set this as a mindset • Mix static with full rendering (they serve different purposes) • Enzyme is very friendly (very nice API, well documented) • You will have to pair it with other technologies: either go for a granular stack (Mocha + Chai + Sinon) or opt for something like Jasmine (comprehensive, lots of functionality under one roof)