Skip to content

Commit

Permalink
fix: throw if enableCallHistory is not a boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
blephy committed Jan 30, 2025
1 parent 200d8dc commit 7d9c679
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
8 changes: 5 additions & 3 deletions lib/mock/mock-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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') {
Expand All @@ -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]()
Expand Down
10 changes: 8 additions & 2 deletions lib/mock/mock-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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
}
}
Expand All @@ -401,7 +407,7 @@ module.exports = {
mockDispatch,
buildMockDispatch,
checkNetConnect,
buildMockOptions,
buildAndValidateMockOptions,
getHeaderByName,
buildHeadersFromArray
}
23 changes: 18 additions & 5 deletions test/mock-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,36 @@ 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)
})

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 => {
Expand All @@ -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()

Expand Down

0 comments on commit 7d9c679

Please sign in to comment.