From fad8f0e12e6b41588bbdff97855d48cbf0e9ba76 Mon Sep 17 00:00:00 2001 From: Martynas_Luneckas Date: Mon, 8 Jun 2020 22:12:07 +0300 Subject: [PATCH 1/3] Added teremock dependency, added teremock helper and 4 tests --- example/tests/requestMocking.test.js | 85 ++++++++++++++++++++++++++++ framework/requestMocker.js | 19 +++++++ index.js | 1 + package.json | 3 +- 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 example/tests/requestMocking.test.js create mode 100644 framework/requestMocker.js diff --git a/example/tests/requestMocking.test.js b/example/tests/requestMocking.test.js new file mode 100644 index 0000000..48cf227 --- /dev/null +++ b/example/tests/requestMocking.test.js @@ -0,0 +1,85 @@ +import { RequestMocker, Element, Helpers } from "test-juggler"; +const fs = require("fs"); + +describe("Request mocking by recording and replaying and indercetipt specific requests", () => { + + let mocker = new RequestMocker(); + let helpers = new Helpers(); + + it("Recording all requests and replaying it on the second test run google", async () => { + + //Arrange + await mocker.start(); + + //Act + await helpers.goToUrlAndLoad("https://downforeveryoneorjustme.com/google"); + await mocker.stop(); + + //Assert + expect(fs.existsSync("mockData\\Request mocking by recording and replaying and indercetipt specific requests\\Recording all requests and replaying it on the second test run google\\interceptors\\get.json")).toBe(true); + }); + + it("Interceptor response 404 to all requests", async () => { + + //Arrange + const errorTextElement = new Element("div.humane.humane-jackedup-error.humane-animate"); + const notFoundInterceptor = { + response: { status: 404 } + }; + + //Act + await mocker.start(notFoundInterceptor); + await helpers.goToUrlAndLoad("https://www.cheapshark.com/"); + await mocker.stop(); + const actualText = await errorTextElement.text(); + + //Assert + expect(actualText).toMatch("Error - 404 Not Found"); + }); + + it("Interceptor changing response body", async () => { + + //Arrange + const errorTextElement = new Element("#json > span:nth-child(42)"); + const responseBodyChanged = { + url: "https://api.ratesapi.io/api/latest", + response: { + status: 200, + body: { "base": "EUR", "rates": { "This": 0.89448, "Response": 8.7809, "is": 15882.4, "mocked": 3.9172, "!": 7.4564 }, "date": "2020-06-05" } + } + }; + + //Act + await mocker.start(responseBodyChanged); + await helpers.goToUrlAndLoad("https://ratesapi.io/"); + await mocker.stop(); + const actualText = await errorTextElement.text(); + + //Assert + expect(actualText).toMatch("\"mocked\""); + }); + + + it("Interceptor delaying response by 3s (ttfb)", async () => { + + //Arrange + const timeStamp = Date.now(); + const responseBodyChanged = { + url: "https://api.ratesapi.io/api/latest", + response: { + status: 200, + body: { "base": "EUR", "rates": { "This": 0.89448, "Response": 8.7809, "is": 15882.4, "mocked": 3.9172, "!": 7.4564 }, "date": "2020-06-05" }, + ttfb: 3000 + } + }; + + //Act + await mocker.start(responseBodyChanged); + await helpers.goToUrlAndLoad("https://ratesapi.io/"); + const pageLoadTimeinMs = Date.now() - timeStamp; + await mocker.stop(); + + //Assert + expect(pageLoadTimeinMs).toBeGreaterThan(3000); + }); +}); \ No newline at end of file diff --git a/framework/requestMocker.js b/framework/requestMocker.js new file mode 100644 index 0000000..c7f2123 --- /dev/null +++ b/framework/requestMocker.js @@ -0,0 +1,19 @@ +/*global page*/ +import teremock from "teremock"; + +const DEFAULT_INTERCEPTOR_CAPTURE = { + resourceTypes: "xhr,fetch" +}; + +export default class RequestMocker { + + async stop() { + await teremock.stop(); + } + + async start(interceptors = DEFAULT_INTERCEPTOR_CAPTURE) { + + const targetDir = `./mockData/${jasmine["currentSuite"].fullName}/${jasmine["currentTest"].description}`; + await teremock.start({ page, wd: targetDir, interceptors: { interceptors } }); + } +} \ No newline at end of file diff --git a/index.js b/index.js index bc77592..a06be7c 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ export { default as Element } from "./framework/Element"; export { default as Helpers } from "./framework/helpers"; +export { default as RequestMocker } from "./framework/requestMocker"; export { default as Interceptor } from "./framework/interceptor"; \ No newline at end of file diff --git a/package.json b/package.json index d559402..022e2bc 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,8 @@ "jest-image-snapshot": "^3.0.0", "jest-junit": "^10.0.0", "jest-puppeteer": "^4.0.0", - "puppeteer": "^3.0.0" + "puppeteer": "^3.0.0", + "teremock": "^1.0.5" }, "devDependencies": { "eslint": "^6.8.0", From 0ccf35c6a27725be8bb0151c03aed67200ed50be Mon Sep 17 00:00:00 2001 From: Martynas_Luneckas Date: Fri, 12 Jun 2020 08:39:57 +0300 Subject: [PATCH 2/3] Added documentation --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 802e84d..9ad37db 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,13 @@ * interceptor.waitForRequestAfterAction() waits for specific or any first request and returns all its data. * interceptor.waitForResponseAfterAction() waits for specific or any first response and returns all its data. +### Request Mocking ### + +* We are using teremock - Easy to use test request mocker for puppeteer / mocha / karma +* The documentation is located at: https://github.com/Diokuz/teremock +* The Helper (RequestMocker) that is implemented has simplified behavior where it will record all xhr and fetch responses from backend and when replay on the subsequent runs. +Default behavior can be overridden with custom options passed. The options allow to target speicic requests and allow to specify response body, headers, status and delay. + ### Parallel execution ### * By default Jest runs tests in parallel with a worker pool of child processes * The console commands responsible for parallelization settings ([Official Jest documentation](https://jestjs.io/docs/en/cli.html)): From 67174bf5e1e3f257097d4998dd2bb34efbd13754 Mon Sep 17 00:00:00 2001 From: Martynas_Luneckas Date: Mon, 24 Aug 2020 08:19:36 +0300 Subject: [PATCH 3/3] fixed pr comments --- README.md | 2 +- example/tests/requestMocking.test.js | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 9ad37db..75f454a 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ * We are using teremock - Easy to use test request mocker for puppeteer / mocha / karma * The documentation is located at: https://github.com/Diokuz/teremock * The Helper (RequestMocker) that is implemented has simplified behavior where it will record all xhr and fetch responses from backend and when replay on the subsequent runs. -Default behavior can be overridden with custom options passed. The options allow to target speicic requests and allow to specify response body, headers, status and delay. +Default behavior can be overridden with custom options passed. The options allow to target specific requests and allow to specify response body, headers, status and delay. ### Parallel execution ### * By default Jest runs tests in parallel with a worker pool of child processes diff --git a/example/tests/requestMocking.test.js b/example/tests/requestMocking.test.js index 48cf227..f0276f0 100644 --- a/example/tests/requestMocking.test.js +++ b/example/tests/requestMocking.test.js @@ -1,13 +1,12 @@ import { RequestMocker, Element, Helpers } from "test-juggler"; const fs = require("fs"); -describe("Request mocking by recording and replaying and indercetipt specific requests", () => { +describe("Request mocking by recording and replaying specific requests", () => { let mocker = new RequestMocker(); let helpers = new Helpers(); it("Recording all requests and replaying it on the second test run google", async () => { - //Arrange await mocker.start(); @@ -20,7 +19,6 @@ describe("Request mocking by recording and replaying and indercetipt specific re }); it("Interceptor response 404 to all requests", async () => { - //Arrange const errorTextElement = new Element("div.humane.humane-jackedup-error.humane-animate"); const notFoundInterceptor = { @@ -38,7 +36,6 @@ describe("Request mocking by recording and replaying and indercetipt specific re }); it("Interceptor changing response body", async () => { - //Arrange const errorTextElement = new Element("#json > span:nth-child(42)"); const responseBodyChanged = { @@ -59,9 +56,7 @@ describe("Request mocking by recording and replaying and indercetipt specific re expect(actualText).toMatch("\"mocked\""); }); - it("Interceptor delaying response by 3s (ttfb)", async () => { - //Arrange const timeStamp = Date.now(); const responseBodyChanged = {