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. With Factories
    Supercharge your
    Tests
    Chris Ball
    @cball_

    View Slide

  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

    View Slide

  3. 03
    Unit Tests
    Please write them. We're
    focusing higher today.
    Supercharge your
    tests with Factories
    Prisma Day 2021
    by Chris Ball

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  10. 03
    Creating Valid Data
    In Request Tests
    Supercharge your
    tests with Factories
    Prisma Day 2021
    by Chris Ball

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  15. 03
    The Factory Pattern
    Make it easier to create valid data.
    Supercharge your
    tests with Factories
    Prisma Day 2021
    by Chris Ball

    View Slide

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

    View Slide

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

    View Slide

  18. View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  27. 03
    Factories in E2E
    Example using Cypress.
    Supercharge your
    tests with Factories
    Prisma Day 2021
    by Chris Ball

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  32. 03
    Supercharge your
    tests with Factories
    Prisma Day 2021
    by Chris Ball
    Tests ❤ Factories,
    Factories ❤

    View Slide

  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

    View Slide

  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

    View Slide

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

    View Slide