From 115b7e42c6bd2a7c0eb5831959838acc5ad740aa Mon Sep 17 00:00:00 2001 From: cexbrayat Date: Thu, 8 Nov 2018 16:25:29 +0100 Subject: [PATCH] fix(common): http/testing expectOne lists the received requests if no matches (#27005) Fixes #18013 Previously it was hard to debug an `expectOne` if the request had no match, as the error message was: Expected one matching request for criteria "Match URL: /some-url?query=hello", found none. This commit adds a bit more info to the error, by listing the actual requests received: Expected one matching request for criteria "Match URL: /some-url?query=hello", found none. Requests received are: POST /some-url?query=world. PR Close #27005 --- packages/common/http/testing/src/backend.ts | 14 +++++++++++++- packages/common/http/testing/test/request_spec.ts | 7 +++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/common/http/testing/src/backend.ts b/packages/common/http/testing/src/backend.ts index d5311fd551..75a32389b8 100644 --- a/packages/common/http/testing/src/backend.ts +++ b/packages/common/http/testing/src/backend.ts @@ -90,7 +90,19 @@ export class HttpClientTestingBackend implements HttpBackend, HttpTestingControl `Expected one matching request for criteria "${description}", found ${matches.length} requests.`); } if (matches.length === 0) { - throw new Error(`Expected one matching request for criteria "${description}", found none.`); + let message = `Expected one matching request for criteria "${description}", found none.`; + if (this.open.length > 0) { + // Show the methods and URLs of open requests in the error, for convenience. + const requests = this.open + .map(testReq => { + const url = testReq.request.urlWithParams; + const method = testReq.request.method; + return `${method} ${url}`; + }) + .join(', '); + message += ` Requests received are: ${requests}.`; + } + throw new Error(message); } return matches[0]; } diff --git a/packages/common/http/testing/test/request_spec.ts b/packages/common/http/testing/test/request_spec.ts index aefb5d0a4e..2b03893447 100644 --- a/packages/common/http/testing/test/request_spec.ts +++ b/packages/common/http/testing/test/request_spec.ts @@ -36,7 +36,9 @@ describe('HttpClient TestRequest', () => { fail(); } catch (error) { expect(error.message) - .toBe('Expected one matching request for criteria "Match URL: /some-url", found none.'); + .toBe( + 'Expected one matching request for criteria "Match URL: /some-url", found none.' + + ' Requests received are: GET /some-other-url.'); } }); @@ -55,7 +57,8 @@ describe('HttpClient TestRequest', () => { } catch (error) { expect(error.message) .toBe( - 'Expected one matching request for criteria "Match URL: /some-url?query=world", found none.'); + 'Expected one matching request for criteria "Match URL: /some-url?query=world", found none.' + + ' Requests received are: GET /some-url?query=hello.'); } }); });