Skip to content

Commit 9a11489

Browse files
author
EthanHan
committed
test: add version specifier normalization tests
Addresses CodeRabbit review suggestions: - Add tests for caret (^), tilde (~), workspace:*, and >= specifiers - Normalize globalVersion before comparison to handle specifiers - Prevent false mismatches from version specifier differences Related to #1911
1 parent 802b4e2 commit 9a11489

2 files changed

Lines changed: 36 additions & 15 deletions

File tree

packages/cli/src/__tests__/commands/versionMismatch.test.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -195,21 +195,37 @@ describe('Generate with version mismatch', () => {
195195
})
196196
})
197197

198-
describe('extractExactVersion', () => {
199-
// Note: extractExactVersion is internal to version-mismatch-checker.ts
200-
// We test it indirectly through getLocalPrismaVersion
201-
202-
it('should handle caret version specifier', async () => {
203-
// This test verifies that version specifiers like ^5.0.0 are properly handled
204-
// The actual extraction logic is tested in version-mismatch-checker.test.ts
205-
expect(true).toBe(true) // Placeholder - actual testing in module tests
198+
describe('version specifier normalization', () => {
199+
const optionsFor = (clientVersion: string | null): VersionMismatchOptions => ({
200+
isGlobalInstall: () => 'npm',
201+
getClientVersion: () => Promise.resolve(clientVersion),
202+
getLocalPrismaVersion: () => Promise.resolve(null),
206203
})
207204

208-
it('should handle tilde version specifier', async () => {
209-
expect(true).toBe(true) // Placeholder - actual testing in module tests
205+
it('treats caret specifier as matching the same global version', async () => {
206+
const result = await checkVersionMismatch('5.0.0', optionsFor('^5.0.0'))
207+
expect(result).toBeNull()
210208
})
211209

212-
it('should handle exact version specifier', async () => {
213-
expect(true).toBe(true) // Placeholder - actual testing in module tests
210+
it('treats tilde specifier as matching the same global version', async () => {
211+
const result = await checkVersionMismatch('5.0.0', optionsFor('~5.0.0'))
212+
expect(result).toBeNull()
213+
})
214+
215+
it('still reports mismatch for different exact versions', async () => {
216+
const result = await checkVersionMismatch('5.0.0', optionsFor('4.0.0'))
217+
expect(result?.localPackageType).toBe('@prisma/client')
218+
expect(result?.localVersion).toBe('4.0.0')
219+
})
220+
221+
it('handles workspace:* specifier gracefully', async () => {
222+
// workspace:* should be normalized to null and not cause false warnings
223+
const result = await checkVersionMismatch('5.0.0', optionsFor('workspace:*'))
224+
expect(result).toBeNull()
225+
})
226+
227+
it('handles >= specifier', async () => {
228+
const result = await checkVersionMismatch('5.0.0', optionsFor('>=5.0.0'))
229+
expect(result).toBeNull()
214230
})
215231
})

packages/cli/src/utils/version-mismatch-checker.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,16 @@ export async function checkVersionMismatch(
7979
}
8080

8181
try {
82-
const localClientVersion = await getClientVersion(cwd)
82+
// Normalize global version (extract exact version from specifier)
83+
const normalizedGlobalVersion = extractExactVersion(globalVersion) ?? globalVersion
84+
85+
const localClientVersionRaw = await getClientVersion(cwd)
86+
const localClientVersion = extractExactVersion(localClientVersionRaw) ?? localClientVersionRaw
87+
8388
const localPrismaVersion = await getLocalPrismaVersion(cwd)
8489

8590
// Check @prisma/client version mismatch
86-
if (localClientVersion && localClientVersion !== globalVersion) {
91+
if (localClientVersion && localClientVersion !== normalizedGlobalVersion) {
8792
return {
8893
hasMismatch: true,
8994
globalVersion,
@@ -93,7 +98,7 @@ export async function checkVersionMismatch(
9398
}
9499

95100
// Check local prisma version mismatch
96-
if (localPrismaVersion && localPrismaVersion !== globalVersion) {
101+
if (localPrismaVersion && localPrismaVersion !== normalizedGlobalVersion) {
97102
return {
98103
hasMismatch: true,
99104
globalVersion,

0 commit comments

Comments
 (0)