/
happy-dom.test.ts
89 lines (67 loc) · 2.45 KB
/
happy-dom.test.ts
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
/**
* @vitest-environment happy-dom
*/
/* eslint-disable vars-on-top */
import { expect, it } from 'vitest'
declare global {
// eslint-disable-next-line no-var
var __property: unknown
}
it('defined on self/window are defined on global', () => {
expect(self).toBeDefined()
expect(window).toBeDefined()
expect(self.__property).not.toBeDefined()
expect(window.__property).not.toBeDefined()
expect(globalThis.__property).not.toBeDefined()
globalThis.__property = 'defined_value'
expect(__property).toBe('defined_value')
expect(self.__property).toBe('defined_value')
expect(window.__property).toBe('defined_value')
expect(globalThis.__property).toBe('defined_value')
self.__property = 'test_value'
expect(__property).toBe('test_value')
expect(self.__property).toBe('test_value')
expect(window.__property).toBe('test_value')
expect(globalThis.__property).toBe('test_value')
window.__property = 'new_value'
expect(__property).toBe('new_value')
expect(self.__property).toBe('new_value')
expect(window.__property).toBe('new_value')
expect(globalThis.__property).toBe('new_value')
globalThis.__property = 'global_value'
expect(__property).toBe('global_value')
expect(self.__property).toBe('global_value')
expect(window.__property).toBe('global_value')
expect(globalThis.__property).toBe('global_value')
const obj = {}
self.__property = obj
expect(self.__property).toBe(obj)
expect(window.__property).toBe(obj)
expect(globalThis.__property).toBe(obj)
})
it('usage with defineProperty', () => {
Object.defineProperty(self, '__property', {
get: () => 'self_property',
configurable: true,
})
expect(__property).toBe('self_property')
expect(self.__property).toBe('self_property')
expect(globalThis.__property).toBe('self_property')
expect(window.__property).toBe('self_property')
Object.defineProperty(window, '__property', {
get: () => 'window_property',
configurable: true,
})
expect(__property).toBe('window_property')
expect(self.__property).toBe('window_property')
expect(globalThis.__property).toBe('window_property')
expect(window.__property).toBe('window_property')
Object.defineProperty(globalThis, '__property', {
get: () => 'global_property',
configurable: true,
})
expect(__property).toBe('global_property')
expect(self.__property).toBe('global_property')
expect(globalThis.__property).toBe('global_property')
expect(window.__property).toBe('global_property')
})