UNPKG

5.03 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = readConfigFileAndSetRootDir;
7
8function path() {
9 const data = _interopRequireWildcard(require('path'));
10
11 path = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function fs() {
19 const data = _interopRequireWildcard(require('graceful-fs'));
20
21 fs = function () {
22 return data;
23 };
24
25 return data;
26}
27
28function _jestUtil() {
29 const data = require('jest-util');
30
31 _jestUtil = function () {
32 return data;
33 };
34
35 return data;
36}
37
38var _constants = require('./constants');
39
40var _jsonlint = _interopRequireDefault(require('./vendor/jsonlint'));
41
42function _interopRequireDefault(obj) {
43 return obj && obj.__esModule ? obj : {default: obj};
44}
45
46function _getRequireWildcardCache(nodeInterop) {
47 if (typeof WeakMap !== 'function') return null;
48 var cacheBabelInterop = new WeakMap();
49 var cacheNodeInterop = new WeakMap();
50 return (_getRequireWildcardCache = function (nodeInterop) {
51 return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
52 })(nodeInterop);
53}
54
55function _interopRequireWildcard(obj, nodeInterop) {
56 if (!nodeInterop && obj && obj.__esModule) {
57 return obj;
58 }
59 if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
60 return {default: obj};
61 }
62 var cache = _getRequireWildcardCache(nodeInterop);
63 if (cache && cache.has(obj)) {
64 return cache.get(obj);
65 }
66 var newObj = {};
67 var hasPropertyDescriptor =
68 Object.defineProperty && Object.getOwnPropertyDescriptor;
69 for (var key in obj) {
70 if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
71 var desc = hasPropertyDescriptor
72 ? Object.getOwnPropertyDescriptor(obj, key)
73 : null;
74 if (desc && (desc.get || desc.set)) {
75 Object.defineProperty(newObj, key, desc);
76 } else {
77 newObj[key] = obj[key];
78 }
79 }
80 }
81 newObj.default = obj;
82 if (cache) {
83 cache.set(obj, newObj);
84 }
85 return newObj;
86}
87
88/**
89 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
90 *
91 * This source code is licensed under the MIT license found in the
92 * LICENSE file in the root directory of this source tree.
93 */
94// @ts-expect-error: vendored
95// Read the configuration and set its `rootDir`
96// 1. If it's a `package.json` file, we look into its "jest" property
97// 2. If it's a `jest.config.ts` file, we use `ts-node` to transpile & require it
98// 3. For any other file, we just require it. If we receive an 'ERR_REQUIRE_ESM'
99// from node, perform a dynamic import instead.
100async function readConfigFileAndSetRootDir(configPath) {
101 const isTS = configPath.endsWith(_constants.JEST_CONFIG_EXT_TS);
102 const isJSON = configPath.endsWith(_constants.JEST_CONFIG_EXT_JSON);
103 let configObject;
104
105 try {
106 if (isTS) {
107 configObject = await loadTSConfigFile(configPath);
108 } else {
109 configObject = await (0, _jestUtil().requireOrImportModule)(configPath);
110 }
111 } catch (error) {
112 if (isJSON) {
113 throw new Error(
114 `Jest: Failed to parse config file ${configPath}\n` +
115 ` ${_jsonlint.default.errors(fs().readFileSync(configPath, 'utf8'))}`
116 );
117 } else if (isTS) {
118 throw new Error(
119 `Jest: Failed to parse the TypeScript config file ${configPath}\n` +
120 ` ${error}`
121 );
122 } else {
123 throw error;
124 }
125 }
126
127 if (configPath.endsWith(_constants.PACKAGE_JSON)) {
128 // Event if there's no "jest" property in package.json we will still use
129 // an empty object.
130 configObject = configObject.jest || {};
131 }
132
133 if (typeof configObject === 'function') {
134 configObject = await configObject();
135 }
136
137 if (configObject.rootDir) {
138 // We don't touch it if it has an absolute path specified
139 if (!path().isAbsolute(configObject.rootDir)) {
140 // otherwise, we'll resolve it relative to the file's __dirname
141 configObject.rootDir = path().resolve(
142 path().dirname(configPath),
143 configObject.rootDir
144 );
145 }
146 } else {
147 // If rootDir is not there, we'll set it to this file's __dirname
148 configObject.rootDir = path().dirname(configPath);
149 }
150
151 return configObject;
152} // Load the TypeScript configuration
153
154const loadTSConfigFile = async configPath => {
155 let registerer; // Register TypeScript compiler instance
156
157 try {
158 registerer = require('ts-node').register({
159 compilerOptions: {
160 module: 'CommonJS'
161 }
162 });
163 } catch (e) {
164 if (e.code === 'MODULE_NOT_FOUND') {
165 throw new Error(
166 `Jest: 'ts-node' is required for the TypeScript configuration files. Make sure it is installed\nError: ${e.message}`
167 );
168 }
169
170 throw e;
171 }
172
173 registerer.enabled(true);
174 let configObject = (0, _jestUtil().interopRequireDefault)(
175 require(configPath)
176 ).default; // In case the config is a function which imports more Typescript code
177
178 if (typeof configObject === 'function') {
179 configObject = await configObject();
180 }
181
182 registerer.enabled(false);
183 return configObject;
184};