File size: 3,107 Bytes
d4b85c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { vi, beforeAll, afterEach, afterAll, it, expect } from 'vitest'
import { hasConfigurableGlobal } from './hasConfigurableGlobal'

beforeAll(() => {
  vi.spyOn(console, 'error').mockImplementation(() => {})
})

afterEach(() => {
  vi.resetAllMocks()
})

afterAll(() => {
  vi.restoreAllMocks()
})

it('returns true if the global property exists and is configurable', () => {
  Object.defineProperty(global, '_existsAndConfigurable', {
    value: 'something',
    configurable: true,
  })

  expect(hasConfigurableGlobal('_existsAndConfigurable')).toBe(true)
})

it('returns false if the global property does not exist', () => {
  expect(hasConfigurableGlobal('_non-existing')).toBe(false)
})

it('returns false for existing global with undefined as a value', () => {
  Object.defineProperty(global, '_existsAndUndefined', {
    value: undefined,
    configurable: true,
  })
  expect(hasConfigurableGlobal('_existsAndUndefined')).toBe(false)
})

it('returns false for existing global with null as a value', () => {
  Object.defineProperty(global, '_existsAndNull', {
    value: null,
    configurable: true,
  })
  expect(hasConfigurableGlobal('_existsAndNull')).toBe(false)
})

it('returns false for existing global with a getter that returns undefined', () => {
  Object.defineProperty(global, '_existsGetterUndefined', {
    get: () => undefined,
    configurable: true,
  })
  expect(hasConfigurableGlobal('_existsGetterUndefined')).toBe(false)
})

it('returns false and prints an error for implicitly non-configurable global property', () => {
  Object.defineProperty(global, '_implicitlyNonConfigurable', {
    value: 'something',
  })

  expect(hasConfigurableGlobal('_implicitlyNonConfigurable')).toBe(false)
  expect(console.error).toHaveBeenCalledWith(
    '[MSW] Failed to apply interceptor: the global `_implicitlyNonConfigurable` property is non-configurable. This is likely an issue with your environment. If you are using a framework, please open an issue about this in their repository.'
  )
})

it('returns false and prints an error for explicitly non-configurable global property', () => {
  Object.defineProperty(global, '_explicitlyNonConfigurable', {
    value: 'something',
    configurable: false,
  })

  expect(hasConfigurableGlobal('_explicitlyNonConfigurable')).toBe(false)
  expect(console.error).toHaveBeenCalledWith(
    '[MSW] Failed to apply interceptor: the global `_explicitlyNonConfigurable` property is non-configurable. This is likely an issue with your environment. If you are using a framework, please open an issue about this in their repository.'
  )
})

it('returns false and prints an error for global property that only has a getter', () => {
  Object.defineProperty(global, '_onlyGetter', { get: () => 'something' })

  expect(hasConfigurableGlobal('_onlyGetter')).toBe(false)
  expect(console.error).toHaveBeenCalledWith(
    '[MSW] Failed to apply interceptor: the global `_onlyGetter` property is non-configurable. This is likely an issue with your environment. If you are using a framework, please open an issue about this in their repository.'
  )
})