I want to test my API with jest. Before executing the tests of a test file I want to reset my database using a npm command like this:
import { execSync } from 'child_process'
import prisma from 'lib/prisma'
import { foods } from 'prisma/seed-data'
import { getFoods } from './foods'
beforeAll(() => {
execSync('npm run db:test:reset')
})
afterEach(async () => {
await prisma.$disconnect()
})
test('getFoods', async () => {
const fetchedFoods = await getFoods()
expect(fetchedFoods).toEqual(foods)
})
The npm run db:test:reset excecutes this script:
echo "DROP DATABASE foodplanet WITH (FORCE); CREATE DATABASE foodplanet;" | sudo docker exec -i foodplanet_test psql -U postgres
cat prisma/backups/test/backup.sql | sudo docker exec -i foodplanet_test psql -U postgres foodplanet
Everything works fine if I run this test file by itself. However if I execute all tests there are issues with multiple tests executing this script at the same time. Running the tests sequentially with -i still results in errors like:
ERROR: relation "Account" already exists
or
ERROR: database "foodplanet" does not exist
I get different errors every time I run my unit test therefore I think there are still multiple instances trying to execute this script at the same time.
What could be the reason that the tests doesn't get executed sequentially?