-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 766fe6f
Showing
39 changed files
with
8,605 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
; http://editorconfig.org | ||
|
||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.{js,ts}] | ||
indent_style = space | ||
indent_size = 2 | ||
|
||
[*.json] | ||
indent_style = space | ||
indent_size = 4 | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
indent_size = 4 | ||
|
||
[node_modules/**.js] | ||
codepaint = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
node_modules/ | ||
.pnp.js | ||
.pnp | ||
.idea | ||
.vscode | ||
npm-debug.log* | ||
.DS_Store | ||
coverage | ||
*.sublime* | ||
*.log | ||
dist | ||
.yarn | ||
.yarnrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
assets | ||
screenshots | ||
tests | ||
.pnp | ||
.pnp.js | ||
__remocks__ | ||
yarn.lock | ||
Makefile | ||
.editorconfig | ||
.prettierignore | ||
.prettierrc.yml | ||
changelog.md | ||
README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/assets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
printWidth: 120 | ||
semi: false | ||
singleQuote: true | ||
trailingComma: "es5" | ||
arrowParens: "always" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.PHONY: prepare | ||
prepare: | ||
yarn pnpify tsc | ||
yarn prettier --write tests/**/*.js | ||
yarn prettier --write src/**/*.ts | ||
yarn jest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# teremock | ||
|
||
## Do I need that thing? | ||
|
||
If you are writing puppeteer tests, and you want to mock your network responses easily – probably yes. | ||
|
||
## How to use | ||
|
||
```js | ||
import mocker from 'teremock' | ||
|
||
await mocker.start() | ||
|
||
// async stuff which is making requests | ||
|
||
await mocker.stop() | ||
``` | ||
|
||
## How it works | ||
|
||
First, `teremock` intercepts puppeteers page requests and tries to find its body in mocks folder. Generated filename depends on `url`, `method` and `postBody` – so, you always know, do you have a mock for that particular request or not. If you have it – you will get it as a response, instantly. If not – request will go to the real backend (see also: mockList and okList). | ||
|
||
Second, `teremock` intercepts all responds, and writes them to the filesystem, if they are not on it already. In case of `CI` (if mock was not found), it uses mockMiss middleware, so you could be sure – all your requests are mocked (or build will fail otherwise). | ||
|
||
## Pipeline | ||
|
||
<img src="assets/pipeline.svg" /> | ||
|
||
## API | ||
|
||
You could use `options` | ||
```js | ||
mocker.start(options) | ||
``` | ||
All options are optional (that's why they called so). | ||
```js | ||
const options = { | ||
// Absolute path to folder where you want to store mocks | ||
// process.cwd() + '/__remocks__' by default | ||
workDir: __dirname, | ||
|
||
// puppeteer page | ||
// global.page by default | ||
page: page, | ||
|
||
// In some cases you could have some random GET params, which are not affect the response body | ||
// but several params may be important for you (White List) | ||
// [] by default | ||
queryParams: ['important'], | ||
|
||
// In some cases you could have some random GET params, which are not affects the response body | ||
// but could lead to `always out of date` mocks (Black List) | ||
// [] by default | ||
skipQueryParams: ['randomId', 'timestamp'], | ||
|
||
// Same as skipQueryParams but for post body params | ||
// Only application/json MIME type is supported | ||
skipPostParams: [ | ||
'randomId', | ||
'timestamp', | ||
['objectParameter', 'property'] | ||
], | ||
|
||
// Probably you dont want to mock some requests (e.g. cdn js files) | ||
// And you definitely dont want to mock your webapp requests (e.g. localhost/app.js) | ||
// So, you could explicitly whitelist urls you want to mock | ||
// _all except localhost_ if both – mockList and okList – were not set | ||
// Could be an array, or a `,` delimited string | ||
mockList: 'my-backend.org/used/by/test', | ||
|
||
// It is recommended to explicitly mock only _critical-for-your-test_ urls | ||
// But you could also mock with simple 200-OK response some other requests, | ||
// which are not critical, but should be intercepted | ||
// (to prevent ddos-effect to your real backend, for example) | ||
// All items from mockList have higher priority over okList | ||
// Could be an array, or a `,` delimited string | ||
okList: ['my-backend.org/not/critical/for/test'], | ||
|
||
// If url not in mockList, nor okList, it will be blocked, unless any of two conditions | ||
// 1) url is same origin, and method is GET | ||
// 2) url is matched agains any string in passList | ||
// By default, block any cross origin and non-GET same-origin requests | ||
passList: [], | ||
|
||
// Run as CI if true. That means, your tests will fail if any of the requests were not mocked | ||
// Default is `is-ci` package value (same as in Jest) | ||
ci: require('is-ci'), | ||
|
||
// A middleware to call when mock is not found on the file system | ||
// Works only in CI mode | ||
// Possible values are: | ||
// 1) CODE (number) – respond with CODE http code for any unmocked request (e.g. 200) | ||
// 2) 'throw' (string) – will throw an error | ||
// 3) (next) => next(anyResponse) - respond with anyResponse object | ||
// default value is: 500 | ||
// Note: request is not available in the middleware function | ||
// Note: body must be a string (use JSON.stringify for objects) | ||
mockMiss: (next) => next({ code: 200, body: JSON.stringify({ foo: 'bar' }) }), | ||
|
||
// Set true, to await all non-closed connections when trying to stop mocker | ||
// Warning: some tests could became flaky | ||
awaitConnectionsOnStop: false, | ||
|
||
// Custom headers or/and body or/and status for ANY request from the mockList | ||
// All keys in the object are optional (e.g. you could change only status code) | ||
// Usefull with combination of mocker.set() method | ||
// Warning! It is not working in the mocks generation mode! So, first, create your mocks. | ||
// See also https://github.com/puppeteer/puppeteer/issues/599 | ||
response: { | ||
headers: { 'Access-Control-Allow-Origin': '*' }, | ||
body: 'OK', | ||
status: 200, | ||
}, | ||
} | ||
``` | ||
Both `mocker.start()` and `mocker.stop()` return a `Promise`. | ||
|
||
## mocker.set() | ||
|
||
You could temporary change any option and then get back to its initial value. | ||
|
||
For example: | ||
|
||
```js | ||
mocker.set('response', { status: 427 }) | ||
const result = await doRequest() | ||
expect(result).toBe('Server responded with an error, status code is 427') | ||
// note: headers and body will be taken from the mocks, only status code is changed | ||
mocker.unset('response) // or mocker.reset() | ||
``` |
27 changes: 27 additions & 0 deletions
27
__teremocks-post__/localhost-api/post-diet-sink-color.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"request": { | ||
"method": "POST", | ||
"url": "http://localhost:3000/api", | ||
"headers": { | ||
"accept": "application/json", | ||
"referer": "http://localhost:3000/", | ||
"origin": "http://localhost:3000", | ||
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/79.0.3945.0 Safari/537.36", | ||
"content-type": "application/json" | ||
}, | ||
"postData": { | ||
"q": "abc" | ||
} | ||
}, | ||
"response": { | ||
"status": 200, | ||
"headers": { | ||
"access-control-allow-origin": "*", | ||
"connection": "keep-alive", | ||
"content-type": "application/json; charset=utf-8" | ||
}, | ||
"body": { | ||
"suggest": "green" | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
__teremocks-post__/localhost-api/post-enemy-miss-fillet.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"request": { | ||
"method": "POST", | ||
"url": "http://localhost:3000/api", | ||
"headers": { | ||
"accept": "application/json", | ||
"referer": "http://localhost:3000/", | ||
"origin": "http://localhost:3000", | ||
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/79.0.3945.0 Safari/537.36", | ||
"content-type": "application/json" | ||
}, | ||
"postData": { | ||
"q": "ab" | ||
} | ||
}, | ||
"response": { | ||
"status": 200, | ||
"headers": { | ||
"access-control-allow-origin": "*", | ||
"connection": "keep-alive", | ||
"content-type": "application/json; charset=utf-8" | ||
}, | ||
"body": { | ||
"suggest": "world" | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
__teremocks-post__/localhost-api/post-jupiter-kitten-iowa.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"request": { | ||
"method": "POST", | ||
"url": "http://localhost:3000/api", | ||
"headers": { | ||
"accept": "application/json", | ||
"referer": "http://localhost:3000/", | ||
"origin": "http://localhost:3000", | ||
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/79.0.3945.0 Safari/537.36", | ||
"content-type": "application/json" | ||
}, | ||
"postData": { | ||
"q": "a" | ||
} | ||
}, | ||
"response": { | ||
"status": 200, | ||
"headers": { | ||
"access-control-allow-origin": "*", | ||
"connection": "keep-alive", | ||
"content-type": "application/json; charset=utf-8" | ||
}, | ||
"body": { | ||
"suggest": "example" | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
__teremocks-post__/localhost-api/post-social-december-moon.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"request": { | ||
"method": "POST", | ||
"url": "http://localhost:3000/api", | ||
"headers": { | ||
"accept": "application/json", | ||
"referer": "http://localhost:3000/", | ||
"origin": "http://localhost:3000", | ||
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/79.0.3945.0 Safari/537.36", | ||
"content-type": "application/json" | ||
}, | ||
"postData": { | ||
"q": "abcd" | ||
} | ||
}, | ||
"response": { | ||
"status": 200, | ||
"headers": { | ||
"access-control-allow-origin": "*", | ||
"connection": "keep-alive", | ||
"content-type": "application/json; charset=utf-8" | ||
}, | ||
"body": { | ||
"suggest": "book" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"request": { | ||
"method": "GET", | ||
"url": "http://localhost:3000/api?q=a", | ||
"headers": { | ||
"referer": "http://localhost:3000/", | ||
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/79.0.3945.0 Safari/537.36" | ||
}, | ||
"postData": "" | ||
}, | ||
"response": { | ||
"status": 200, | ||
"headers": { | ||
"access-control-allow-origin": "*", | ||
"connection": "keep-alive", | ||
"content-type": "application/json; charset=utf-8" | ||
}, | ||
"body": { | ||
"suggest": "example" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"request": { | ||
"method": "GET", | ||
"url": "http://localhost:3000/api?q=abc", | ||
"headers": { | ||
"referer": "http://localhost:3000/", | ||
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/79.0.3945.0 Safari/537.36" | ||
}, | ||
"postData": "" | ||
}, | ||
"response": { | ||
"status": 200, | ||
"headers": { | ||
"access-control-allow-origin": "*", | ||
"connection": "keep-alive", | ||
"content-type": "application/json; charset=utf-8" | ||
}, | ||
"body": { | ||
"suggest": "green" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"request": { | ||
"method": "GET", | ||
"url": "http://localhost:3000/api?q=abcd", | ||
"headers": { | ||
"referer": "http://localhost:3000/", | ||
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/79.0.3945.0 Safari/537.36" | ||
}, | ||
"postData": "" | ||
}, | ||
"response": { | ||
"status": 200, | ||
"headers": { | ||
"access-control-allow-origin": "*", | ||
"connection": "keep-alive", | ||
"content-type": "application/json; charset=utf-8" | ||
}, | ||
"body": { | ||
"suggest": "book" | ||
} | ||
} | ||
} |
Oops, something went wrong.