Skip to content

Commit a407fba

Browse files
committed
Fix issues related to spies and stubbing environments in utils.spec.ts
1 parent f933b24 commit a407fba

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,65 +1,71 @@
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()
16+
})
17+
18+
afterAll(() => {
19+
consoleWarnSpy.mockRestore()
1520
})
1621

1722
it('should not warn when key does exist', async () => {
1823
const { selectIdValue } = await import('../utils')
19-
const spy = vi.spyOn(console, 'warn')
2024

2125
selectIdValue(AClockworkOrange, (book: any) => book.id)
22-
expect(spy).not.toHaveBeenCalled()
26+
expect(consoleWarnSpy).not.toHaveBeenCalled()
2327
})
2428

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

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

31-
expect(spy).toHaveBeenCalled()
36+
expect(consoleWarnSpy).toHaveBeenCalledOnce()
3237
})
3338

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

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

41-
expect(spy).toHaveBeenCalled()
47+
expect(consoleWarnSpy).toHaveBeenCalledOnce()
4248
})
4349

4450
it('should not warn when key does not exist in prod mode', async () => {
45-
process.env.NODE_ENV = 'production'
51+
vi.stubEnv('NODE_ENV', 'production')
52+
4653
const { selectIdValue } = await import('../utils')
47-
const spy = vi.spyOn(console, 'warn')
4854

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

51-
expect(spy).not.toHaveBeenCalled()
57+
expect(consoleWarnSpy).not.toHaveBeenCalled()
5258
})
5359

5460
it('should not warn when key is undefined in prod mode', async () => {
55-
process.env.NODE_ENV = 'production'
61+
vi.stubEnv('NODE_ENV', 'production')
62+
5663
const { selectIdValue } = await import('../utils')
57-
const spy = vi.spyOn(console, 'warn')
5864

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

62-
expect(spy).not.toHaveBeenCalled()
68+
expect(consoleWarnSpy).not.toHaveBeenCalled()
6369
})
6470
})
6571
})

0 commit comments

Comments
 (0)