Skip to content

Commit

Permalink
fix(client): add console error log for caching on top of error (#18759)
Browse files Browse the repository at this point in the history
  • Loading branch information
millsp committed Apr 17, 2023
1 parent 6285a15 commit 84af70e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 16 deletions.
52 changes: 46 additions & 6 deletions packages/client/src/runtime/core/init/checkPlatformCaching.test.ts
@@ -1,31 +1,71 @@
import { PrismaClientInitializationError } from '@prisma/engine-core'

import { checkPlatformCaching } from './checkPlatformCaching'

const consoleMock = jest.spyOn(global.console, 'error').mockImplementation()

beforeEach(() => {
consoleMock.mockClear()
})

test('generated via postinstall on vercel', () => {
expect(() => checkPlatformCaching({ postinstall: true, ciName: 'Vercel', clientVersion: '0.0.0' }))
.toThrowErrorMatchingInlineSnapshot(`
We have detected that you've built your project on Vercel, which caches dependencies.
This leads to an outdated Prisma Client because Prisma's auto-generation isn't triggered.
To fix this, make sure to run the \`prisma generate\` command during your build process.
Prisma has detected that this project was built on Vercel, which caches dependencies. This leads to an outdated Prisma Client because Prisma's auto-generation isn't triggered. To fix this, make sure to run the \`prisma generate\` command during the build process.
Learn how: https://pris.ly/d/vercel-build
`)

expect(consoleMock.mock.calls[0]).toMatchInlineSnapshot(`
[
Prisma has detected that this project was built on Vercel, which caches dependencies. This leads to an outdated Prisma Client because Prisma's auto-generation isn't triggered. To fix this, make sure to run the \`prisma generate\` command during the build process.
Learn how: https://pris.ly/d/vercel-build,
]
`)
})

test('generated on vercel', () => {
expect(() => checkPlatformCaching({ postinstall: false, ciName: 'Vercel', clientVersion: '0.0.0' })).not.toThrow()

expect(consoleMock.mock.calls[0]).toMatchInlineSnapshot(`undefined`)
})

test('generated via postinstall on netlify', () => {
expect(() => checkPlatformCaching({ postinstall: true, ciName: 'Netlify CI', clientVersion: '0.0.0' }))
.toThrowErrorMatchingInlineSnapshot(`
We have detected that you've built your project on Netlify CI, which caches dependencies.
This leads to an outdated Prisma Client because Prisma's auto-generation isn't triggered.
To fix this, make sure to run the \`prisma generate\` command during your build process.
Prisma has detected that this project was built on Netlify CI, which caches dependencies. This leads to an outdated Prisma Client because Prisma's auto-generation isn't triggered. To fix this, make sure to run the \`prisma generate\` command during the build process.
Learn how: https://pris.ly/d/netlify-build
`)

expect(consoleMock.mock.calls[0]).toMatchInlineSnapshot(`
[
Prisma has detected that this project was built on Netlify CI, which caches dependencies. This leads to an outdated Prisma Client because Prisma's auto-generation isn't triggered. To fix this, make sure to run the \`prisma generate\` command during the build process.
Learn how: https://pris.ly/d/netlify-build,
]
`)
})

test('generated on netlify', () => {
expect(() => checkPlatformCaching({ postinstall: false, ciName: 'Netlify CI', clientVersion: '0.0.0' })).not.toThrow()
expect(consoleMock.mock.calls[0]).toMatchInlineSnapshot(`undefined`)
})

test('error is a PrismaClientInitializationError', () => {
try {
checkPlatformCaching({ postinstall: true, ciName: 'Netlify CI', clientVersion: '0.0.0' })
} catch (e) {
expect(e).toBeInstanceOf(PrismaClientInitializationError)
expect(consoleMock.mock.calls[0]).toMatchInlineSnapshot(`
[
Prisma has detected that this project was built on Netlify CI, which caches dependencies. This leads to an outdated Prisma Client because Prisma's auto-generation isn't triggered. To fix this, make sure to run the \`prisma generate\` command during the build process.
Learn how: https://pris.ly/d/netlify-build,
]
`)
}
})

export {}
15 changes: 8 additions & 7 deletions packages/client/src/runtime/core/init/checkPlatformCaching.ts
Expand Up @@ -32,12 +32,13 @@ export function checkPlatformCaching({ postinstall, ciName, clientVersion }: Con

// and we generated on one a caching CI
if (ciName && ciName in cachingPlatforms) {
throw new PrismaClientInitializationError(
`We have detected that you've built your project on ${ciName}, which caches dependencies.
This leads to an outdated Prisma Client because Prisma's auto-generation isn't triggered.
To fix this, make sure to run the \`prisma generate\` command during your build process.
Learn how: https://pris.ly/d/${cachingPlatforms[ciName]}-build`,
clientVersion,
)
const message = `Prisma has detected that this project was built on ${ciName}, which caches dependencies. This leads to an outdated Prisma Client because Prisma's auto-generation isn't triggered. To fix this, make sure to run the \`prisma generate\` command during the build process.
Learn how: https://pris.ly/d/${cachingPlatforms[ciName]}-build`

console.error(message) // display a nice and visible error message

// also throw an error so that the user can catch it and handle it
throw new PrismaClientInitializationError(message, clientVersion)
}
}
@@ -1,15 +1,28 @@
import { PrismaClient } from '@prisma/client'

const consoleMock = jest.spyOn(global.console, 'error').mockImplementation()

beforeEach(() => {
consoleMock.mockClear()
})

test('vercel env var + auto generate', () => {
try {
const prisma = new PrismaClient()
prisma.$connect()
} catch (e) {
expect(e).toMatchInlineSnapshot(`
[Error: We have detected that you've built your project on Vercel, which caches dependencies.
This leads to an outdated Prisma Client because Prisma's auto-generation isn't triggered.
To fix this, make sure to run the \`prisma generate\` command during your build process.
[Error: Prisma has detected that this project was built on Vercel, which caches dependencies. This leads to an outdated Prisma Client because Prisma's auto-generation isn't triggered. To fix this, make sure to run the \`prisma generate\` command during the build process.
Learn how: https://pris.ly/d/vercel-build]
`)

expect(consoleMock.mock.calls[0]).toMatchInlineSnapshot(`
[
"Prisma has detected that this project was built on Vercel, which caches dependencies. This leads to an outdated Prisma Client because Prisma's auto-generation isn't triggered. To fix this, make sure to run the \`prisma generate\` command during the build process.
Learn how: https://pris.ly/d/vercel-build",
]
`)
}
})

0 comments on commit 84af70e

Please sign in to comment.