Skip to content

Commit

Permalink
feat: FE-132 add mock.waitForRequestsCount()
Browse files Browse the repository at this point in the history
This method allows to check if mock was matched by given number of requests. It is implemented as async, throwing method to cover edge cases when request is made with delay, after this method is invoked in test code.
  • Loading branch information
lukaszfiszer committed Mar 19, 2020
1 parent b7273fe commit 142c6b2
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 16 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,7 @@ expect(patchClientRequest).toEqual({
params: { clientId: '' }
})
```

#### `waitForRequestCount(n: number): Promise<void>`

Waits until mock is matched my `n` requests. Throws error when timeout (equal to 100ms) is exceeded.
22 changes: 20 additions & 2 deletions src/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ export class Mock {
return this.requests[index];
}

public async waitForRequestsCount(count: number): Promise<void> {
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 {
Expand Down Expand Up @@ -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 : '')}`;
}
}
18 changes: 18 additions & 0 deletions test/integration/mocketeer.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
40 changes: 26 additions & 14 deletions test/integration/test-helpers/make-request-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ export function makeRequestFactory(page: Page) {
method: string,
url: string,
headers?: Record<string, string>,
body?: any
body?: any,
options?: { waitForRequestEnd: boolean }
): Promise<{ status: number; headers: Record<string, string>; body: any }> {
return page.evaluate(makeRequest, url, method, headers, body);
return page.evaluate(makeRequest, url, method, headers, body, options);
};
}

function makeRequest(
url: string,
method: string,
headers: Record<string, string>,
body
body,
options: { waitForRequestEnd: boolean } = { waitForRequestEnd: true }
) {
function headersToObject(headers: Headers): Record<string, string> {
const headerObj = {};
Expand All @@ -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;
}
}

0 comments on commit 142c6b2

Please sign in to comment.