Skip to content

Commit 980ac8b

Browse files
committed
test: add comprehensive unit tests for Node.js 18 deprecation warning
- Test warning message content and formatting - Verify warning appears only for Node.js v18.x versions - Test edge cases: browser environment, undefined process, malformed versions - Ensure client creation still works when warning is triggered - Cover regex pattern matching accuracy for version detection
1 parent 8941498 commit 980ac8b

File tree

1 file changed

+252
-0
lines changed

1 file changed

+252
-0
lines changed

test/unit/deprecation-warning.test.ts

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
// Store original values to restore after tests
2+
const originalProcess = global.process
3+
const originalWindow = (global as any).window
4+
const originalConsoleWarn = console.warn
5+
6+
// Make this a module to fix TypeScript error
7+
export {}
8+
9+
describe('Node.js 18 Deprecation Warning', () => {
10+
let mockConsoleWarn: jest.SpyInstance
11+
12+
beforeEach(() => {
13+
// Mock console.warn to capture calls
14+
mockConsoleWarn = jest.spyOn(console, 'warn').mockImplementation(() => {})
15+
// Clear module cache to allow re-imports
16+
jest.resetModules()
17+
})
18+
19+
afterEach(() => {
20+
// Restore original values
21+
global.process = originalProcess
22+
;(global as any).window = originalWindow
23+
mockConsoleWarn.mockRestore()
24+
jest.resetModules()
25+
})
26+
27+
afterAll(() => {
28+
// Ensure console.warn is fully restored
29+
console.warn = originalConsoleWarn
30+
})
31+
32+
describe('shouldShowDeprecationWarning conditions', () => {
33+
test('should show warning for Node.js 18.0.0', () => {
34+
// Setup Node.js environment with version 18.0.0
35+
delete (global as any).window
36+
global.process = {
37+
...originalProcess,
38+
version: 'v18.0.0',
39+
} as NodeJS.Process
40+
41+
// Re-require the module to trigger the deprecation check
42+
require('../../src/index')
43+
44+
expect(mockConsoleWarn).toHaveBeenCalledWith(
45+
expect.stringContaining('⚠️ Node.js 18 is deprecated')
46+
)
47+
expect(mockConsoleWarn).toHaveBeenCalledWith(
48+
expect.stringContaining('Please upgrade to Node.js 20 or later')
49+
)
50+
expect(mockConsoleWarn).toHaveBeenCalledWith(
51+
expect.stringContaining('https://github.com/orgs/supabase/discussions/37217')
52+
)
53+
})
54+
55+
test('should show warning for Node.js 18.19.1', () => {
56+
delete (global as any).window
57+
global.process = {
58+
...originalProcess,
59+
version: 'v18.19.1',
60+
} as NodeJS.Process
61+
62+
require('../../src/index')
63+
64+
expect(mockConsoleWarn).toHaveBeenCalled()
65+
})
66+
67+
test('should show warning for Node.js 18.20.0', () => {
68+
delete (global as any).window
69+
global.process = {
70+
...originalProcess,
71+
version: 'v18.20.0',
72+
} as NodeJS.Process
73+
74+
require('../../src/index')
75+
76+
expect(mockConsoleWarn).toHaveBeenCalled()
77+
})
78+
79+
test('should NOT show warning for Node.js 20.0.0', () => {
80+
delete (global as any).window
81+
global.process = {
82+
...originalProcess,
83+
version: 'v20.0.0',
84+
} as NodeJS.Process
85+
86+
require('../../src/index')
87+
88+
expect(mockConsoleWarn).not.toHaveBeenCalled()
89+
})
90+
91+
test('should NOT show warning for Node.js 22.17.1', () => {
92+
delete (global as any).window
93+
global.process = {
94+
...originalProcess,
95+
version: 'v22.17.1',
96+
} as NodeJS.Process
97+
98+
require('../../src/index')
99+
100+
expect(mockConsoleWarn).not.toHaveBeenCalled()
101+
})
102+
103+
test('should NOT show warning for Node.js 16.20.0', () => {
104+
delete (global as any).window
105+
global.process = {
106+
...originalProcess,
107+
version: 'v16.20.0',
108+
} as NodeJS.Process
109+
110+
require('../../src/index')
111+
112+
expect(mockConsoleWarn).not.toHaveBeenCalled()
113+
})
114+
115+
test('should NOT show warning when window is defined (browser environment)', () => {
116+
;(global as any).window = {}
117+
global.process = {
118+
...originalProcess,
119+
version: 'v18.19.1',
120+
} as NodeJS.Process
121+
122+
require('../../src/index')
123+
124+
expect(mockConsoleWarn).not.toHaveBeenCalled()
125+
})
126+
127+
test('should NOT show warning when process is undefined', () => {
128+
delete (global as any).window
129+
130+
// Mock the shouldShowDeprecationWarning condition directly
131+
const mockWindow = undefined
132+
const mockProcess = undefined
133+
const shouldShow =
134+
typeof mockWindow === 'undefined' &&
135+
typeof mockProcess !== 'undefined' &&
136+
(mockProcess as any)?.version !== undefined &&
137+
/^v18\./.test((mockProcess as any)?.version || '')
138+
139+
expect(shouldShow).toBe(false)
140+
expect(mockConsoleWarn).not.toHaveBeenCalled()
141+
})
142+
143+
test('should NOT show warning when process.version is undefined', () => {
144+
delete (global as any).window
145+
global.process = {
146+
...originalProcess,
147+
version: undefined,
148+
} as any
149+
150+
require('../../src/index')
151+
152+
expect(mockConsoleWarn).not.toHaveBeenCalled()
153+
})
154+
155+
test('should NOT show warning for version string "18" without "v" prefix', () => {
156+
delete (global as any).window
157+
global.process = {
158+
...originalProcess,
159+
version: '18.19.1',
160+
} as any
161+
162+
require('../../src/index')
163+
164+
expect(mockConsoleWarn).not.toHaveBeenCalled()
165+
})
166+
167+
test('should NOT show warning for version containing "18" but not starting with "v18."', () => {
168+
delete (global as any).window
169+
global.process = {
170+
...originalProcess,
171+
version: 'v1.18.0',
172+
} as NodeJS.Process
173+
174+
require('../../src/index')
175+
176+
expect(mockConsoleWarn).not.toHaveBeenCalled()
177+
})
178+
})
179+
180+
describe('createClient functionality with warning', () => {
181+
test('should create client successfully even with deprecation warning', () => {
182+
delete (global as any).window
183+
global.process = {
184+
...originalProcess,
185+
version: 'v18.19.1',
186+
} as NodeJS.Process
187+
188+
const { createClient: freshCreateClient } = require('../../src/index')
189+
190+
const client = freshCreateClient('https://test.supabase.co', 'fake-key')
191+
192+
expect(client).toBeDefined()
193+
expect(mockConsoleWarn).toHaveBeenCalled()
194+
})
195+
})
196+
197+
describe('warning message content', () => {
198+
test('should contain all required information in warning message', () => {
199+
delete (global as any).window
200+
global.process = {
201+
...originalProcess,
202+
version: 'v18.19.1',
203+
} as NodeJS.Process
204+
205+
require('../../src/index')
206+
207+
expect(mockConsoleWarn).toHaveBeenCalledTimes(1)
208+
209+
const warningMessage = mockConsoleWarn.mock.calls[0][0]
210+
expect(warningMessage).toContain('⚠️ Node.js 18 is deprecated')
211+
expect(warningMessage).toContain('will no longer be supported in future versions')
212+
expect(warningMessage).toContain('@supabase/supabase-js')
213+
expect(warningMessage).toContain('Please upgrade to Node.js 20 or later')
214+
expect(warningMessage).toContain('https://github.com/orgs/supabase/discussions/37217')
215+
})
216+
})
217+
218+
describe('edge cases', () => {
219+
test('should handle malformed version strings gracefully', () => {
220+
delete (global as any).window
221+
global.process = {
222+
...originalProcess,
223+
version: 'invalid-version',
224+
} as NodeJS.Process
225+
226+
expect(() => require('../../src/index')).not.toThrow()
227+
expect(mockConsoleWarn).not.toHaveBeenCalled()
228+
})
229+
230+
test('should handle empty version string', () => {
231+
delete (global as any).window
232+
global.process = {
233+
...originalProcess,
234+
version: '',
235+
} as NodeJS.Process
236+
237+
expect(() => require('../../src/index')).not.toThrow()
238+
expect(mockConsoleWarn).not.toHaveBeenCalled()
239+
})
240+
241+
test('should handle null version', () => {
242+
delete (global as any).window
243+
global.process = {
244+
...originalProcess,
245+
version: null,
246+
} as any
247+
248+
expect(() => require('../../src/index')).not.toThrow()
249+
expect(mockConsoleWarn).not.toHaveBeenCalled()
250+
})
251+
})
252+
})

0 commit comments

Comments
 (0)