-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathdetectJestVersion.ts
More file actions
46 lines (38 loc) · 954 Bytes
/
detectJestVersion.ts
File metadata and controls
46 lines (38 loc) · 954 Bytes
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
import type { JSONSchemaForNPMPackageJsonFiles } from '@schemastore/package';
export type JestVersion =
| 14
| 15
| 16
| 17
| 18
| 19
| 20
| 21
| 22
| 23
| 24
| 25
| 26
| 27
| 28
| 29
| number;
let cachedJestVersion: JestVersion | null = null;
export const detectJestVersion = (): JestVersion => {
if (cachedJestVersion) {
return cachedJestVersion;
}
try {
const jestPath = require.resolve('jest/package.json');
const jestPackageJson =
// eslint-disable-next-line @typescript-eslint/no-require-imports
require(jestPath) as JSONSchemaForNPMPackageJsonFiles;
if (jestPackageJson.version) {
const [majorVersion] = jestPackageJson.version.split('.');
return (cachedJestVersion = parseInt(majorVersion, 10));
}
} catch {}
throw new Error(
'Unable to detect Jest version - please ensure jest package is installed, or otherwise set version explicitly',
);
};