Skip to content

Commit 581475d

Browse files
committed
test_runner: add mock file system support
1 parent 95852d7 commit 581475d

File tree

7 files changed

+2096
-0
lines changed

7 files changed

+2096
-0
lines changed

lib/internal/test_runner/mock/mock.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ const {
4848
validateOneOf,
4949
} = require('internal/validators');
5050
const { MockTimers } = require('internal/test_runner/mock/mock_timers');
51+
const { MockFileSystem } = require('internal/test_runner/mock/mock_file_system');
5152
const { Module } = require('internal/modules/cjs/loader');
5253
const { _load, _nodeModulePaths, _resolveFilename, isBuiltin } = Module;
5354
function kDefaultFunction() {}
5455
const enableModuleMocking = getOptionValue('--experimental-test-module-mocks');
56+
const enableFsMocking = getOptionValue('--experimental-test-fs-mocks');
5557
const kSupportedFormats = [
5658
'builtin',
5759
'commonjs-typescript',
@@ -405,6 +407,7 @@ const { restore: restoreProperty } = MockPropertyContext.prototype;
405407
class MockTracker {
406408
#mocks = [];
407409
#timers;
410+
#fs;
408411

409412
/**
410413
* Returns the mock timers of this MockTracker instance.
@@ -415,6 +418,21 @@ class MockTracker {
415418
return this.#timers;
416419
}
417420

421+
/**
422+
* Returns the mock file system of this MockTracker instance.
423+
* @returns {MockFileSystem} The mock file system instance.
424+
*/
425+
get fs() {
426+
if (!enableFsMocking) {
427+
throw new ERR_INVALID_STATE(
428+
'File system mocking is not enabled. ' +
429+
'Use --experimental-test-fs-mocks to enable it.',
430+
);
431+
}
432+
this.#fs ??= new MockFileSystem();
433+
return this.#fs;
434+
}
435+
418436
/**
419437
* Creates a mock function tracker.
420438
* @param {Function} [original] - The original function to be tracked.
@@ -731,6 +749,7 @@ class MockTracker {
731749
reset() {
732750
this.restoreAll();
733751
this.#timers?.reset();
752+
this.#fs?.reset();
734753
this.#mocks = [];
735754
}
736755

0 commit comments

Comments
 (0)