From 24de5065cbff8ee36726694f0cd5e3c666eccd48 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Wed, 29 Mar 2023 19:16:26 +0100 Subject: [PATCH] test(index): use `describe.each()()` for duplicate test suites --- src/index.test.js | 68 +++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/src/index.test.js b/src/index.test.js index c7b1256..dd2de8c 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -87,55 +87,49 @@ describe("JSON-To-XML plugin", () => { { testName: "'application/xml' as only value in accept HTTP request header", - request: { - headers: { - accept: "application/xml", - }, + headers: { + accept: "application/xml", }, }, { testName: "'application/xml' before 'application/json' in accept HTTP request header", - request: { - headers: { - accept: "application/xml, application/json", - }, + headers: { + accept: "application/xml, application/json", }, }, ]; - xmlTests.forEach((testObject) => { - describe(`${testObject.testName}`, () => { - test("Should return HTTP status code 500 due to invalid XML characters", async () => { - const response = await server.inject({ - method: "GET", - url: "/no-replace", - headers: testObject.request.headers, - }); - - expect(response.payload).toBe( - '500Internal Server Errorin XML document > element "response": element name "$test-key" should not contain characters not allowed in XML names' - ); - expect(response.headers["content-type"]).toBe( - "application/xml; charset=utf-8" - ); - expect(response.statusCode).toBe(500); + describe.each(xmlTests)("$testName", ({ headers }) => { + test("Should return HTTP status code 500 due to invalid XML characters", async () => { + const response = await server.inject({ + method: "GET", + url: "/no-replace", + headers, }); - test("Should return XML payload with invalid XML characters replaced", async () => { - const response = await server.inject({ - method: "GET", - url: "/replace", - headers: testObject.request.headers, - }); + expect(response.payload).toBe( + '500Internal Server Errorin XML document > element "response": element name "$test-key" should not contain characters not allowed in XML names' + ); + expect(response.headers["content-type"]).toBe( + "application/xml; charset=utf-8" + ); + expect(response.statusCode).toBe(500); + }); - expect(response.payload).toBe( - 'test-value<�test-key>test-value' - ); - expect(response.headers["content-type"]).toBe( - "application/xml; charset=utf-8" - ); - expect(response.statusCode).toBe(200); + test("Should return XML payload with invalid XML characters replaced", async () => { + const response = await server.inject({ + method: "GET", + url: "/replace", + headers, }); + + expect(response.payload).toBe( + 'test-value<�test-key>test-value' + ); + expect(response.headers["content-type"]).toBe( + "application/xml; charset=utf-8" + ); + expect(response.statusCode).toBe(200); }); }); });