Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/docs/api/MockClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ const mockClient = mockAgent.get('http://localhost:3000')

Implements: [`MockPool.intercept(options)`](/docs/docs/api/MockPool.md#mockpoolinterceptoptions)

### `MockClient.cleanMocks()`

Implements: [`MockPool.cleanMocks()`](/docs/docs/api/MockPool.md#mockpoolcleanmocks)

### `MockClient.close()`

Implements: [`MockPool.close()`](/docs/docs/api/MockPool.md#mockpoolclose)
Expand Down
6 changes: 6 additions & 0 deletions docs/docs/api/MockPool.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,3 +546,9 @@ for await (const data of body) {
console.log('data', data.toString('utf8')) // data foo
}
```

### `MockPool.cleanMocks()`

This method cleans up all the prepared mocks.

Returns: `void`
4 changes: 4 additions & 0 deletions lib/mock/mock-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class MockClient extends Client {
)
}

cleanMocks () {
this[kDispatches] = []
}

async [kClose] () {
await promisify(this[kOriginalClose])()
this[kConnected] = 0
Expand Down
4 changes: 4 additions & 0 deletions lib/mock/mock-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class MockPool extends Pool {
)
}

cleanMocks () {
this[kDispatches] = []
}

async [kClose] () {
await promisify(this[kOriginalClose])()
this[kConnected] = 0
Expand Down
40 changes: 40 additions & 0 deletions test/mock-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,43 @@ test('MockClient - basic intercept with MockClient.request', async (t) => {
foo: 'bar'
})
})

test('MockClient - cleans mocks', async (t) => {
t = tspl(t, { plan: 4 })

const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
res.setHeader('content-type', 'text/plain')
res.end('hello')
})
after(() => server.close())

await promisify(server.listen.bind(server))(0)

const baseUrl = `http://localhost:${server.address().port}`

const mockAgent = new MockAgent({ connections: 1 })
after(() => mockAgent.close())

const mockClient = mockAgent.get(baseUrl)
t.ok(mockClient instanceof MockClient)
setGlobalDispatcher(mockClient)

mockClient.intercept({
path: '/foo',
method: 'GET'
}).reply(500, () => {
t.fail('should not be called')
})

mockClient.cleanMocks()

t.strictEqual(mockClient[kDispatches].length, 0)

const { statusCode, body } = await request(`${baseUrl}/foo`, {
method: 'GET'
})
t.strictEqual(statusCode, 200)

const response = await getResponse(body)
t.deepStrictEqual(response, 'hello')
})
41 changes: 41 additions & 0 deletions test/mock-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,44 @@ test('MockPool - allows matching headers in fetch', async (t) => {
}
}), new TypeError('fetch failed'))
})

test('MockPool - cleans mocks', async (t) => {
t = tspl(t, { plan: 4 })

const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
res.setHeader('content-type', 'text/plain')
res.end('hello')
})
after(() => server.close())

await promisify(server.listen.bind(server))(0)

const baseUrl = `http://localhost:${server.address().port}`

const mockAgent = new MockAgent()
after(() => mockAgent.close())

const mockPool = mockAgent.get(baseUrl)
t.ok(mockPool instanceof MockPool)
setGlobalDispatcher(mockPool)

mockPool.intercept({
path: '/foo',
method: 'GET'
}).reply(500, () => {
t.fail('should not be called')
t.end()
})

mockPool.cleanMocks()

t.strictEqual(mockPool[kDispatches].length, 0)

const { statusCode, body } = await request(`${baseUrl}/foo`, {
method: 'GET'
})
t.strictEqual(statusCode, 200)

const response = await getResponse(body)
t.deepStrictEqual(response, 'hello')
})
3 changes: 3 additions & 0 deletions test/types/mock-client.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ import { MockInterceptor } from '../../types/mock-interceptor'

// close
expectAssignable<Promise<void>>(mockClient.close())

// cleanMocks
expectAssignable<void>(mockClient.cleanMocks())
}

expectAssignable<MockClient>(new MockClient('', { agent: new MockAgent({ connections: 1 }) }))
3 changes: 3 additions & 0 deletions test/types/mock-pool.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ import { MockInterceptor } from '../../types/mock-interceptor'

// close
expectAssignable<Promise<void>>(mockPool.close())

// cleanMocks
expectAssignable<void>(mockPool.cleanMocks())
}

expectAssignable<MockPool>(new MockPool('', { agent: new MockAgent({ connections: 1 }) }))
2 changes: 2 additions & 0 deletions types/mock-client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ declare class MockClient extends Client implements Interceptable {
dispatch (options: Dispatcher.DispatchOptions, handlers: Dispatcher.DispatchHandler): boolean
/** Closes the mock client and gracefully waits for enqueued requests to complete. */
close (): Promise<void>
/** Clean up all the prepared mocks. */
cleanMocks (): void
}

declare namespace MockClient {
Expand Down
2 changes: 2 additions & 0 deletions types/mock-interceptor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ declare namespace MockInterceptor {
interface Interceptable extends Dispatcher {
/** Intercepts any matching requests that use the same origin as this mock client. */
intercept(options: MockInterceptor.Options): MockInterceptor;
/** Clean up all the prepared mocks. */
cleanMocks (): void
}

export {
Expand Down
2 changes: 2 additions & 0 deletions types/mock-pool.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ declare class MockPool extends Pool implements Interceptable {
dispatch (options: Dispatcher.DispatchOptions, handlers: Dispatcher.DispatchHandler): boolean
/** Closes the mock pool and gracefully waits for enqueued requests to complete. */
close (): Promise<void>
/** Clean up all the prepared mocks. */
cleanMocks (): void
}

declare namespace MockPool {
Expand Down
Loading