@@ -51,7 +51,8 @@ const {
5151const { MockTimers } = require ( 'internal/test_runner/mock/mock_timers' ) ;
5252const { Module } = require ( 'internal/modules/cjs/loader' ) ;
5353const { _load, _nodeModulePaths, _resolveFilename, isBuiltin } = Module ;
54- const { dirname, join } = require ( 'path' ) ;
54+ const path = require ( 'path' ) ;
55+ const { dirname, join } = path ;
5556
5657// Lazy-load VirtualFileSystem to avoid loading VFS code if fs mocking is not used
5758const lazyVirtualFileSystem = getLazy (
@@ -413,11 +414,7 @@ const { restore: restoreProperty } = MockPropertyContext.prototype;
413414 * Context for mocking the file system using VFS.
414415 */
415416class MockFSContext {
416- /**
417- * The underlying VirtualFileSystem instance.
418- * @type {VirtualFileSystem }
419- */
420- vfs ;
417+ #vfs;
421418
422419 /**
423420 * The mount prefix for the mock file system.
@@ -426,10 +423,18 @@ class MockFSContext {
426423 prefix ;
427424
428425 constructor ( vfs , prefix ) {
429- this . vfs = vfs ;
426+ this . # vfs = vfs ;
430427 this . prefix = prefix ;
431428 }
432429
430+ /**
431+ * The underlying VirtualFileSystem instance.
432+ * @type {VirtualFileSystem }
433+ */
434+ get vfs ( ) {
435+ return this . #vfs;
436+ }
437+
433438 /**
434439 * Adds a file to the mock file system.
435440 * @param {string } filePath - The path of the file (relative to prefix).
@@ -438,10 +443,11 @@ class MockFSContext {
438443 addFile ( filePath , content ) {
439444 const fullPath = join ( this . prefix , filePath ) ;
440445 const parentDir = dirname ( fullPath ) ;
441- if ( parentDir !== '/' ) {
442- this . vfs . mkdirSync ( parentDir , { __proto__ : null , recursive : true } ) ;
446+ const { root } = path . parse ( parentDir ) ;
447+ if ( parentDir !== root ) {
448+ this . #vfs. mkdirSync ( parentDir , { __proto__ : null , recursive : true } ) ;
443449 }
444- this . vfs . writeFileSync ( fullPath , content ) ;
450+ this . # vfs. writeFileSync ( fullPath , content ) ;
445451 }
446452
447453 /**
@@ -450,7 +456,7 @@ class MockFSContext {
450456 */
451457 addDirectory ( dirPath ) {
452458 const fullPath = join ( this . prefix , dirPath ) ;
453- this . vfs . mkdirSync ( fullPath , { __proto__ : null , recursive : true } ) ;
459+ this . # vfs. mkdirSync ( fullPath , { __proto__ : null , recursive : true } ) ;
454460 }
455461
456462 /**
@@ -460,14 +466,14 @@ class MockFSContext {
460466 */
461467 existsSync ( path ) {
462468 const fullPath = join ( this . prefix , path ) ;
463- return this . vfs . existsSync ( fullPath ) ;
469+ return this . # vfs. existsSync ( fullPath ) ;
464470 }
465471
466472 /**
467473 * Restores the file system to its original state.
468474 */
469475 restore ( ) {
470- this . vfs . unmount ( ) ;
476+ this . # vfs. unmount ( ) ;
471477 }
472478}
473479
@@ -819,7 +825,8 @@ class MockTracker {
819825 const filePath = paths [ i ] ;
820826 const content = files [ filePath ] ;
821827 const parentDir = dirname ( filePath ) ;
822- if ( parentDir !== '/' ) {
828+ const { root } = path . parse ( parentDir ) ;
829+ if ( parentDir !== root ) {
823830 vfs . mkdirSync ( parentDir , { __proto__ : null , recursive : true } ) ;
824831 }
825832 vfs . writeFileSync ( filePath , content ) ;
@@ -850,12 +857,20 @@ class MockTracker {
850857
851858 /**
852859 * Restore all mocks created by this MockTracker instance.
860+ * Collects errors and throws AggregateError after all mocks are restored.
853861 */
854862 restoreAll ( ) {
863+ const errors = [ ] ;
855864 for ( let i = 0 ; i < this . #mocks. length ; i ++ ) {
856865 const { ctx, restore } = this . #mocks[ i ] ;
857-
858- FunctionPrototypeCall ( restore , ctx ) ;
866+ try {
867+ FunctionPrototypeCall ( restore , ctx ) ;
868+ } catch ( err ) {
869+ ArrayPrototypePush ( errors , err ) ;
870+ }
871+ }
872+ if ( errors . length > 0 ) {
873+ throw new AggregateError ( errors , 'Failed to restore some mocks' ) ;
859874 }
860875 }
861876
0 commit comments