@@ -12,7 +12,7 @@ import type { Readable } from 'stream';
12
12
*/
13
13
import type * as tsNodeTypes from '../index' ;
14
14
import type _createRequire from 'create-require' ;
15
- import { has , once } from 'lodash' ;
15
+ import { has , mapValues , once } from 'lodash' ;
16
16
import semver = require( 'semver' ) ;
17
17
const createRequire : typeof _createRequire = require ( 'create-require' ) ;
18
18
export { tsNodeTypes } ;
@@ -218,25 +218,29 @@ export function resetNodeEnvironment() {
218
218
resetObject ( require ( 'module' ) , defaultModule ) ;
219
219
220
220
// May be modified by REPL tests, since the REPL sets globals.
221
- resetObject ( global , defaultGlobal ) ;
221
+ // Avoid deleting nyc's coverage data.
222
+ resetObject ( global , defaultGlobal , [ '__coverage__' ] ) ;
222
223
}
223
224
224
225
function captureObjectState ( object : any ) {
226
+ const descriptors = Object . getOwnPropertyDescriptors ( object ) ;
227
+ const values = mapValues ( descriptors , ( _d , key ) => object [ key ] ) ;
225
228
return {
226
- descriptors : Object . getOwnPropertyDescriptors ( object ) ,
227
- values : { ... object } ,
229
+ descriptors,
230
+ values,
228
231
} ;
229
232
}
230
233
// Redefine all property descriptors and delete any new properties
231
234
function resetObject (
232
235
object : any ,
233
- state : ReturnType < typeof captureObjectState >
236
+ state : ReturnType < typeof captureObjectState > ,
237
+ doNotDeleteTheseKeys : string [ ] = [ ]
234
238
) {
235
239
const currentDescriptors = Object . getOwnPropertyDescriptors ( object ) ;
236
240
for ( const key of Object . keys ( currentDescriptors ) ) {
237
- if ( ! has ( state . descriptors , key ) ) {
238
- delete object [ key ] ;
239
- }
241
+ if ( doNotDeleteTheseKeys . includes ( key ) ) continue ;
242
+ if ( has ( state . descriptors , key ) ) continue ;
243
+ delete object [ key ] ;
240
244
}
241
245
// Trigger nyc's setter functions
242
246
for ( const [ key , value ] of Object . entries ( state . values ) ) {
0 commit comments