Slide 1

Slide 1 text

With Factories Supercharge your Tests Chris Ball @cball_

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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