File size: 6,931 Bytes
cd6720a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { it, beforeEach, afterEach, expect, vi } from 'vitest'
import { patchesRegistry } from './patchesRegistry'

declare global {
  var foo: { original: boolean }
}

const realGlobalPrototype = Object.getPrototypeOf(global)

beforeEach(() => {
  Object.defineProperty(global, 'foo', {
    value: { original: true },
    writable: true,
    enumerable: true,
    configurable: true,
  })
})

afterEach(() => {
  Object.setPrototypeOf(global, realGlobalPrototype)
  patchesRegistry.restoreAllPatches()
})

it('replaces the global', () => {
  patchesRegistry.applyPatch(global, 'foo', () => ({ original: false }))
  expect(global.foo).toEqual({ original: false })
})

it('exposes the real value to the replacement callback', () => {
  patchesRegistry.applyPatch(global, 'foo', (realValue) => {
    expect(realValue).toEqual({ original: true })
    return { original: false }
  })
  expect(global.foo).toEqual({ original: false })
})

it('replaces the global set on the prototype', () => {
  function FakeGlobalScope() {}
  FakeGlobalScope.prototype.foo = { prototype: true }
  Object.setPrototypeOf(global, FakeGlobalScope.prototype)
  Reflect.deleteProperty(global, 'foo')

  expect(global.foo).toEqual({ prototype: true })
  expect(FakeGlobalScope.prototype.foo).toEqual({ prototype: true })

  patchesRegistry.applyPatch(global, 'foo', () => ({ original: false }))

  expect(global.foo).toEqual({ original: false })
  expect(FakeGlobalScope.prototype.foo, 'Preserves prototype value').toEqual({
    prototype: true,
  })
})

it('replaces the global after it was restored', () => {
  const restoreGlobal = patchesRegistry.applyPatch(global, 'foo', () => ({
    original: false,
  }))
  expect(global.foo).toEqual({ original: false })

  restoreGlobal()
  expect(global.foo).toEqual({ original: true })

  patchesRegistry.applyPatch(global, 'foo', () => ({ original: false }))
  expect(global.foo).toEqual({ original: false })
})

it('replaces a property on a custom owner', () => {
  const owner = { bar: { original: true } }
  const restoreGlobal = patchesRegistry.applyPatch(owner, 'bar', () => ({
    original: false,
  }))

  expect(owner.bar).toEqual({ original: false })
  expect(global.foo).toEqual({ original: true })

  restoreGlobal()
  expect(owner.bar).toEqual({ original: true })
})

it('tracks replacements per owner independently', () => {
  const ownerA = { shared: 'a-original' }
  const ownerB = { shared: 'b-original' }

  const restoreA = patchesRegistry.applyPatch(ownerA, 'shared', () => 'a-next')
  const restoreB = patchesRegistry.applyPatch(ownerB, 'shared', () => 'b-next')

  expect(ownerA.shared).toBe('a-next')
  expect(ownerB.shared).toBe('b-next')

  restoreA()
  expect(ownerA.shared).toBe('a-original')
  expect(ownerB.shared).toBe('b-next')

  restoreB()
  expect(ownerB.shared).toBe('b-original')
})

it('warns on replacing a non-existing global', () => {
  vi.spyOn(console, 'warn').mockImplementation(() => {})

  patchesRegistry.applyPatch(
    global,
    // @ts-expect-error Intentionally invalid value.
    'NON-EXISTING',
    () => ({ original: false })
  )
  expect(console.warn).toHaveBeenCalledExactlyOnceWith(
    'Failed to replace a global value at "NON-EXISTING": not a global value.'
  )
})

it('throws if replacing an already replaced global', () => {
  patchesRegistry.applyPatch(global, 'foo', () => ({ original: false }))
  expect(global.foo).toEqual({ original: false })

  expect(() =>
    patchesRegistry.applyPatch(global, 'foo', () => ({ original: false }))
  ).toThrow('Failed to replace a global value at "foo": already replaced.')
})

it('does nothing if restoring an already restored global', () => {
  const restoreGlobal = patchesRegistry.applyPatch(global, 'foo', () => ({
    original: false,
  }))

  expect(global.foo).toEqual({ original: false })

  restoreGlobal()
  expect(global.foo).toEqual({ original: true })

  restoreGlobal()
  expect(global.foo).toEqual({ original: true })
})

it('restores the global', () => {
  const restoreGlobal = patchesRegistry.applyPatch(global, 'foo', () => ({
    original: false,
  }))
  expect(global.foo).toEqual({ original: false })

  restoreGlobal()
  expect(global.foo).toEqual({ original: true })
})

it('restores the global set on the prototype', () => {
  function FakeGlobalScope() {}
  FakeGlobalScope.prototype.foo = { prototype: true }
  Object.setPrototypeOf(global, FakeGlobalScope.prototype)
  Reflect.deleteProperty(global, 'foo')

  expect(global.foo).toEqual({ prototype: true })

  const restoreGlobal = patchesRegistry.applyPatch(global, 'foo', () => ({
    original: false,
  }))

  expect(global.foo).toEqual({ original: false })

  restoreGlobal()
  expect(global.foo).toEqual({ prototype: true })
})

it('restores global to the original property descriptor', () => {
  const descriptor: PropertyDescriptor = {
    value: { original: true },
    enumerable: false,
    configurable: true,
    writable: false,
  }
  Object.defineProperty(global, 'foo', descriptor)
  expect(Object.getOwnPropertyDescriptor(global, 'foo')).toEqual(descriptor)

  const restoreGlobal = patchesRegistry.applyPatch(global, 'foo', () => ({
    original: false,
  }))

  expect(global.foo).toEqual({ original: false })
  expect(Object.getOwnPropertyDescriptor(global, 'foo')).toEqual({
    value: { original: false },
    enumerable: true,
    configurable: true,
    writable: false,
  })

  restoreGlobal()

  expect(global.foo).toEqual({ original: true })
  expect(Object.getOwnPropertyDescriptor(global, 'foo')).toEqual(descriptor)
})

it('replaces a non-configurable writable property', () => {
  const owner = {} as { foo: { original: boolean } }
  const descriptor: PropertyDescriptor = {
    value: { original: true },
    enumerable: true,
    writable: true,
    configurable: false,
  }
  Object.defineProperty(owner, 'foo', descriptor)

  const restoreGlobal = patchesRegistry.applyPatch(owner, 'foo', () => ({
    original: false,
  }))

  expect(owner.foo).toEqual({ original: false })
  expect(
    Object.getOwnPropertyDescriptor(owner, 'foo'),
    'Preserves the original descriptor'
  ).toEqual({
    value: { original: false },
    enumerable: true,
    configurable: false,
    writable: true,
  })

  restoreGlobal()

  expect(owner.foo).toEqual({ original: true })
  expect(
    Object.getOwnPropertyDescriptor(owner, 'foo'),
    'Restores the original descriptor'
  ).toEqual(descriptor)
})

it('throws when replacing a non-configurable non-writable property', () => {
  let owner = {} as { foo: { original: boolean } }
  const descriptor: PropertyDescriptor = {
    value: { original: true },
    enumerable: true,
    writable: false,
    configurable: false,
  }
  Object.defineProperty(owner, 'foo', descriptor)

  expect(() =>
    patchesRegistry.applyPatch(owner, 'foo', () => ({
      original: false,
    }))
  ).toThrow('Failed to patch a non-configurable non-writable property "foo"')
})