Snapshot Testing

With Jest

Greg Douglas

@gwshark0

What Is Jest?

  • A JavaScript testing framework

  • Developed and used by Facebook

  • Available via npm

Why Use Jest?

  • It is Fast

  • It requires very little configuration

  • It has a great CLI and watch mode

  • It has clear and concise error messages

  • Snapshot tests

Snapshot Tests?

  • Generate serialized snapshots from a test
  • The snapshots are automatically written to a file
  • No manual assertions necessary
  • Subsequent executions of the test compare the current result against the saved snapshot
  • Changes are made by updating the snapshot
  • Snapshots are then committed to version control

DEMO

Why?

Advantages

  • Easy (trivial) to write
  • Reduces friction for writing tests
  • Documents object / component structure
  • Good for testing visual or "spatial" objects
  • Great for code that changes frequently

 

Disadvantages

  • Not as suited for testing complex functional logic
  • Cannot do traditional TDD (red/green/refactor)
  • Cannot do non-deterministic (random) tests
  • Requires vigilance on the tester's part
  • Noise from small changes and versioning

Another tool in the Toolbox.

QUESTIONS?

Snapshot Testing With Jest

By unicode

Snapshot Testing With Jest

Demo: https://github.com/GWShark0/magical-fruits

  • 171