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!

Chris Ball

June 30, 2021
Tweet

More Decks by Chris Ball

Other Decks in Programming

Transcript

  1. 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
  2. 03 Unit Tests Please write them. We're focusing higher today.

    Supercharge your tests with Factories Prisma Day 2021 by Chris Ball
  3. 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
  4. 03 Supercharge your tests with Factories Prisma Day 2021 by

    Chris Ball Inline Snapshots are great! (if used properly)
  5. 03 Creating Valid Data In Request Tests Supercharge your tests

    with Factories Prisma Day 2021 by Chris Ball
  6. 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
  7. 03 The Factory Pattern Make it easier to create valid

    data. Supercharge your tests with Factories Prisma Day 2021 by Chris Ball
  8. 03 Why Factories? Less brittle tests. Update in a single

    place! Supercharge your tests with Factories Prisma Day 2021 by Chris Ball
  9. 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
  10. 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
  11. 03 Factories in E2E Example using Cypress. Supercharge your tests

    with Factories Prisma Day 2021 by Chris Ball
  12. 03 Tips & Tricks Just a few, don't worry. Supercharge

    your tests with Factories Prisma Day 2021 by Chris Ball
  13. 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
  14. 03 Supercharge your tests with Factories Prisma Day 2021 by

    Chris Ball Tests ❤ Factories, Factories ❤
  15. 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
  16. 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