Skip to content

Commit

Permalink
test: copy over db instead of cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kentcdodds committed Oct 1, 2024
1 parent 0d44092 commit 7b16bdc
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions tests/setup/db-setup.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import path from 'node:path'
import fsExtra from 'fs-extra'
import { afterAll, afterEach, beforeAll } from 'vitest'
import { cleanupDb } from '#tests/db-utils.ts'
import { afterAll, beforeEach } from 'vitest'
import { BASE_DATABASE_PATH } from './global-setup.ts'

const databaseFile = `./tests/prisma/data.${process.env.VITEST_POOL_ID || 0}.db`
const databasePath = path.join(process.cwd(), databaseFile)
process.env.DATABASE_URL = `file:${databasePath}`

beforeAll(async () => {
beforeEach(async () => {
await fsExtra.copyFile(BASE_DATABASE_PATH, databasePath)
})

// we *must* use dynamic imports here so the process.env.DATABASE_URL is set
// before prisma is imported and initialized
afterEach(async () => {
await cleanupDb()
})

afterAll(async () => {
// we *must* use dynamic imports here so the process.env.DATABASE_URL is set
// before prisma is imported and initialized
const { prisma } = await import('#app/utils/db.server.ts')
await prisma.$disconnect()
await fsExtra.remove(databasePath)
Expand Down

2 comments on commit 7b16bdc

@devneill
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so because we've moved the required seed data into our migrations, we have all the seed data we need already inside the base db!

Nice!

@kentcdodds
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep! Much much better setup now!

Please sign in to comment.