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

Supercharge your Tests with Factories

Supercharge your Tests with Factories

This talk will explore the factory pattern and how to leverage it to greatly improve your automated tests. By leaning on Prisma's ORM features and TypeScript types, you'll be able to quickly and painlessly create the exact data your tests need.

Sound interesting? Join our discussion at https://github.com/echobind/prisma-factory!

6254dc2b7e4f26b2ab5d05c560834671?s=128

Chris Ball

June 30, 2021
Tweet

Transcript

  1. With Factories Supercharge your Tests Chris Ball @cball_

  2. Part 1: Request Tests (Integration Tests) Part 2: Resetting the

    Database & Creating Data Part 3: The Factory Pattern & Prisma Part 4: E2E Tests Part 5: Tips & Tricks Tests ❤ Factories, Factories ❤ Supercharge your tests with Factories Prisma Day 2021 by Chris Ball 02
  3. 03 Unit Tests Please write them. We're focusing higher today.

    Supercharge your tests with Factories Prisma Day 2021 by Chris Ball
  4. 03 Request Tests API Layer. aka Integration Tests. Request ➡

    Your Code ➡ Database ➡ Your Code ➡ Response Supercharge your tests with Factories Prisma Day 2021 by Chris Ball
  5. 03 Supercharge your tests with Factories Prisma Day 2021 by

    Chris Ball
  6. 03 Supercharge your tests with Factories Prisma Day 2021 by

    Chris Ball
  7. 03 Supercharge your tests with Factories Prisma Day 2021 by

    Chris Ball
  8. 03 Supercharge your tests with Factories Prisma Day 2021 by

    Chris Ball Inline Snapshots are great! (if used properly)
  9. 03 Supercharge your tests with Factories Prisma Day 2021 by

    Chris Ball
  10. 03 Creating Valid Data In Request Tests Supercharge your tests

    with Factories Prisma Day 2021 by Chris Ball
  11. 03 Supercharge your tests with Factories Prisma Day 2021 by

    Chris Ball
  12. 03 Supercharge your tests with Factories Prisma Day 2021 by

    Chris Ball
  13. 03 Supercharge your tests with Factories Prisma Day 2021 by

    Chris Ball
  14. 06 And nothing more. Create the data you need for

    that test Check errors, edge cases, proper responses Make a request (as a user or a guest) and assert the response Before each test, make sure the database is reset. Start each test with a clean slate. Reset the database Biggest bang-for-buck at API layer Use to test all permissions and edge cases! Request Tests Supercharge your tests with Factories Prisma Day 2021 by Chris Ball
  15. 03 The Factory Pattern Make it easier to create valid

    data. Supercharge your tests with Factories Prisma Day 2021 by Chris Ball
  16. 03 Factories? A simple object maker. Supercharge your tests with

    Factories Prisma Day 2021 by Chris Ball
  17. 03 Supercharge your tests with Factories Prisma Day 2021 by

    Chris Ball
  18. None
  19. 03 Supercharge your tests with Factories Prisma Day 2021 by

    Chris Ball
  20. 03 Supercharge your tests with Factories Prisma Day 2021 by

    Chris Ball
  21. 03 Supercharge your tests with Factories Prisma Day 2021 by

    Chris Ball
  22. 03 Supercharge your tests with Factories Prisma Day 2021 by

    Chris Ball
  23. 03 Supercharge your tests with Factories Prisma Day 2021 by

    Chris Ball
  24. 03 Why Factories? Less brittle tests. Update in a single

    place! Supercharge your tests with Factories Prisma Day 2021 by Chris Ball
  25. 06 This reduces the amount of code you need to

    write. Add a new required attribute? Just update the factory. Add defaults for required attributes This lets you use the patterns we just discussed. Use Prisma types for type safety! Add an object that exports build and create functions ex: Chance.js. Comes in handy with E2E tests. Long or unexpected names can break layout. Avoids false confidence from copy/pasta! Use a faker library for random values Make your life easier when creating data for your tests. Factories Supercharge your tests with Factories Prisma Day 2021 by Chris Ball
  26. 03 E2E Tests Frontend Layer. Runner ➡ Browser ➡ Your

    Code ➡ API call ➡ Your Code ➡ Browser Supercharge your tests with Factories Prisma Day 2021 by Chris Ball
  27. 03 Factories in E2E Example using Cypress. Supercharge your tests

    with Factories Prisma Day 2021 by Chris Ball
  28. 03 Supercharge your tests with Factories Prisma Day 2021 by

    Chris Ball
  29. 03 Supercharge your tests with Factories Prisma Day 2021 by

    Chris Ball
  30. 03 Tips & Tricks Just a few, don't worry. Supercharge

    your tests with Factories Prisma Day 2021 by Chris Ball
  31. Don't put relationships in your factory functions. Randomize your eunms

    Leverage fake data to find edge cases You create additional records you may not need. Same as above Always using known quanities can create bugs Pass in all relationships via Prisma's connect / create methods. chance.pickone(Object.values(MyEnum)) Use the great functions provided by chance (or your chosen fake library). 07 Tips & Tricks for Factories in Prisma Tip Why? How? Supercharge your tests with Factories Prisma Day 2021 by Chris Ball
  32. 03 Supercharge your tests with Factories Prisma Day 2021 by

    Chris Ball Tests ❤ Factories, Factories ❤
  33. 12 Interested? Want to help with this? Supercharge your tests

    with Factories Prisma Day 2021 by Chris Ball Discussion at: https://github.com/echobind/prisma-factory
  34. These patterns and more. Echobind's Default Web Template. https://github.com/echobind/bisonapp 12

    Supercharge your tests with Factories Prisma Day 2021 by Chris Ball
  35. @cball_ 11 Thanks! Supercharge your tests with Factories Prisma Day

    2021 by Chris Ball