-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ts
More file actions
68 lines (64 loc) · 1.66 KB
/
test.ts
File metadata and controls
68 lines (64 loc) · 1.66 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* @fileoverview Test environment variable getters and detection.
* Provides access to test framework environment variables and utilities.
*/
import { envAsBoolean, envAsString } from './helpers'
import { getNodeEnv } from './node-env'
import { getEnvValue } from './rewire'
/**
* JEST_WORKER_ID environment variable.
* Set when running tests with Jest.
*
* @returns The Jest worker ID string, or empty string if not set
*
* @example
* ```typescript
* import { getJestWorkerId } from '@socketsecurity/lib/env/test'
*
* const workerId = getJestWorkerId()
* // e.g. '1' when running in Jest, or ''
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function getJestWorkerId(): string {
return envAsString(getEnvValue('JEST_WORKER_ID'))
}
/**
* VITEST environment variable.
* Set when running tests with Vitest.
*
* @returns `true` if running in Vitest, `false` otherwise
*
* @example
* ```typescript
* import { getVitest } from '@socketsecurity/lib/env/test'
*
* if (getVitest()) {
* console.log('Running in Vitest')
* }
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function getVitest(): boolean {
return envAsBoolean(getEnvValue('VITEST'))
}
/**
* Check if code is running in a test environment.
* Checks NODE_ENV, VITEST, and JEST_WORKER_ID.
*
* @returns `true` if running in a test environment, `false` otherwise
*
* @example
* ```typescript
* import { isTest } from '@socketsecurity/lib/env/test'
*
* if (isTest()) {
* console.log('Running in test environment')
* }
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function isTest(): boolean {
const nodeEnv = envAsString(getNodeEnv())
return nodeEnv === 'test' || getVitest() || !!getJestWorkerId()
}