/
globalRef.spec.js
112 lines (109 loc) · 3.13 KB
/
globalRef.spec.js
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
/* eslint-disable no-undef */
const test = require('ava')
const { runScenario } = require('./util')
const {
createScenarioFromScaffold,
runAndTestScenario,
} = require('lavamoat-core/test/util')
test('globalRef - has only the expected global circular refs', async (t) => {
const scenario = createScenarioFromScaffold({
name: 'globalRef - has only the expected global circular refs',
defineOne: () => {
const circularKeys = Reflect.ownKeys(globalThis).filter((key) => {
const value = globalThis[key]
return value === globalThis
})
module.exports = circularKeys.sort()
},
expectedResult: ['window', 'self', 'global', 'globalThis'].sort(),
})
await runAndTestScenario(t, scenario, runScenario)
})
test('globalRef - globalRef - check default containment', async (t) => {
const scenario = createScenarioFromScaffold({
name: 'globalRef - check default containment',
defineOne: () => {
const testResults = {}
try {
testResults.objCheckThis = this.Object === Object
} catch (_) {}
try {
testResults.objCheckSelf = self.Object === Object
} catch (_) {}
try {
testResults.objCheckGlobal = global.Object === Object
} catch (_) {}
try {
testResults.thisIsExports = exports === this
} catch (_) {}
module.exports = testResults
},
expectedResult: {
objCheckThis: false,
objCheckSelf: true,
objCheckGlobal: true,
thisIsExports: true,
},
})
await runAndTestScenario(t, scenario, runScenario)
})
test('globalRef - ensure endowments are accessible on globals', async (t) => {
const scenario = createScenarioFromScaffold({
name: 'globalRef - ensure endowments are accessible on globals',
defineOne: () => {
const testResults = {}
testResults.contextHasPostMessage = typeof postMessage !== 'undefined'
testResults.selfHasPostMessage = !!self.postMessage
try {
testResults.checkThis = this.postMessage === postMessage
} catch (err) {
checkThis = err.message
}
try {
testResults.checkSelf = self.postMessage === postMessage
} catch (err) {
checkSelf = err.message
}
try {
testResults.checkWindow = window.postMessage === postMessage
} catch (err) {
checkWindow = err.message
}
try {
testResults.checkGlobal = global.postMessage === postMessage
} catch (err) {
checkGlobal = err.message
}
module.exports = testResults
},
config: {
resources: {
$root$: {
packages: {
one: true,
},
},
one: {
globals: {
postMessage: true,
},
},
},
},
expectedResult: {
// "this" is module.exports
checkThis: false,
checkSelf: true,
checkWindow: true,
checkGlobal: true,
contextHasPostMessage: true,
selfHasPostMessage: true,
},
context: {
postMessage: () => {
throw new Error('this should never be called')
},
},
})
await runAndTestScenario(t, scenario, runScenario)
})