-
Notifications
You must be signed in to change notification settings - Fork 6
Back end mocking with teremock #53
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { RequestMocker, Element, Helpers } from "test-juggler"; | ||
const fs = require("fs"); | ||
|
||
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(); | ||
|
||
//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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use "/" slashes to be cross-platform and consistent with other paths in framework There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Path should be updated according to describe: "Request mocking by recording and replaying and indercetipt specific requests" > "Request mocking by recording and replaying specific requests" |
||
}); | ||
|
||
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\""); | ||
}); | ||
|
||
Martynienas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } }); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no constructor, class should be refactored to single ton pattern, like i.e.: https://github.com/devbridge/test-juggler/blob/master/framework/interceptor.js |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test fails: