Skip to content

Commit 18960cc

Browse files
void-mAlexNullVoxPopuli
authored andcommitted
this change makes it so strict package managers installing duplicate versions of test-helpers across different addons use a shared global state instead of module state
1 parent 741a18a commit 18960cc

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,21 @@ export function isTestContext(context: BaseContext): context is TestContext {
9898
);
9999
}
100100

101-
let __test_context__: BaseContext | undefined;
101+
/**
102+
@private
103+
@param {Object} it the global object to test
104+
@returns {Boolean} it exists
105+
*/
106+
function check(it: any) {
107+
// Math is known to exist as a global in every environment.
108+
return it && it.Math === Math && it;
109+
}
110+
111+
const globalObject =
112+
check(typeof globalThis == 'object' && globalThis) ||
113+
check(typeof window === 'object' && window) ||
114+
check(typeof self === 'object' && self) ||
115+
check(typeof global === 'object' && global);
102116

103117
/**
104118
Stores the provided context as the "global testing context".
@@ -109,7 +123,7 @@ let __test_context__: BaseContext | undefined;
109123
@param {Object} context the context to use
110124
*/
111125
export function setContext(context: BaseContext): void {
112-
__test_context__ = context;
126+
globalObject.__test_context__ = context;
113127
}
114128

115129
/**
@@ -119,7 +133,7 @@ export function setContext(context: BaseContext): void {
119133
@returns {Object} the previously stored testing context
120134
*/
121135
export function getContext(): BaseContext | undefined {
122-
return __test_context__;
136+
return globalObject.__test_context__;
123137
}
124138

125139
/**
@@ -130,7 +144,7 @@ export function getContext(): BaseContext | undefined {
130144
@public
131145
*/
132146
export function unsetContext(): void {
133-
__test_context__ = undefined;
147+
globalObject.__test_context__ = undefined;
134148
}
135149

136150
/**

0 commit comments

Comments
 (0)