Skip to content

Commit ef53f5c

Browse files
authored
test(client): fix invalid error instances when using $connect() or datasource override (prisma#24706)
* feat(client): remove 10th instance of Prisma Client warning * test(client): fix invalid error instances when using connect or datasource override * test(client): minor test fixes
1 parent dfe1c22 commit ef53f5c

File tree

3 files changed

+105
-80
lines changed

3 files changed

+105
-80
lines changed

packages/client/tests/functional/datasource-overrides/tests.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { PrismaClient } from './node_modules/@prisma/client'
99
declare let newPrismaClient: NewPrismaClient<typeof PrismaClient>
1010

1111
testMatrix.setupTestSuite(
12-
({ provider, clientRuntime, driverAdapter }, suiteMeta, clientMeta) => {
12+
({ provider, driverAdapter }, _suiteMeta, clientMeta) => {
1313
let dbURL: string
1414
beforeAll(() => {
1515
dbURL = process.env[`DATABASE_URI_${provider}`]!
@@ -21,8 +21,7 @@ testMatrix.setupTestSuite(
2121
})
2222

2323
describeIf(driverAdapter === undefined)('default case: no Driver Adapter', () => {
24-
// TODO: Fails with Expected PrismaClientInitError, Received Error
25-
skipTestIf(clientRuntime === 'wasm')('verify that connect fails without override', async () => {
24+
test('verify that connect fails without override', async () => {
2625
// this a smoke to verify that our beforeAll setup worked correctly and right
2726
// url won't be picked up by Prisma client anymore.
2827
// If this test fails, subsequent tests can't be trusted regardless of whether or not they pass or not.

packages/client/tests/functional/invalid-env-value/tests.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import stripAnsi from 'strip-ansi'
33
import { NewPrismaClient } from '../_utils/types'
44
import testMatrix from './_matrix'
55
// @ts-ignore
6-
import type { Prisma as PrismaNamespace, PrismaClient } from './node_modules/@prisma/client'
6+
import type { PrismaClient } from './node_modules/@prisma/client'
77

88
declare const newPrismaClient: NewPrismaClient<typeof PrismaClient>
9-
declare let Prisma: typeof PrismaNamespace
109

1110
testMatrix.setupTestSuite(
12-
({ clientRuntime, provider }, suiteMeta, clientMeta) => {
11+
({ provider }, _suiteMeta, clientMeta) => {
1312
const OLD_ENV = process.env
1413

1514
beforeEach(() => {
@@ -21,8 +20,7 @@ testMatrix.setupTestSuite(
2120
process.env = OLD_ENV
2221
})
2322

24-
// TODO: fails with Expected constructor: PrismaClientInitializationError Received constructor: Error
25-
skipTestIf(clientRuntime === 'wasm')('PrismaClientInitializationError for invalid env', async () => {
23+
test('PrismaClientInitializationError for invalid env', async () => {
2624
// This test often fails on macOS CI with thrown: "Exceeded timeout of
2725
// 60000 ms for a hook. Retrying might help, let's find out
2826
const isMacCI = Boolean(process.env.CI) && ['darwin'].includes(process.platform)
@@ -33,23 +31,19 @@ testMatrix.setupTestSuite(
3331
const prisma = newPrismaClient()
3432
const promise = prisma.$connect()
3533

36-
if (clientMeta.dataProxy && clientMeta.runtime === 'edge') {
37-
// TODO Prisma 6: should be a PrismaClientInitializationError, but the message is correct
38-
// await expect(promise).rejects.toBeInstanceOf(Prisma.InvalidDatasourceError)
39-
await expect(promise).rejects.toThrowErrorMatchingInlineSnapshot(
40-
`"Error validating datasource \`db\`: the URL must start with the protocol \`prisma://\`"`,
41-
)
42-
} else if (clientMeta.dataProxy && clientMeta.runtime === 'node') {
43-
// TODO Prisma 6: should be a PrismaClientInitializationError, but the message is correct
44-
// await expect(promise).rejects.toBeInstanceOf(Prisma.InvalidDatasourceError)
45-
await expect(promise).rejects.toThrowErrorMatchingInlineSnapshot(
46-
`"Error validating datasource \`db\`: the URL must start with the protocol \`prisma://\`"`,
47-
)
48-
} else if (!clientMeta.dataProxy) {
34+
if (!clientMeta.dataProxy) {
4935
await promise.catch((e) => {
5036
const message = stripAnsi(e.message)
51-
expect(e).toBeInstanceOf(Prisma.PrismaClientInitializationError)
52-
expect(message).toContain('error: Error validating datasource `db`: the URL must start with the protocol')
37+
expect(e.name).toEqual('PrismaClientInitializationError')
38+
expect(message).toContain('Error validating datasource `db`: the URL must start with the protocol')
39+
})
40+
} else if (['edge', 'node', 'wasm'].includes(clientMeta.runtime)) {
41+
await promise.catch((e) => {
42+
const message = stripAnsi(e.message)
43+
expect(e.name).toEqual('InvalidDatasourceError')
44+
expect(message).toContain(
45+
'Error validating datasource `db`: the URL must start with the protocol `prisma://`',
46+
)
5347
})
5448
} else {
5549
throw new Error('Unhandled case')

packages/client/tests/functional/missing-env/tests.ts

Lines changed: 89 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,10 @@ declare const newPrismaClient: NewPrismaClient<typeof PrismaClient>
99
declare let Prisma: typeof PrismaNamespace
1010

1111
testMatrix.setupTestSuite(
12-
({ clientRuntime, driverAdapter }, suiteMeta, clientMeta) => {
13-
// TODO: Fails with Expected PrismaClientInitError, Received Error
14-
skipTestIf(clientRuntime === 'wasm')('PrismaClientInitializationError for missing env', async () => {
15-
const prisma = newPrismaClient()
16-
17-
try {
18-
await prisma.$connect()
19-
} catch (e) {
20-
const message = stripAnsi(e.message as string)
21-
expect(e).toBeInstanceOf(Prisma.PrismaClientInitializationError)
22-
expect(message).toContain('error: Environment variable not found: DATABASE_URI.')
23-
}
24-
})
25-
// TODO: Fails with Expected PrismaClientInitError, Received Error
26-
skipTestIf(driverAdapter !== undefined)(
27-
'PrismaClientInitializationError for missing env and empty override',
28-
async () => {
29-
const prisma = newPrismaClient({
30-
datasources: {
31-
db: {},
32-
},
33-
})
12+
({ driverAdapter }, _suiteMeta, clientMeta) => {
13+
describeIf(driverAdapter === undefined)('default case: no Driver Adapter', () => {
14+
test('PrismaClientInitializationError for missing env', async () => {
15+
const prisma = newPrismaClient()
3416

3517
try {
3618
await prisma.$connect()
@@ -39,62 +21,112 @@ testMatrix.setupTestSuite(
3921
expect(e).toBeInstanceOf(Prisma.PrismaClientInitializationError)
4022
expect(message).toContain('error: Environment variable not found: DATABASE_URI.')
4123
}
42-
},
43-
)
4424

45-
testIf(clientMeta.dataProxy && clientMeta.runtime === 'edge')(
46-
'PrismaClientInitializationError for missing env on edge',
47-
async () => {
48-
const prisma = newPrismaClient()
25+
expect.hasAssertions()
26+
})
27+
28+
test('PrismaClientInitializationError for missing env and empty override', async () => {
29+
const prisma = newPrismaClient({
30+
datasources: {
31+
db: {},
32+
},
33+
})
4934

5035
try {
5136
await prisma.$connect()
5237
} catch (e) {
5338
const message = stripAnsi(e.message as string)
5439
expect(e).toBeInstanceOf(Prisma.PrismaClientInitializationError)
55-
expect(message).toMatchInlineSnapshot(`"error: Environment variable not found: DATABASE_URI."`)
40+
expect(message).toContain('error: Environment variable not found: DATABASE_URI.')
5641
}
57-
},
58-
)
5942

60-
testIf(clientMeta.dataProxy && clientMeta.runtime === 'edge')(
61-
'PrismaClientInitializationError for missing env on edge on cloudflare',
62-
async () => {
63-
globalThis.navigator = { userAgent: 'Cloudflare-Workers' }
43+
expect.hasAssertions()
44+
})
6445

65-
const prisma = newPrismaClient()
46+
testIf(clientMeta.dataProxy && clientMeta.runtime === 'edge')(
47+
'PrismaClientInitializationError for missing env on edge',
48+
async () => {
49+
const prisma = newPrismaClient()
6650

67-
try {
68-
await prisma.$connect()
69-
} catch (e) {
70-
const message = stripAnsi(e.message as string)
71-
expect(e).toBeInstanceOf(Prisma.PrismaClientInitializationError)
72-
expect(message).toMatchInlineSnapshot(`
73-
"error: Environment variable not found: DATABASE_URI.
51+
try {
52+
await prisma.$connect()
53+
} catch (e) {
54+
const message = stripAnsi(e.message as string)
55+
expect(e).toBeInstanceOf(Prisma.PrismaClientInitializationError)
56+
expect(message).toMatchInlineSnapshot(`"error: Environment variable not found: DATABASE_URI."`)
57+
}
7458

75-
In Cloudflare module Workers, environment variables are available only in the Worker's \`env\` parameter of \`fetch\`.
76-
To solve this, provide the connection string directly: https://pris.ly/d/cloudflare-datasource-url"
77-
`)
78-
}
59+
expect.hasAssertions()
60+
},
61+
)
62+
63+
testIf(clientMeta.dataProxy && clientMeta.runtime === 'edge')(
64+
'PrismaClientInitializationError for missing env on edge on cloudflare',
65+
async () => {
66+
globalThis.navigator = { userAgent: 'Cloudflare-Workers' }
67+
68+
const prisma = newPrismaClient()
69+
70+
try {
71+
await prisma.$connect()
72+
} catch (e) {
73+
const message = stripAnsi(e.message as string)
74+
expect(e).toBeInstanceOf(Prisma.PrismaClientInitializationError)
75+
expect(message).toMatchInlineSnapshot(`
76+
"error: Environment variable not found: DATABASE_URI.
7977
80-
delete globalThis.navigator
81-
},
82-
)
78+
In Cloudflare module Workers, environment variables are available only in the Worker's \`env\` parameter of \`fetch\`.
79+
To solve this, provide the connection string directly: https://pris.ly/d/cloudflare-datasource-url"
80+
`)
81+
}
8382

84-
testIf(clientMeta.dataProxy && clientMeta.runtime === 'node')(
85-
'PrismaClientInitializationError for missing env with --no-engine on node',
86-
async () => {
83+
expect.hasAssertions()
84+
delete globalThis.navigator
85+
},
86+
)
87+
88+
testIf(clientMeta.dataProxy && clientMeta.runtime === 'node')(
89+
'PrismaClientInitializationError for missing env with --no-engine on node',
90+
async () => {
91+
const prisma = newPrismaClient()
92+
93+
try {
94+
await prisma.$connect()
95+
} catch (e) {
96+
const message = stripAnsi(e.message as string)
97+
expect(e).toBeInstanceOf(Prisma.PrismaClientInitializationError)
98+
expect(message).toMatchInlineSnapshot(`"error: Environment variable not found: DATABASE_URI."`)
99+
}
100+
101+
expect.hasAssertions()
102+
},
103+
)
104+
})
105+
106+
describeIf(driverAdapter !== undefined)('with Driver Adapters', () => {
107+
test('Initialisation works even when missing env var referenced in the schema', async () => {
87108
const prisma = newPrismaClient()
109+
await prisma.$connect()
110+
})
88111

112+
test('PrismaClientInitializationError for missing env and empty override', () => {
89113
try {
90-
await prisma.$connect()
114+
newPrismaClient({
115+
datasources: {
116+
db: {},
117+
},
118+
})
91119
} catch (e) {
92120
const message = stripAnsi(e.message as string)
93121
expect(e).toBeInstanceOf(Prisma.PrismaClientInitializationError)
94-
expect(message).toMatchInlineSnapshot(`"error: Environment variable not found: DATABASE_URI."`)
122+
expect(message).toMatchInlineSnapshot(
123+
`"Custom datasource configuration is not compatible with Prisma Driver Adapters. Please define the database connection string directly in the Driver Adapter configuration."`,
124+
)
95125
}
96-
},
97-
)
126+
127+
expect.hasAssertions()
128+
})
129+
})
98130
},
99131
{
100132
skipDb: true,

0 commit comments

Comments
 (0)