1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 | exports.default = readConfigFileAndSetRootDir;
|
7 |
|
8 | function path() {
|
9 | const data = _interopRequireWildcard(require('path'));
|
10 |
|
11 | path = function () {
|
12 | return data;
|
13 | };
|
14 |
|
15 | return data;
|
16 | }
|
17 |
|
18 | function fs() {
|
19 | const data = _interopRequireWildcard(require('graceful-fs'));
|
20 |
|
21 | fs = function () {
|
22 | return data;
|
23 | };
|
24 |
|
25 | return data;
|
26 | }
|
27 |
|
28 | function _jestUtil() {
|
29 | const data = require('jest-util');
|
30 |
|
31 | _jestUtil = function () {
|
32 | return data;
|
33 | };
|
34 |
|
35 | return data;
|
36 | }
|
37 |
|
38 | var _constants = require('./constants');
|
39 |
|
40 | var _jsonlint = _interopRequireDefault(require('./vendor/jsonlint'));
|
41 |
|
42 | function _interopRequireDefault(obj) {
|
43 | return obj && obj.__esModule ? obj : {default: obj};
|
44 | }
|
45 |
|
46 | function _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 |
|
55 | function _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 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | async 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 |
|
129 |
|
130 | configObject = configObject.jest || {};
|
131 | }
|
132 |
|
133 | if (typeof configObject === 'function') {
|
134 | configObject = await configObject();
|
135 | }
|
136 |
|
137 | if (configObject.rootDir) {
|
138 |
|
139 | if (!path().isAbsolute(configObject.rootDir)) {
|
140 |
|
141 | configObject.rootDir = path().resolve(
|
142 | path().dirname(configPath),
|
143 | configObject.rootDir
|
144 | );
|
145 | }
|
146 | } else {
|
147 |
|
148 | configObject.rootDir = path().dirname(configPath);
|
149 | }
|
150 |
|
151 | return configObject;
|
152 | }
|
153 |
|
154 | const loadTSConfigFile = async configPath => {
|
155 | let registerer;
|
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;
|
177 |
|
178 | if (typeof configObject === 'function') {
|
179 | configObject = await configObject();
|
180 | }
|
181 |
|
182 | registerer.enabled(false);
|
183 | return configObject;
|
184 | };
|