diff --git a/README.md b/README.md index 97283a7..80c2c7a 100644 --- a/README.md +++ b/README.md @@ -444,3 +444,7 @@ expect(patchClientRequest).toEqual({ params: { clientId: '' } }) ``` + +#### `waitForRequestCount(n: number): Promise` + +Waits until mock is matched my `n` requests. Throws error when timeout (equal to 100ms) is exceeded. diff --git a/src/mock.ts b/src/mock.ts index 9f5eaae..22e777d 100644 --- a/src/mock.ts +++ b/src/mock.ts @@ -98,6 +98,24 @@ export class Mock { return this.requests[index]; } + public async waitForRequestsCount(count: number): Promise { + try { + await waitFor( + () => this.requests.length === count, + GET_REQUEST_TIMEOUT + ); + } catch (e) { + if (e instanceof TimeoutError) { + throw new Error( + `Expected ${count} requests to match mock [${this.prettyPrint()}]. Instead ${ + this.requests.length + } request were matched.` + ); + } + throw e; + } + } + public getResponseForRequest( request: Request ): MockedResponseObject | null { @@ -211,7 +229,7 @@ export class Mock { private prettyPrint(): string { const qs = stringify(this.filter.query); - return `(${this.debugId}) ${this.filter.method} ${this.filter.path + - (qs ? '?' + qs : '')}`; + return `(${this.debugId}) ${this.filter.method || 'HTTP'} ${this.filter + .path + (qs ? '?' + qs : '')}`; } } diff --git a/test/integration/mocketeer.int.test.ts b/test/integration/mocketeer.int.test.ts index 0fec0d5..dab8dad 100644 --- a/test/integration/mocketeer.int.test.ts +++ b/test/integration/mocketeer.int.test.ts @@ -706,4 +706,22 @@ describe('Mocketeer integration', () => { 'application/json; charset=utf-8' ); }); + + test('wait for a number of requests to be matched', async () => { + const mock = await mocketeer.mock('/example', { status: 200 }); + await makeRequest('GET', '/example'); + await makeRequest('GET', '/example'); + await mock.waitForRequestsCount(2); + }); + + test('wait for a number of requests to be matched - async scenario', async () => { + const mock = await mocketeer.mock('/example', { status: 200 }); + await makeRequest('GET', '/example', {}, undefined, { + waitForRequestEnd: false, + }); + await makeRequest('GET', '/example', {}, undefined, { + waitForRequestEnd: false, + }); + await mock.waitForRequestsCount(2); + }); }); diff --git a/test/integration/test-helpers/make-request-factory.ts b/test/integration/test-helpers/make-request-factory.ts index ed32184..7a2bf27 100644 --- a/test/integration/test-helpers/make-request-factory.ts +++ b/test/integration/test-helpers/make-request-factory.ts @@ -5,9 +5,10 @@ export function makeRequestFactory(page: Page) { method: string, url: string, headers?: Record, - body?: any + body?: any, + options?: { waitForRequestEnd: boolean } ): Promise<{ status: number; headers: Record; body: any }> { - return page.evaluate(makeRequest, url, method, headers, body); + return page.evaluate(makeRequest, url, method, headers, body, options); }; } @@ -15,7 +16,8 @@ function makeRequest( url: string, method: string, headers: Record, - body + body, + options: { waitForRequestEnd: boolean } = { waitForRequestEnd: true } ) { function headersToObject(headers: Headers): Record { const headerObj = {}; @@ -34,15 +36,25 @@ function makeRequest( } } - return fetch(url, { method, headers, body }) - .then(res => { - return Promise.all([res.status, res.headers, res.text()]); - }) - .then(([status, headers, body]) => { - return { - status, - body: getParsedBody(body), - headers: headersToObject(headers), - }; - }); + function makeRequest() { + return fetch(url, { method, headers, body }) + .then(res => { + return Promise.all([res.status, res.headers, res.text()]); + }) + .then(([status, headers, body]) => { + return { + status, + body: getParsedBody(body), + headers: headersToObject(headers), + }; + }); + } + + const request = makeRequest(); + + if (options.waitForRequestEnd === true) { + return request; + } else { + return null; + } }