Skip to content

Commit 9e3c5ef

Browse files
committed
backpoirt of #1449
1 parent 54ffab8 commit 9e3c5ef

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

addon-test-support/@ember/test-helpers/setup-context.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,21 @@ export function isTestContext(context: BaseContext): context is TestContext {
9090
);
9191
}
9292

93-
let __test_context__: BaseContext | undefined;
93+
/**
94+
@private
95+
@param {Object} it the global object to test
96+
@returns {Boolean} it exists
97+
*/
98+
function check(it: any) {
99+
// Math is known to exist as a global in every environment.
100+
return it && it.Math === Math && it;
101+
}
102+
103+
const globalObject =
104+
check(typeof globalThis == 'object' && globalThis) ||
105+
check(typeof window === 'object' && window) ||
106+
check(typeof self === 'object' && self) ||
107+
check(typeof global === 'object' && global);
94108

95109
/**
96110
Stores the provided context as the "global testing context".
@@ -101,17 +115,17 @@ let __test_context__: BaseContext | undefined;
101115
@param {Object} context the context to use
102116
*/
103117
export function setContext(context: BaseContext): void {
104-
__test_context__ = context;
118+
globalObject.__test_context__ = context;
105119
}
106120

107121
/**
108-
Retrive the "global testing context" as stored by `setContext`.
122+
Retrieve the "global testing context" as stored by `setContext`.
109123
110124
@public
111125
@returns {Object} the previously stored testing context
112126
*/
113127
export function getContext(): BaseContext | undefined {
114-
return __test_context__;
128+
return globalObject.__test_context__;
115129
}
116130

117131
/**
@@ -122,7 +136,7 @@ export function getContext(): BaseContext | undefined {
122136
@public
123137
*/
124138
export function unsetContext(): void {
125-
__test_context__ = undefined;
139+
globalObject.__test_context__ = undefined;
126140
}
127141

128142
/**

0 commit comments

Comments
 (0)