-
Notifications
You must be signed in to change notification settings - Fork 39
/
dev.ts
41 lines (35 loc) · 1.32 KB
/
dev.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
const isJest = typeof (globalThis as any).jest !== 'undefined';
const isMocha =
typeof (globalThis as any).beforeEach !== 'undefined' &&
`${(globalThis as any).beforeEach}`.replace(/\s/g, '') ===
'function(name,fn){suites[0].beforeEach(name,fn);}';
const isVitest = typeof (globalThis as any).__vitest_index__ !== 'undefined';
const isUnitTest = isJest || isVitest || isMocha;
let isDev = false;
// wrapping in try-catch because process might be undefined
try {
isDev = process.env.NODE_ENV !== 'production' && !isUnitTest;
} catch {}
/**
* Logs message one time only in dev environments.
*
* @example
* const logWarningInDev = createWarningLogger();
* logWarningInDev("please don't use this")
*/
const createWarningLogger = !isDev
? () => () => {}
: () => {
let logged = false;
return (message: string) => {
if (!logged) {
console.warn(message);
logged = true;
}
};
};
export { isUnitTest, isDev, createWarningLogger };