Skip to content

Commit

Permalink
Merge pull request #539 from wheresrhys/spy-on-route
Browse files Browse the repository at this point in the history
Spy on route
  • Loading branch information
wheresrhys committed May 5, 2020
2 parents 6b9c47b + 20c67c2 commit 2a9066c
Show file tree
Hide file tree
Showing 39 changed files with 188 additions and 134 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ references:
only: /.*/
env: &env
docker:
- image: circleci/node:8.9.0
- image: circleci/node:10.20.1
env-browsers: &env-browsers
docker:
- image: circleci/node:8.9.0-browsers
- image: circleci/node:10.20.1-browsers

jobs:
checkout_code:
Expand Down
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
"node": true
},
"parserOptions": {
"ecmaVersion": 2017
"ecmaVersion": 2018
},
"globals": {
"expect": true
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ typecheck:

lint-ci: typecheck
eslint --ignore-pattern test/fixtures/* src test
prettier *.md
prettier *.md docs/*.md docs/**/*.md

lint:
eslint --cache --fix .
prettier --write *.md
eslint --cache --fix --ignore-pattern test/fixtures/* src test
prettier --write *.md docs/*.md docs/**/*.md

coverage-report:
nyc --reporter=lcovonly --reporter=text mocha test/server.js
Expand Down
1 change: 0 additions & 1 deletion docs/_about/previous-versions.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ content_markdown: |-
- [v7 upgrade guide](https://github.com/wheresrhys/fetch-mock/blob/master/docs/v6-v7-upgrade-guide.md)
- [v6 docs](https://github.com/wheresrhys/fetch-mock/tree/4231044aa94e234b53e296181ca5b6b4cecb6e3f/docs)
- [v5 docs](https://github.com/wheresrhys/fetch-mock/tree/b8270640d5711feffb01d1bf85bb7da95179c4de/docs)
---
1 change: 0 additions & 1 deletion docs/_about/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,4 @@ content_markdown: |-
$ node mocked.js
'got data' { hello: 'world' }
```
---
5 changes: 2 additions & 3 deletions docs/_api-inspection/done.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ parameters:
content: Rule for matching calls to `fetch`.
options:
- types:
- undefined
- true
- undefined
- true
content: |-
Returns true if all routes have been called the expected number of times
- name: routeIdentifier
Expand All @@ -24,5 +24,4 @@ parameters:
Returns true if the routes specified by the identifier has been called the expected number of times
content_markdown: |-
If several routes have the same matcher/url, but use [mocking options](#apimockingmock_options), the recommended way to handle this is to [name each route](#api-mockingmock_options) and filter using those names
---
16 changes: 8 additions & 8 deletions docs/_api-inspection/fundamentals.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
---
title: "Inspection fundamentals"
title: 'Inspection fundamentals'
position: 0
description: |-
Check out the new [cheatsheet](https://github.com/wheresrhys/fetch-mock/blob/master/docs/cheatsheet.md)
{: .info}
`fetch-mock`'s inspection methods allow information about how `fetch` was called to be retrieved after your application code has run. Most inspection methods take two arguments — `filter` and `options` — which allow individual, or groups of, `fetch` calls to be extracted and examined.
parameters:
- name: filter
content: |-
Filter calls to `fetch` using one of the following criteria:
options:
- types:
- undefined
- undefined
content: |-
Retrieve all calls made to `fetch`
- types:
- true
- '"matched"'
- true
- '"matched"'
content: |-
Retrieve all calls to `fetch` matched by some route defined by `fetch-mock`. The string `'matched'` can be used instead of `true` to make tests more readable
examples:
Expand All @@ -26,8 +26,8 @@ parameters:
...
fetchMock.calls(MATCHED)
- types:
- false
- '"unmatched"'
- false
- '"unmatched"'
content: |-
Retrieve all calls to `fetch` not matched by some route defined by `fetch-mock`. The string `'unmatched'` can be used instead of `false` to make tests more readable
examples:
Expand Down Expand Up @@ -60,7 +60,7 @@ parameters:
content: |-
Either an object compatible with the [mocking api](#api-mockingmock_options) or a string specifying a http method to filter by. This will be used to filter the list of calls further
content_markdown: |-
The filtering API is powerful, but potentially confusing. If in doubt, [add a `name` to your route](#api-mockingmock_options), and pass that name in to retrieve exactly the calls you want.
#### A note on Regular Expression and Function matchers
Expand Down
2 changes: 1 addition & 1 deletion docs/_api-lifecycle/flush.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: .flush(waitForBody)
navTitle: ".flush()"
navTitle: '.flush()'
position: 2
description: |-
Returns a `Promise` that resolves once all fetches handled by fetch-mock have resolved
Expand Down
2 changes: 1 addition & 1 deletion docs/_api-lifecycle/resetBehavior.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: .resetBehavior()
position: 5
description: |-
Removes all mock routes from the instance of `fetch-mock`, and restores `fetch` to its original implementation if movcking globally. Will not clear data recorded for `fetch`'s calls.
Removes all mock routes from the instance of `fetch-mock`, and restores `fetch` to its original implementation if movcking globally. Will not clear data recorded for `fetch`'s calls.
content_markdown: |-
`resetBehavior()` is bound to fetchMock, and can be used directly as a callback e.g. `afterEach(fetchMock.resetBehavior)` will work just fine. There is no need for `afterEach(() => fetchMock.resetBehavior())`
---
3 changes: 1 addition & 2 deletions docs/_api-lifecycle/sandbox.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: ".sandbox()"
title: '.sandbox()'
position: 1.0
description: |-
Returns a function that can be used as a drop-in replacement for `fetch`. Pass this into your mocking library of choice. The function returned by `sandbox()` has all the methods of `fetch-mock` exposed on it and maintains its own state independent of other instances, so tests can be run in parallel.
Expand All @@ -10,5 +10,4 @@ left_code_blocks:
.mock('http://domain.com', 200)
title: Example
language: javascript
---
2 changes: 1 addition & 1 deletion docs/_api-mocking/_defaults.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title:
title:
position:
parameters:
- name:
Expand Down
4 changes: 1 addition & 3 deletions docs/_api-mocking/catch.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: ".catch(response)"
title: '.catch(response)'
navTitle: .catch()
position: 8
description: |-
Expand All @@ -8,5 +8,3 @@ parentMethodGroup: mocking
content_markdown: |-
It accepts any valid [fetch-mock response](#api-mockingmock_response), and can also take an arbitrary function to completely customise behaviour. If no argument is passed, then every unmatched call will receive a `200` response
---


21 changes: 10 additions & 11 deletions docs/_api-mocking/cookies.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
---
title: "Setting cookies in the browser"
title: 'Setting cookies in the browser'
navTitle: cookies
position: 10
parentMethodGroup: mocking
content_markdown: |-
The `Set-Cookie` header is used to set cookies in the browser. This behaviour is part of the [browser/http spec](https://tools.ietf.org/html/rfc6265#section-4.1), not the fetch spec. As fetch-mock prevents requests getting out of js and into the browser, `Set-Cookie` will have no effect.
The `Set-Cookie` header is used to set cookies in the browser. This behaviour is part of the [browser/http spec](https://tools.ietf.org/html/rfc6265#section-4.1), not the fetch spec. As fetch-mock prevents requests getting out of js and into the browser, `Set-Cookie` will have no effect.
The following code samples demonstrate how to replicate the normal cookie setting behaviour when using fetch-mock.
The following code samples demonstrate how to replicate the normal cookie setting behaviour when using fetch-mock.
left_code_blocks:
- title: Set up
code_block: |-
fetchMock.get("https://mydomain.com", () => {
const cookieString = 'mycookie=hello; Max-Age=3600; Path=/;';
document.cookie = cookieString;
return { status: 200, headers: { 'Set-Cookie': cookieString }};
})
fetchMock.get("https://mydomain.com", () => {
const cookieString = 'mycookie=hello; Max-Age=3600; Path=/;';
document.cookie = cookieString;
return { status: 200, headers: { 'Set-Cookie': cookieString }};
})
language: javascript
- title: Tear down
code_block: |-
fetchMock.reset();
document.cookie = 'mycookie=; Max-Age=0'
fetchMock.reset();
document.cookie = 'mycookie=; Max-Age=0'
language: javascript

---
2 changes: 1 addition & 1 deletion docs/_api-mocking/getAnyOnce_postAnyOnce.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: ".getAnyOnce(), .postAnyOnce(), .putAnyOnce(), .deleteAnyOnce(), .headAnyOnce(), .patchAnyOnce()"
title: '.getAnyOnce(), .postAnyOnce(), .putAnyOnce(), .deleteAnyOnce(), .headAnyOnce(), .patchAnyOnce()'
navTitle: .getAnyOnce(), .postAnyOnce() ...
position: 7
description: |-
Expand Down
2 changes: 1 addition & 1 deletion docs/_api-mocking/getAny_postAny.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: ".getAny(), .postAny(), .putAny(), .deleteAny(), .headAny(), .patchAny()"
title: '.getAny(), .postAny(), .putAny(), .deleteAny(), .headAny(), .patchAny()'
navTitle: .getAny(), .postAny() ...
position: 5.5
description: |-
Expand Down
2 changes: 1 addition & 1 deletion docs/_api-mocking/getOnce_postOnce.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: ".getOnce(), .postOnce(), .putOnce(), .deleteOnce(), .headOnce(), .patchOnce()"
title: '.getOnce(), .postOnce(), .putOnce(), .deleteOnce(), .headOnce(), .patchOnce()'
navTitle: .getOnce(), .postOnce() ...
position: 4
description: |-
Expand Down
2 changes: 1 addition & 1 deletion docs/_api-mocking/get_post.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: ".get(), .post(), .put(), .delete(), .head(), .patch()"
title: '.get(), .post(), .put(), .delete(), .head(), .patch()'
navTitle: .get(), .post() ...
position: 2
description: |-
Expand Down
4 changes: 1 addition & 3 deletions docs/_api-mocking/mock.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: ".mock(matcher, response, options)"
title: '.mock(matcher, response, options)'
navTitle: .mock()
position: 1
description: |
Expand Down Expand Up @@ -120,5 +120,3 @@ left_code_blocks:
expect(res.status).to.equal(200)
})
---


2 changes: 1 addition & 1 deletion docs/_api-mocking/mock_any.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: ".any(response, options)"
title: '.any(response, options)'
navTitle: .any()
position: 5
description: |-
Expand Down
2 changes: 1 addition & 1 deletion docs/_api-mocking/mock_any_once.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: ".anyOnce(response, options)"
title: '.anyOnce(response, options)'
navTitle: .anyOnce()
position: 6
description: |-
Expand Down
8 changes: 3 additions & 5 deletions docs/_api-mocking/mock_matcher.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "matcher"
title: 'matcher'
position: 1.1
description: |-
Criteria for deciding which requests to mock.
Expand All @@ -17,7 +17,7 @@ parametersBlockTitle: Argument values
parentMethod: mock
parentMethodGroup: mocking
parameters:
- name: "*"
- name: '*'
types:
- String
content: Match any url
Expand Down Expand Up @@ -89,7 +89,7 @@ parameters:
Match if a function returns something truthy. The function will be passed the `url` and `options` `fetch` was called with. If `fetch` was called with a `Request` instance, it will be passed `url` and `options` inferred from the `Request` instance, with the original `Request` will be passed as a third argument.
This can also be set as a `functionMatcher` in the [options parameter](#api-mockingmock_options), and in this way powerful arbitrary matching criteria can be combined with the ease of the declarative matching rules above.
- types:
- types:
- Object
examples:
- |-
Expand Down Expand Up @@ -135,7 +135,6 @@ parameters:
Note that if matching on body _and_ using `Request` instances in your source code, this forces fetch-mock into an asynchronous flow _before_ it is able to route requests effectively. This means no [inspection methods](#api-inspectionfundamentals) can be used synchronously. You must first either await the fetches to resolve, or `await fetchMock.flush()`. The popular library [Ky](https://github.com/sindresorhus/ky) uses `Request` instances internally, and so also triggers this mode.
{: .warning}
examples:
- |-
{ "key1": "value1", "key2": "value2" }
Expand Down Expand Up @@ -182,4 +181,3 @@ content_markdown: |-
If multiple mocks use the same `matcher` but use different options, such as `headers`, you will need to use the `overwriteRoutes: false` option.
{: .warning}
---

2 changes: 1 addition & 1 deletion docs/_api-mocking/mock_once.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: ".once()"
title: '.once()'
position: 3
description: |-
Shorthand for `mock()` which creates a route that can only mock a single request. (see `repeat` option above)
Expand Down
17 changes: 8 additions & 9 deletions docs/_api-mocking/mock_response.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "response"
title: 'response'
position: 1.2
description: |-
Configures the http response returned by the mock. Accepts any of the following values or a `Promise` for any of them (useful when testing race conditions, loading transitions etc.). Unless otherwise stated, all responses have a `200` status
Expand Down Expand Up @@ -74,23 +74,22 @@ parameters:
examples:
- "new TypeError('Failed to fetch')"
- types:
- Object
- ArrayBuffer
- ...
- Object
- ArrayBuffer
- ...
content: |-
If the `sendAsJson` option is set to `true`, any object that does not meet the criteria above will be converted to a `JSON` string and set as the response `body`. Otherwise, the object will be set as the response `body` (useful for `ArrayBuffer`s etc.)
- types:
- Promise
- Promise
content: |-
A `Promise` that resolves to any of the options documented above
examples:
- "new Promise(res => setTimeout(() => res(200), 50))"
- 'new Promise(res => setTimeout(() => res(200), 50))'
- types:
- Function
- Function
content: |-
A function that returns any of the options documented above. The function will be passed the `url` and `options` `fetch` was called with. If `fetch` was called with a `Request` instance, it will be passed `url` and `options` inferred from the `Request` instance, with the original `Request` will be passed as a third argument.
examples:
- "(url, opts) => opts.headers.Authorization ? 200 : 403"
- '(url, opts) => opts.headers.Authorization ? 200 : 403'
- "(_, _, request) => request.headers.get('Authorization') ? 200 : 403"

---
6 changes: 3 additions & 3 deletions docs/_api-mocking/spy.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---
title: ".spy()"
title: '.spy(matcher)'
navTitle: .spy()
position: 9
description: |-
Records call history while passing each call on to `fetch` to be handled by the network
Records call history while passing each call on to `fetch` to be handled by the network. Optionally pass in a `matcher` to scope this to only matched calls, e.g. to fetch a specific resource from the network.
parentMethodGroup: mocking
content_markdown: |-
To use `.spy()` on a sandboxed `fetchMock`, `fetchMock.config.fetch` must be set to the same `fetch` implementation used in your application. [See how to configure this](#usagecustom-classes)
To use `.spy()` on a sandboxed `fetchMock`, `fetchMock.config.fetch` must be set to the same `fetch` implementation used in your application. [See how to configure this](#usagecustom-classes). By default this will be the locally installed version of `node-fetch`
---
8 changes: 2 additions & 6 deletions docs/_troubleshooting/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,18 @@ content_markdown: |-
request class. This needs to be done if you are mocking the `Request` object
for a test or you are running npm with a version below 3.
- use `fetchMock.config.Request = myRequest`, where `myRequest` is a reference to the Request constructor used in your application code.
---


it.md

- When using karma-webpack it's best not to use the `webpack.ProvidePlugin` for this. Instead just add `node_modules/whatwg-fetch/fetch.js` to your list of files to include, or require it directly into your tests before requiring fetch-mock.

- chaining

- note that end matches qs, path matches only path


Put this with the spy() docs
When using `node-fetch`, `fetch-mock` will use the instance you have installed. The one exception is that a reference to `fetchMock.config.fetch = require('node-fetch')` is required if you intend to use the `.spy()` method)
{: .info}


to Within individual tests `.catch()` and `spy()` can be used for fine-grained control of this"
to Within individual tests `.catch()` and `spy()` can be used for fine-grained control of this"

0 comments on commit 2a9066c

Please sign in to comment.