-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
readConfigFileAndSetRootDir.ts
83 lines (73 loc) · 2.66 KB
/
readConfigFileAndSetRootDir.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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import * as path from 'path';
import {pathToFileURL} from 'url';
import * as fs from 'graceful-fs';
import type {Config} from '@jest/types';
// @ts-ignore: vendored
import jsonlint from './vendor/jsonlint';
import {JEST_CONFIG_EXT_JSON, PACKAGE_JSON} from './constants';
// Read the configuration and set its `rootDir`
// 1. If it's a `package.json` file, we look into its "jest" property
// 2. For any other file, we just require it. If we receive an 'ERR_REQUIRE_ESM'
// from node, perform a dynamic import instead.
export default async function readConfigFileAndSetRootDir(
configPath: Config.Path,
): Promise<Config.InitialOptions> {
const isJSON = configPath.endsWith(JEST_CONFIG_EXT_JSON);
let configObject;
try {
configObject = require(configPath);
} catch (error) {
if (error.code === 'ERR_REQUIRE_ESM') {
try {
const configUrl = pathToFileURL(configPath);
// node `import()` supports URL, but TypeScript doesn't know that
const importedConfig = await import(configUrl.href);
if (!importedConfig.default) {
throw new Error(
`Jest: Failed to load mjs config file ${configPath} - did you use a default export?`,
);
}
configObject = importedConfig.default;
} catch (innerError) {
if (innerError.message === 'Not supported') {
throw new Error(
`Jest: Your version of Node does not support dynamic import - please enable it or use a .cjs file extension for file ${configPath}`,
);
}
throw innerError;
}
} else if (isJSON) {
throw new Error(
`Jest: Failed to parse config file ${configPath}\n` +
` ${jsonlint.errors(fs.readFileSync(configPath, 'utf8'))}`,
);
} else {
throw error;
}
}
if (configPath.endsWith(PACKAGE_JSON)) {
// Event if there's no "jest" property in package.json we will still use
// an empty object.
configObject = configObject.jest || {};
}
if (configObject.rootDir) {
// We don't touch it if it has an absolute path specified
if (!path.isAbsolute(configObject.rootDir)) {
// otherwise, we'll resolve it relative to the file's __dirname
configObject.rootDir = path.resolve(
path.dirname(configPath),
configObject.rootDir,
);
}
} else {
// If rootDir is not there, we'll set it to this file's __dirname
configObject.rootDir = path.dirname(configPath);
}
return configObject;
}