-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
packages/client/tests/functional/query-error-logging/_matrix.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { defineMatrix } from '../_utils/defineMatrix' | ||
|
||
export default defineMatrix(() => [ | ||
[ | ||
{ | ||
provider: 'sqlite', | ||
}, | ||
{ | ||
provider: 'postgresql', | ||
}, | ||
{ | ||
provider: 'mysql', | ||
}, | ||
{ | ||
provider: 'mongodb', | ||
}, | ||
{ | ||
provider: 'cockroachdb', | ||
}, | ||
{ | ||
provider: 'sqlserver', | ||
}, | ||
], | ||
]) |
20 changes: 20 additions & 0 deletions
20
packages/client/tests/functional/query-error-logging/prisma/_schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { idForProvider } from '../../_utils/idForProvider' | ||
import testMatrix from '../_matrix' | ||
|
||
export default testMatrix.setupSchema(({ provider }) => { | ||
return /* Prisma */ ` | ||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
datasource db { | ||
provider = "${provider}" | ||
url = env("DATABASE_URI_${provider}") | ||
} | ||
model User { | ||
id ${idForProvider(provider)} | ||
email String @unique | ||
} | ||
` | ||
}) |
67 changes: 67 additions & 0 deletions
67
packages/client/tests/functional/query-error-logging/tests.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { faker } from '@faker-js/faker' | ||
// @ts-ignore | ||
import type { PrismaClient } from '@prisma/client' | ||
|
||
import { NotFoundError } from '../../../src/runtime' | ||
import { LogEvent } from '../../../src/runtime/getPrismaClient' | ||
import { NewPrismaClient } from '../_utils/types' | ||
import testMatrix from './_matrix' | ||
|
||
let prisma: PrismaClient<{ log: [{ emit: 'event'; level: 'error' }] }> | ||
declare let newPrismaClient: NewPrismaClient<typeof PrismaClient> | ||
|
||
const email = faker.internet.email() | ||
|
||
testMatrix.setupTestSuite( | ||
() => { | ||
const errors: LogEvent[] = [] | ||
|
||
beforeAll(() => { | ||
prisma = newPrismaClient({ log: [{ emit: 'event', level: 'error' }] }) | ||
prisma.$on('error', (e) => errors.push(e)) | ||
}) | ||
|
||
afterEach(() => { | ||
errors.length = 0 | ||
}) | ||
|
||
test('findUniqueOrThrown when error thrown', async () => { | ||
await expect(() => | ||
prisma.user.findUniqueOrThrow({ | ||
where: { | ||
email, | ||
}, | ||
}), | ||
).rejects.toThrowError(new NotFoundError('No User found')) | ||
|
||
expect(errors).toHaveLength(1) | ||
|
||
const errorEvent = errors[0] | ||
expect(errorEvent.message).toContain( | ||
'An operation failed because it depends on one or more records that were required but not found. Expected a record, found none.', | ||
) | ||
expect(errorEvent.target).toContain('user.findUniqueOrThrow') | ||
}) | ||
|
||
test('findFirstOrThrow when error thrown', async () => { | ||
await expect(() => | ||
prisma.user.findFirstOrThrow({ | ||
where: { | ||
email, | ||
}, | ||
}), | ||
).rejects.toThrowError(new NotFoundError('No User found')) | ||
|
||
expect(errors).toHaveLength(1) | ||
|
||
const errorEvent = errors[0] | ||
expect(errorEvent.message).toContain( | ||
'An operation failed because it depends on one or more records that were required but not found. Expected a record, found none.', | ||
) | ||
expect(errorEvent.target).toContain('user.findFirstOrThrow') | ||
}) | ||
}, | ||
{ | ||
skipDefaultClientInstance: true, | ||
}, | ||
) |