diff --git a/docs/api/MockAgent.md b/docs/api/MockAgent.md index 04cfcb0e554..5c8eda24937 100644 --- a/docs/api/MockAgent.md +++ b/docs/api/MockAgent.md @@ -465,7 +465,7 @@ agent.disableNetConnect() agent .get('https://example.com') .intercept({ method: 'GET', path: '/' }) - .reply(200, '') + .reply(200) const pendingInterceptors = agent.pendingInterceptors() // Returns [ @@ -508,7 +508,7 @@ agent.disableNetConnect() agent .get('https://example.com') .intercept({ method: 'GET', path: '/' }) - .reply(200, '') + .reply(200) agent.assertNoPendingInterceptors() // Throws an UndiciError with the following message: diff --git a/lib/mock/mock-interceptor.js b/lib/mock/mock-interceptor.js index b51e6f2df3f..781e477502a 100644 --- a/lib/mock/mock-interceptor.js +++ b/lib/mock/mock-interceptor.js @@ -130,7 +130,7 @@ class MockInterceptor { throw new InvalidArgumentError('reply options callback must return an object') } - const { statusCode, data, responseOptions = {} } = resolvedData + const { statusCode, data = '', responseOptions = {} } = resolvedData this.validateReplyParameters(statusCode, data, responseOptions) // Since the values can be obtained immediately we return them // from this higher order function that will be resolved later. @@ -145,10 +145,10 @@ class MockInterceptor { } // We can have either one or three parameters, if we get here, - // we should have 2-3 parameters. So we spread the arguments of + // we should have 1-3 parameters. So we spread the arguments of // this function to obtain the parameters, since replyData will always // just be the statusCode. - const [statusCode, data, responseOptions = {}] = [...arguments] + const [statusCode, data = '', responseOptions = {}] = [...arguments] this.validateReplyParameters(statusCode, data, responseOptions) // Send in-already provided data like usual diff --git a/test/mock-interceptor.js b/test/mock-interceptor.js index 20bf649b3e8..a11377d3af0 100644 --- a/test/mock-interceptor.js +++ b/test/mock-interceptor.js @@ -32,14 +32,13 @@ test('MockInterceptor - reply', t => { }) t.test('should error if passed options invalid', t => { - t.plan(3) + t.plan(2) const mockInterceptor = new MockInterceptor({ path: '', method: '' }, []) t.throws(() => mockInterceptor.reply(), new InvalidArgumentError('statusCode must be defined')) - t.throws(() => mockInterceptor.reply(200), new InvalidArgumentError('data must be defined')) t.throws(() => mockInterceptor.reply(200, '', 'hello'), new InvalidArgumentError('responseOptions must be an object')) }) }) @@ -113,7 +112,7 @@ test('MockInterceptor - reply options callback', t => { }) t.test('should error if passed options invalid', async (t) => { - t.plan(4) + t.plan(3) const baseUrl = 'http://localhost:9999' const mockAgent = new MockAgent() @@ -126,13 +125,6 @@ test('MockInterceptor - reply options callback', t => { method: 'GET' }).reply(() => {}) - mockPool.intercept({ - path: '/test2', - method: 'GET' - }).reply(() => ({ - statusCode: 200 - })) - mockPool.intercept({ path: '/test3', method: 'GET' @@ -159,15 +151,6 @@ test('MockInterceptor - reply options callback', t => { onComplete: () => {} }), new InvalidArgumentError('reply options callback must return an object')) - t.throws(() => mockPool.dispatch({ - path: '/test2', - method: 'GET' - }, { - onHeaders: () => {}, - onData: () => {}, - onComplete: () => {} - }), new InvalidArgumentError('data must be defined')) - t.throws(() => mockPool.dispatch({ path: '/test3', method: 'GET' diff --git a/test/types/mock-interceptor.test-d.ts b/test/types/mock-interceptor.test-d.ts index b3c939aecf9..ed0c0512529 100644 --- a/test/types/mock-interceptor.test-d.ts +++ b/test/types/mock-interceptor.test-d.ts @@ -12,6 +12,7 @@ expectAssignable(mockResponseCall const mockInterceptorDefaultMethod = mockPool.intercept({ path: '' }) // reply + expectAssignable(mockInterceptor.reply(200)) expectAssignable(mockInterceptor.reply(200, '')) expectAssignable(mockInterceptor.reply(200, Buffer)) expectAssignable(mockInterceptor.reply(200, {})) @@ -55,7 +56,7 @@ expectAssignable(mockResponseCall { const mockPool: MockPool = new MockAgent().get('') - const mockScope = mockPool.intercept({ path: '', method: 'GET' }).reply(200, '') + const mockScope = mockPool.intercept({ path: '', method: 'GET' }).reply(200) // delay expectAssignable(mockScope.delay(1)) diff --git a/types/mock-interceptor.d.ts b/types/mock-interceptor.d.ts index b7a2b339c04..8812960573f 100644 --- a/types/mock-interceptor.d.ts +++ b/types/mock-interceptor.d.ts @@ -26,7 +26,7 @@ declare class MockInterceptor { reply(replyOptionsCallback: MockInterceptor.MockReplyOptionsCallback): MockScope; reply( statusCode: number, - data: TData | Buffer | string | MockInterceptor.MockResponseDataHandler, + data?: TData | Buffer | string | MockInterceptor.MockResponseDataHandler, responseOptions?: MockInterceptor.MockResponseOptions ): MockScope; /** Mock an undici request by throwing the defined reply error. */ @@ -84,7 +84,7 @@ declare namespace MockInterceptor { export type MockReplyOptionsCallback = ( opts: MockResponseCallbackOptions - ) => { statusCode: number, data: TData | Buffer | string, responseOptions?: MockResponseOptions } + ) => { statusCode: number, data?: TData | Buffer | string, responseOptions?: MockResponseOptions } } interface Interceptable extends Dispatcher {