diff --git a/lib/mock/mock-agent.js b/lib/mock/mock-agent.js index f3c94a4ae76..fd34d576d12 100644 --- a/lib/mock/mock-agent.js +++ b/lib/mock/mock-agent.js @@ -24,7 +24,7 @@ const { } = require('./mock-symbols') const MockClient = require('./mock-client') const MockPool = require('./mock-pool') -const { matchValue, buildMockOptions } = require('./mock-utils') +const { matchValue, buildAndValidateMockOptions } = require('./mock-utils') const { InvalidArgumentError, UndiciError } = require('../core/errors') const Dispatcher = require('../dispatcher/dispatcher') const PendingInterceptorsFormatter = require('./pending-interceptors-formatter') @@ -34,9 +34,11 @@ class MockAgent extends Dispatcher { constructor (opts) { super(opts) + const mockOptions = buildAndValidateMockOptions(opts) + this[kNetConnect] = true this[kIsMockActive] = true - this[kMockAgentIsCallHistoryEnabled] = Boolean(opts?.enableCallHistory) + this[kMockAgentIsCallHistoryEnabled] = mockOptions?.enableCallHistory ?? false // Instantiate Agent and encapsulate if (opts?.agent && typeof opts.agent.dispatch !== 'function') { @@ -46,7 +48,7 @@ class MockAgent extends Dispatcher { this[kAgent] = agent this[kClients] = agent[kClients] - this[kOptions] = buildMockOptions(opts) + this[kOptions] = mockOptions if (this[kMockAgentIsCallHistoryEnabled]) { this[kMockAgentRegisterCallHistory]() diff --git a/lib/mock/mock-utils.js b/lib/mock/mock-utils.js index df9c8626c0a..d5b931acd6b 100644 --- a/lib/mock/mock-utils.js +++ b/lib/mock/mock-utils.js @@ -19,6 +19,7 @@ const { } } = require('node:util') const { MockCallHistory } = require('./mock-call-history') +const { InvalidArgumentError } = require('../core/errors') function matchValue (match, value) { if (typeof match === 'string') { @@ -381,9 +382,14 @@ function checkNetConnect (netConnect, origin) { return false } -function buildMockOptions (opts) { +function buildAndValidateMockOptions (opts) { if (opts) { const { agent, ...mockOptions } = opts + + if ('enableCallHistory' in mockOptions && typeof mockOptions.enableCallHistory !== 'boolean') { + throw new InvalidArgumentError('options.enableCallHistory must to be a boolean') + } + return mockOptions } } @@ -401,7 +407,7 @@ module.exports = { mockDispatch, buildMockDispatch, checkNetConnect, - buildMockOptions, + buildAndValidateMockOptions, getHeaderByName, buildHeadersFromArray } diff --git a/test/mock-agent.js b/test/mock-agent.js index 98e0abdddaf..44f663151f7 100644 --- a/test/mock-agent.js +++ b/test/mock-agent.js @@ -51,9 +51,8 @@ describe('MockAgent - constructor', () => { test('should disable call history by default', t => { t = tspl(t, { plan: 2 }) - const agent = new Agent() - after(() => agent.close()) const mockAgent = new MockAgent() + after(() => mockAgent.close()) t.strictEqual(mockAgent[kMockAgentIsCallHistoryEnabled], false) t.strictEqual(MockCallHistory[kMockCallHistoryAllMockCallHistoryInstances].size, 0) @@ -61,13 +60,27 @@ describe('MockAgent - constructor', () => { test('should enable call history if option is true', t => { t = tspl(t, { plan: 2 }) - const agent = new Agent() - after(() => agent.close()) const mockAgent = new MockAgent({ enableCallHistory: true }) + after(() => mockAgent.close()) t.strictEqual(mockAgent[kMockAgentIsCallHistoryEnabled], true) t.strictEqual(MockCallHistory[kMockCallHistoryAllMockCallHistoryInstances].size, 1) }) + + test('should disable call history if option is false', t => { + t = tspl(t, { plan: 2 }) + after(() => mockAgent.close()) + const mockAgent = new MockAgent({ enableCallHistory: false }) + + t.strictEqual(mockAgent[kMockAgentIsCallHistoryEnabled], false) + t.strictEqual(MockCallHistory[kMockCallHistoryAllMockCallHistoryInstances].size, 0) + }) + + test('should throw if enableCallHistory option is not a boolean', t => { + t = tspl(t, { plan: 1 }) + + t.throws(() => new MockAgent({ enableCallHistory: 'hello' }), new InvalidArgumentError('options.enableCallHistory must to be a boolean')) + }) }) describe('MockAgent - enableCallHistory', t => { @@ -86,7 +99,7 @@ describe('MockAgent - enableCallHistory', t => { await fetch('http://localhost:9999/foo') - t.strictEqual(mockAgent.getCallHistory()?.calls()?.length, 0) + t.strictEqual(mockAgent.getCallHistory()?.calls()?.length, undefined) mockAgent.enableCallHistory()