Fix mock test env check outside of browser

This commit is contained in:
Jamie Kyle 2024-04-16 13:13:02 -07:00 committed by GitHub
parent dbff1ab4d1
commit 3f074a7737
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 47 additions and 22 deletions

View file

@ -12,6 +12,7 @@ export enum Environment {
}
let environment: undefined | Environment;
let isMockTestEnvironment: undefined | boolean;
export function getEnvironment(): Environment {
if (environment === undefined) {
@ -26,12 +27,16 @@ export function getEnvironment(): Environment {
/**
* Sets the current environment. Should be called early in a process's life, and can only
* be called once.
*
* isMockTestEnv is used when running tests that require a non-"test" environment but
* need to mock certain behaviors.
*/
export function setEnvironment(env: Environment): void {
export function setEnvironment(env: Environment, isMockTestEnv: boolean): void {
if (environment !== undefined) {
throw new Error('Environment has already been set');
}
environment = env;
isMockTestEnvironment = isMockTestEnv;
}
export const parseEnvironment = makeEnumParser(
@ -41,3 +46,10 @@ export const parseEnvironment = makeEnumParser(
export const isTestEnvironment = (env: Environment): boolean =>
env === Environment.Test;
export const isTestOrMockEnvironment = (): boolean => {
if (isMockTestEnvironment == null) {
throw new Error('Mock test environment not set');
}
return isTestEnvironment(getEnvironment()) || isMockTestEnvironment;
};