Created
August 24, 2020 10:57
-
-
Save ryzy/fe75d493f86ad13d89bf1160079c4557 to your computer and use it in GitHub Desktop.
isTextContext
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('#isTestContext', () => { | |
beforeEach(() => { | |
delete process.env.JEST_WORKER_ID; | |
delete (window as any).__karma__; | |
delete (window as any).Cypress; | |
}); | |
it('#isTestContext should be true for Jest', () => { | |
expect(isTestContext()).toBe(false); | |
process.env.JEST_WORKER_ID = '1'; | |
expect(isTestContext()).toBe(true); | |
}); | |
it('#isTestContext should be true for Karma', () => { | |
expect(isTestContext()).toBe(false); | |
(window as any).__karma__ = {}; | |
expect(isTestContext()).toBe(true); | |
}); | |
it('#isTestContext should be true for Cypress', () => { | |
expect(isTestContext()).toBe(false); | |
(window as any).Cypress = {}; | |
expect(isTestContext()).toBe(true); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns true if code is executed in test (i.e. Karma/Jest) context. | |
*/ | |
export function isTestContext(): boolean { | |
const isKarma: boolean = !!(window as any).__karma__; | |
const isJest: boolean = 'undefined' !== typeof process && !!process.env.JEST_WORKER_ID; | |
const isCypress: boolean = !!(window as any).Cypress; | |
return isKarma || isJest || isCypress; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment