()
| 241 | * Must also play nice with `nyc`'s environmental mutations. |
| 242 | */ |
| 243 | export 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 | |
| 280 | function captureObjectState(object: any, avoidGetters: string[] = []) { |
| 281 | const descriptors = Object.getOwnPropertyDescriptors(object); |
searching dependent graphs…