/
helpers.ts
271 lines (245 loc) · 8.76 KB
/
helpers.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import { NodeFS } from '@yarnpkg/fslib';
import { exec as childProcessExec } from 'child_process';
import * as promisify from 'util.promisify';
import { sync as rimrafSync } from 'rimraf';
import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'fs';
import { join, resolve } from 'path';
import * as fs from 'fs';
import { lock } from 'proper-lockfile';
import type { Readable } from 'stream';
/**
* types from ts-node under test
*/
import type * as tsNodeTypes from '../index';
import type _createRequire from 'create-require';
import { has, mapValues, once } from 'lodash';
import semver = require('semver');
const createRequire: typeof _createRequire = require('create-require');
export { tsNodeTypes };
//#region Paths
export const ROOT_DIR = resolve(__dirname, '../..');
export const DIST_DIR = resolve(__dirname, '..');
export const TEST_DIR = join(__dirname, '../../tests');
export const PROJECT = join(TEST_DIR, 'tsconfig.json');
export const PROJECT_TRANSPILE_ONLY = join(
TEST_DIR,
'tsconfig-transpile-only.json'
);
export const BIN_PATH = join(TEST_DIR, 'node_modules/.bin/ts-node');
export const BIN_PATH_JS = join(TEST_DIR, 'node_modules/ts-node/dist/bin.js');
export const BIN_SCRIPT_PATH = join(
TEST_DIR,
'node_modules/.bin/ts-node-script'
);
export const BIN_CWD_PATH = join(TEST_DIR, 'node_modules/.bin/ts-node-cwd');
export const BIN_ESM_PATH = join(TEST_DIR, 'node_modules/.bin/ts-node-esm');
//#endregion
//#region command lines
/** Default `ts-node --project` invocation */
export const CMD_TS_NODE_WITH_PROJECT_FLAG = `"${BIN_PATH}" --project "${PROJECT}"`;
/** Default `ts-node --project` invocation with transpile-only */
export const CMD_TS_NODE_WITH_PROJECT_TRANSPILE_ONLY_FLAG = `"${BIN_PATH}" --project "${PROJECT_TRANSPILE_ONLY}"`;
/** Default `ts-node` invocation without `--project` */
export const CMD_TS_NODE_WITHOUT_PROJECT_FLAG = `"${BIN_PATH}"`;
export const EXPERIMENTAL_MODULES_FLAG = semver.gte(process.version, '12.17.0')
? ''
: '--experimental-modules';
export const CMD_ESM_LOADER_WITHOUT_PROJECT = `node ${EXPERIMENTAL_MODULES_FLAG} --loader ts-node/esm`;
//#endregion
// `createRequire` does not exist on older node versions
export const testsDirRequire = createRequire(join(TEST_DIR, 'index.js'));
export const ts = testsDirRequire('typescript');
//#region version checks
export const nodeSupportsEsmHooks = semver.gte(process.version, '12.16.0');
export const nodeSupportsSpawningChildProcess = semver.gte(
process.version,
'12.17.0'
);
export const nodeUsesNewHooksApi = semver.gte(process.version, '16.12.0');
export const nodeSupportsImportAssertions = semver.gte(
process.version,
'17.1.0'
);
/** Supports tsconfig "extends" >= v3.2.0 */
export const tsSupportsTsconfigInheritanceViaNodePackages = semver.gte(
ts.version,
'3.2.0'
);
/** Supports --showConfig: >= v3.2.0 */
export const tsSupportsShowConfig = semver.gte(ts.version, '3.2.0');
//#endregion
export const xfs = new NodeFS(fs);
/** Pass to `test.context()` to get access to the ts-node API under test */
export const contextTsNodeUnderTest = once(async () => {
await installTsNode();
const tsNodeUnderTest: typeof tsNodeTypes = testsDirRequire('ts-node');
return {
tsNodeUnderTest,
};
});
//#region install ts-node tarball
const ts_node_install_lock = process.env.ts_node_install_lock as string;
const lockPath = join(__dirname, ts_node_install_lock);
interface InstallationResult {
error: string | null;
}
/**
* Pack and install ts-node locally, necessary to test package "exports"
* FS locking b/c tests run in separate processes
*/
export async function installTsNode() {
await lockedMemoizedOperation(lockPath, async () => {
const totalTries = process.platform === 'win32' ? 5 : 1;
let tries = 0;
while (true) {
try {
rimrafSync(join(TEST_DIR, 'node_modules'));
await promisify(childProcessExec)(`npm install`, { cwd: TEST_DIR });
const packageLockPath = join(TEST_DIR, 'package-lock.json');
existsSync(packageLockPath) && unlinkSync(packageLockPath);
break;
} catch (e) {
tries++;
if (tries >= totalTries) throw e;
}
}
});
}
/**
* Attempt an operation once across multiple processes, using filesystem locking.
* If it was executed already by another process, and it errored, throw the same error message.
*/
async function lockedMemoizedOperation(
lockPath: string,
operation: () => Promise<void>
) {
const releaseLock = await lock(lockPath, {
realpath: false,
stale: 120e3,
retries: {
retries: 120,
maxTimeout: 1000,
},
});
try {
const operationHappened = existsSync(lockPath);
if (operationHappened) {
const result: InstallationResult = JSON.parse(
readFileSync(lockPath, 'utf8')
);
if (result.error) throw result.error;
} else {
const result: InstallationResult = { error: null };
try {
await operation();
} catch (e) {
result.error = `${e}`;
throw e;
} finally {
writeFileSync(lockPath, JSON.stringify(result));
}
}
} finally {
releaseLock();
}
}
//#endregion
/**
* Get a stream into a string.
* Will resolve early if
*/
export function getStream(stream: Readable, waitForPattern?: string | RegExp) {
let resolve: (value: string) => void;
const promise = new Promise<string>((res) => {
resolve = res;
});
const received: Buffer[] = [];
let combinedBuffer: Buffer = Buffer.concat([]);
let combinedString: string = '';
stream.on('data', (data) => {
received.push(data);
combine();
if (
(typeof waitForPattern === 'string' &&
combinedString.indexOf(waitForPattern) >= 0) ||
(waitForPattern instanceof RegExp && combinedString.match(waitForPattern))
)
resolve(combinedString);
combinedBuffer = Buffer.concat(received);
});
stream.on('end', () => {
resolve(combinedString);
});
return promise;
function combine() {
combinedBuffer = Buffer.concat(received);
combinedString = combinedBuffer.toString('utf8');
}
}
//#region Reset node environment
const defaultRequireExtensions = captureObjectState(require.extensions);
const defaultProcess = captureObjectState(process);
const defaultModule = captureObjectState(require('module'));
const defaultError = captureObjectState(Error);
const defaultGlobal = captureObjectState(global);
/**
* Undo all of ts-node & co's installed hooks, resetting the node environment to default
* so we can run multiple test cases which `.register()` ts-node.
*
* Must also play nice with `nyc`'s environmental mutations.
*/
export function resetNodeEnvironment() {
// We must uninstall so that it resets its internal state; otherwise it won't know it needs to reinstall in the next test.
require('@cspotcode/source-map-support').uninstall();
// Modified by ts-node hooks
resetObject(require.extensions, defaultRequireExtensions);
// ts-node attaches a property when it registers an instance
// source-map-support monkey-patches the emit function
resetObject(process, defaultProcess);
// source-map-support swaps out the prepareStackTrace function
resetObject(Error, defaultError);
// _resolveFilename is modified by tsconfig-paths, future versions of source-map-support, and maybe future versions of ts-node
resetObject(require('module'), defaultModule);
// May be modified by REPL tests, since the REPL sets globals.
// Avoid deleting nyc's coverage data.
resetObject(global, defaultGlobal, ['__coverage__']);
}
function captureObjectState(object: any) {
const descriptors = Object.getOwnPropertyDescriptors(object);
const values = mapValues(descriptors, (_d, key) => {
// Avoid node deprecation warning for accessing _channel
if (object === process && key === '_channel') return descriptors[key].value;
return object[key];
});
return {
descriptors,
values,
};
}
// Redefine all property descriptors and delete any new properties
function resetObject(
object: any,
state: ReturnType<typeof captureObjectState>,
doNotDeleteTheseKeys: string[] = []
) {
const currentDescriptors = Object.getOwnPropertyDescriptors(object);
for (const key of Object.keys(currentDescriptors)) {
if (doNotDeleteTheseKeys.includes(key)) continue;
if (has(state.descriptors, key)) continue;
delete object[key];
}
// Trigger nyc's setter functions
for (const [key, value] of Object.entries(state.values)) {
try {
// Avoid node deprecation warnings for setting process.config or accessing _channel
if (object === process && key === '_channel') continue;
if (object === process && key === 'config' && object[key] === value)
continue;
object[key] = value;
} catch {}
}
// Reset descriptors
Object.defineProperties(object, state.descriptors);
}
//#endregion
export const delay = promisify(setTimeout);