MCPcopy
hub / github.com/TypeStrong/ts-node / resetNodeEnvironment

Function resetNodeEnvironment

src/test/helpers.ts:243–278  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

241 * Must also play nice with `nyc`'s environmental mutations.
242 */
243export function resetNodeEnvironment() {
244 const sms =
245 require('@cspotcode/source-map-support') as typeof import('@cspotcode/source-map-support');
246 // We must uninstall so that it resets its internal state; otherwise it won't know it needs to reinstall in the next test.
247 sms.uninstall();
248 // Must remove handlers to avoid a memory leak
249 sms.resetRetrieveHandlers();
250
251 // Modified by ts-node hooks
252 resetObject(
253 require.extensions,
254 defaultRequireExtensions,
255 undefined,
256 undefined,
257 undefined,
258 true
259 );
260
261 // ts-node attaches a property when it registers an instance
262 // source-map-support monkey-patches the emit function
263 // Avoid node deprecation warnings for setting process.config or accessing _channel
264 resetObject(process, defaultProcess, undefined, ['_channel'], ['config']);
265
266 // source-map-support swaps out the prepareStackTrace function
267 resetObject(Error, defaultError);
268
269 // _resolveFilename et.al. are modified by ts-node, tsconfig-paths, source-map-support, yarn, maybe other things?
270 resetObject(require('module'), defaultModule, undefined, ['wrap', 'wrapper']);
271
272 // May be modified by REPL tests, since the REPL sets globals.
273 // Avoid deleting nyc's coverage data.
274 resetObject(global, defaultGlobal, ['__coverage__']);
275
276 // Reset our ESM hooks
277 process.__test_setloader__?.(undefined);
278}
279
280function captureObjectState(object: any, avoidGetters: string[] = []) {
281 const descriptors = Object.getOwnPropertyDescriptors(object);

Callers 5

esm-loader.spec.tsFile · 0.90
register.spec.tsFile · 0.90
declareProjectFunction · 0.90
repl.spec.tsFile · 0.90

Calls 2

resetObjectFunction · 0.85
__test_setloader__Method · 0.80

Tested by 1

declareProjectFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…