From 278893a95ae33df9c3d0a46bbd52882c21411bf2 Mon Sep 17 00:00:00 2001 From: Erick Wendel Date: Mon, 1 May 2023 13:53:18 -0300 Subject: [PATCH] test_runner: move fakerTimers to mock module Signed-off-by: Erick Wendel --- .../test_runner/{ => mock}/fake_timers.js | 0 lib/internal/test_runner/{ => mock}/mock.js | 6 ++++++ lib/internal/test_runner/test.js | 9 ++------- lib/test.js | 19 +------------------ ....js => test-runner-mocking-fake-timers.js} | 2 +- 5 files changed, 10 insertions(+), 26 deletions(-) rename lib/internal/test_runner/{ => mock}/fake_timers.js (100%) rename lib/internal/test_runner/{ => mock}/mock.js (97%) rename test/parallel/{test-runner-fake-timers.js => test-runner-mocking-fake-timers.js} (93%) diff --git a/lib/internal/test_runner/fake_timers.js b/lib/internal/test_runner/mock/fake_timers.js similarity index 100% rename from lib/internal/test_runner/fake_timers.js rename to lib/internal/test_runner/mock/fake_timers.js diff --git a/lib/internal/test_runner/mock.js b/lib/internal/test_runner/mock/mock.js similarity index 97% rename from lib/internal/test_runner/mock.js rename to lib/internal/test_runner/mock/mock.js index aa3014a7bc40c6..8b0fd4985528fd 100644 --- a/lib/internal/test_runner/mock.js +++ b/lib/internal/test_runner/mock/mock.js @@ -26,6 +26,7 @@ const { validateInteger, validateObject, } = require('internal/validators'); +const { FakeTimers } = require('internal/test_runner/mock/fake_timers') function kDefaultFunction() {} @@ -35,6 +36,7 @@ class MockFunctionContext { #implementation; #restore; #times; + #fakeTimers; constructor(implementation, restore, times) { this.#calls = []; @@ -48,6 +50,10 @@ class MockFunctionContext { return ArrayPrototypeSlice(this.#calls, 0); } + get fakeTimers() { + this.#fakeTimers ??= new FakeTimers(); + return this.#fakeTimers; + } callCount() { return this.#calls.length; } diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 52c77b94d92279..fad5103224a832 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -30,8 +30,8 @@ const { }, AbortError, } = require('internal/errors'); -const { MockTracker } = require('internal/test_runner/mock'); -const { FakeTimers } = require('internal/test_runner/fake_timers'); +const { MockTracker } = require('internal/test_runner/mock/mock'); +const { FakeTimers } = require('internal/test_runner/mock/fake_timers'); const { TestsStream } = require('internal/test_runner/tests_stream'); const { createDeferredCallback, @@ -109,11 +109,6 @@ class TestContext { return this.#test.mock; } - get fakeTimers() { - this.#test.fakeTimers ??= new FakeTimers(); - return this.#test.fakeTimers; - } - runOnly(value) { this.#test.runOnlySubtests = !!value; } diff --git a/lib/test.js b/lib/test.js index faa743241da6f7..d096cae23d5357 100644 --- a/lib/test.js +++ b/lib/test.js @@ -23,7 +23,7 @@ ObjectDefineProperty(module.exports, 'mock', { enumerable: true, get() { if (lazyMock === undefined) { - const { MockTracker } = require('internal/test_runner/mock'); + const { MockTracker } = require('internal/test_runner/mock/mock'); lazyMock = new MockTracker(); } @@ -31,20 +31,3 @@ ObjectDefineProperty(module.exports, 'mock', { return lazyMock; }, }); - -let lazyFakeTimers; - -ObjectDefineProperty(module.exports, 'fakeTimers', { - __proto__: null, - configurable: true, - enumerable: true, - get() { - if (lazyFakeTimers === undefined) { - const { FakeTimers } = require('internal/test_runner/fake_timers'); - - lazyFakeTimers = new FakeTimers(); - } - - return lazyFakeTimers; - }, -}); diff --git a/test/parallel/test-runner-fake-timers.js b/test/parallel/test-runner-mocking-fake-timers.js similarity index 93% rename from test/parallel/test-runner-fake-timers.js rename to test/parallel/test-runner-mocking-fake-timers.js index f15f83f98197d6..6784c11e82bf91 100644 --- a/test/parallel/test-runner-fake-timers.js +++ b/test/parallel/test-runner-mocking-fake-timers.js @@ -3,7 +3,7 @@ const common = require('../common'); process.env.NODE_TEST_KNOWN_GLOBALS = 0; const assert = require('node:assert'); -const { fakeTimers, it, mock, afterEach, describe } = require('node:test'); +const { it, mock: { fakeTimers }, afterEach, describe } = require('node:test'); describe('Faketimers Test Suite', () => { describe('setTimeout Suite', () => {