Fix mock test env check outside of browser
This commit is contained in:
parent
dbff1ab4d1
commit
3f074a7737
11 changed files with 47 additions and 22 deletions
|
@ -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;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue