Skip to content

Commit 7b0035b

Browse files
committed
Fix issues related to spies and stubbing environments in utils.spec.ts
1 parent 9a8f521 commit 7b0035b

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed
Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,72 @@
1-
import { vi } from 'vitest'
1+
import { noop } from '@internal/listenerMiddleware/utils'
22
import { AClockworkOrange } from './fixtures/book'
33

44
describe('Entity utils', () => {
55
describe(`selectIdValue()`, () => {
6-
const OLD_ENV = process.env
6+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(noop)
77

88
beforeEach(() => {
99
vi.resetModules() // this is important - it clears the cache
10-
process.env = { ...OLD_ENV, NODE_ENV: 'development' }
10+
vi.stubEnv('NODE_ENV', 'development')
1111
})
1212

1313
afterEach(() => {
14-
process.env = OLD_ENV
14+
consoleWarnSpy.mockClear()
15+
vi.unstubAllEnvs()
1516
vi.resetAllMocks()
1617
})
1718

19+
afterAll(() => {
20+
consoleWarnSpy.mockRestore()
21+
})
22+
1823
it('should not warn when key does exist', async () => {
1924
const { selectIdValue } = await import('../utils')
20-
const spy = vi.spyOn(console, 'warn')
2125

2226
selectIdValue(AClockworkOrange, (book: any) => book.id)
23-
expect(spy).not.toHaveBeenCalled()
27+
expect(consoleWarnSpy).not.toHaveBeenCalled()
2428
})
2529

2630
it('should warn when key does not exist in dev mode', async () => {
2731
const { selectIdValue } = await import('../utils')
28-
const spy = vi.spyOn(console, 'warn')
32+
33+
expect(process.env.NODE_ENV).toBe('development')
2934

3035
selectIdValue(AClockworkOrange, (book: any) => book.foo)
3136

32-
expect(spy).toHaveBeenCalled()
37+
expect(consoleWarnSpy).toHaveBeenCalledOnce()
3338
})
3439

3540
it('should warn when key is undefined in dev mode', async () => {
3641
const { selectIdValue } = await import('../utils')
37-
const spy = vi.spyOn(console, 'warn')
42+
43+
expect(process.env.NODE_ENV).toBe('development')
3844

3945
const undefinedAClockworkOrange = { ...AClockworkOrange, id: undefined }
4046
selectIdValue(undefinedAClockworkOrange, (book: any) => book.id)
4147

42-
expect(spy).toHaveBeenCalled()
48+
expect(consoleWarnSpy).toHaveBeenCalledOnce()
4349
})
4450

4551
it('should not warn when key does not exist in prod mode', async () => {
46-
process.env.NODE_ENV = 'production'
52+
vi.stubEnv('NODE_ENV', 'production')
53+
4754
const { selectIdValue } = await import('../utils')
48-
const spy = vi.spyOn(console, 'warn')
4955

5056
selectIdValue(AClockworkOrange, (book: any) => book.foo)
5157

52-
expect(spy).not.toHaveBeenCalled()
58+
expect(consoleWarnSpy).not.toHaveBeenCalled()
5359
})
5460

5561
it('should not warn when key is undefined in prod mode', async () => {
56-
process.env.NODE_ENV = 'production'
62+
vi.stubEnv('NODE_ENV', 'production')
63+
5764
const { selectIdValue } = await import('../utils')
58-
const spy = vi.spyOn(console, 'warn')
5965

6066
const undefinedAClockworkOrange = { ...AClockworkOrange, id: undefined }
6167
selectIdValue(undefinedAClockworkOrange, (book: any) => book.id)
6268

63-
expect(spy).not.toHaveBeenCalled()
69+
expect(consoleWarnSpy).not.toHaveBeenCalled()
6470
})
6571
})
6672
})

0 commit comments

Comments
 (0)