Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V8 #457

Merged
merged 37 commits into from
Nov 12, 2019
Merged

V8 #457

Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
692da2a
Use transform runtime
evan-10e May 22, 2019
3069656
attempt at supporting ES modules
wheresrhys Oct 26, 2019
343574f
pull from master
wheresrhys Oct 26, 2019
723d557
linting and fixes
wheresrhys Oct 26, 2019
f3c06f6
tidy ignore files
wheresrhys Oct 26, 2019
da88dd3
fix build script
wheresrhys Oct 26, 2019
4eddade
fix build script again
wheresrhys Oct 26, 2019
f74e1d6
move webpack to dev dependencies
wheresrhys Oct 26, 2019
8e1eae0
only require whatwg-url on server #perfmatters
wheresrhys Oct 26, 2019
0bf8454
point browser at esm
wheresrhys Oct 26, 2019
92ef8c1
point at correct file name
wheresrhys Oct 26, 2019
3438bb7
correct server module export
wheresrhys Oct 26, 2019
fdbda19
tweak build script
wheresrhys Oct 27, 2019
0f0137c
try again to fix prepublish cjs script
wheresrhys Oct 27, 2019
5158078
I think I've done a types??
wheresrhys Oct 27, 2019
62847f8
local typescript
wheresrhys Oct 27, 2019
d3d4f85
fix docs
wheresrhys Oct 27, 2019
20431c3
add body option to type defs
wheresrhys Oct 27, 2019
2a4b9c9
stop using localTs - I can see why not now. Want max compat
wheresrhys Oct 27, 2019
c044450
Merge branch 'master' of https://github.com/wheresrhys/fetch-mock int…
wheresrhys Oct 27, 2019
4a4843a
Merge branch 'master' of https://github.com/wheresrhys/fetch-mock int…
wheresrhys Oct 27, 2019
ac3b928
add new mock() without arguments behaviour to types
wheresrhys Oct 27, 2019
8270ade
Merge branch 'master' of https://github.com/wheresrhys/fetch-mock int…
wheresrhys Oct 27, 2019
61cade8
Merge branch 'master' of https://github.com/wheresrhys/fetch-mock int…
wheresrhys Oct 27, 2019
2257739
updated types to support delay option
wheresrhys Oct 27, 2019
d8f4dad
Merge branch 'v8' into types
wheresrhys Oct 27, 2019
42d7156
Merge pull request #459 from wheresrhys/types
wheresrhys Oct 27, 2019
3841a05
Merge branch 'master' of https://github.com/wheresrhys/fetch-mock int…
wheresrhys Oct 27, 2019
ef28836
remove MATCHED and UNMATCHED constants
wheresrhys Oct 27, 2019
ca4750c
Merge pull request #466 from wheresrhys/unmatched
wheresrhys Oct 27, 2019
2e4c47d
install querystring dep
wheresrhys Nov 2, 2019
a5566a4
Merge branch 'master' of https://github.com/wheresrhys/fetch-mock int…
wheresrhys Nov 2, 2019
db5a6c8
Merge branch 'v8' of https://github.com/wheresrhys/fetch-mock into v8
wheresrhys Nov 2, 2019
db595de
lint
wheresrhys Nov 2, 2019
0643c23
upgrade dtslint
wheresrhys Nov 2, 2019
31ae8f0
prefer not to use built ins in client bundles
wheresrhys Nov 2, 2019
5c06377
really fix
wheresrhys Nov 2, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
/es5/
/node_modules/

# tooling artefacts
/docs/.sass-cache/
.eslintcache
npm-debug.log
/dist/

# test artifacts
coverage/
/test/fixtures/built-sw.js
.nyc_output
.eslintcache

# built files
/es5
/esm
/cjs
/docs/_site/
/docs/.sass-cache/
19 changes: 10 additions & 9 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
/test/
/.circleci/
.eslintrc
ISSUE_TEMPLATE
karma.conf.js
Makefile
README.md
/docs/
/.nyc_output/
/coverage/
/docs/
/src/
/test/
.codeclimate.yml
.eslint*
.gitignore
.prettierrc
Gemfile
Gemfile.lock
.prettierrc
.codeclimate.yml
ISSUE_TEMPLATE
karma.conf.js
Makefile
package-lock.json
README.md
rollup.config.js
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ test-node6: transpile

lint-ci:
eslint --ignore-pattern test/fixtures/* src test
prettier *.md
dtslint --expectOnly types

lint:
eslint --cache --fix .
prettier --write *.md
dtslint --expectOnly types

coverage-report:
nyc --reporter=lcovonly --reporter=text mocha test/server.js
Expand All @@ -34,11 +37,10 @@ local-coverage:
transpile:
babel src --out-dir es5

bundle:
webpack --mode development \
--output-library fetchMock \
--entry ./es5/client.js \
--output-filename ./es5/client-bundle.js
build: transpile
if [ ! -d "cjs" ]; then mkdir cjs; fi
cp -r src/* cjs
rollup -c rollup.config.js

docs:
cd docs; jekyll serve build --watch
18 changes: 12 additions & 6 deletions docs/_api-inspection/fundamentals.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,29 @@ parameters:
Retrieve all calls made to `fetch`
- types:
- true
- '"matched"'
content: |-
Retrieve all calls to `fetch` matched by some route defined by `fetch-mock`. `fetchMock.MATCHED` is an alias for `true` and may be used to make tests more readable
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:
- |-
const {MATCHED, fetchMock} = require('fetch-mock');
...
fetchMock.calls(MATCHED)
- types:
- false
- '"unmatched"'
content: |-
Retrieves all calls to `fetch` not matched by a route (i.e. those handled by `catch()`, `spy()` or the `fallbackToNetwork` option). `fetchMock.UNMATCHED` is an alias for `false` and may be used to make tests more readable
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:
- |-
const {UNMATCHED, fetchMock} = require('fetch-mock');
...
fetchMock.calls(UNMATCHED)
- name: routeIdentifier
types:
- String|RegExp|function
- String
- RegExp
- function
content: |-
All routes have an identifier:
- If it's a [named route](#api-mockingmock_options), the identifier is the route's `name`
Expand All @@ -41,7 +45,9 @@ parameters:
All calls that were handled by the route with the given identifier will be retrieved
- name: matcher
types:
- String|RegExp|function
- String
- RegExp
- function
content: |-
Any matcher compatible with the [mocking api](#api-mockingmock_matcher) can be passed in to filter the calls arbitrarily. The matcher will be executed using exactly the same rules as the mocking api
- name: options
Expand Down Expand Up @@ -82,7 +88,7 @@ content_markdown: |-
...
// this will retrieve calls handled by either route
fm.called(/user\/biff/)
// this will retrieve only calls handeld by the second route
fm.called(/user\/biff/)
// this will retrieve only calls handled by the second route
fm.called(matcherRX)
```
---
40 changes: 40 additions & 0 deletions docs/_usage/importing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: Importing the correct version
position: 4
parentItem: installation
content_markdown: |-

The JS ecosystem is in a transitional period between module systems, and there are also a number of different build tools available, all with their own idosyncratic opinions about how JS should be compiled. The following detail may help debug any problems, and a few known workarounds are listed below.

#### Built files
In general `server` refers to the version of the source code designed for running in nodejs, whereas `client` refers to the version designed to run in the browser. As well as this distinction, fetch-mock builds several versions of itself:
- `/cjs` directory - this contains a copy of the source files (which are currently written as commonjs modules). They are copied here in order to prevent direct requires from `/src`, which could make migrating the src to ES modules troublesome. `client.js` and `server.js` are the entry points. The directory also contains a `package.json` file specifying that the directory contains commonjs modules.
- `/esm` directory - This contains builds of fetch-mock, exported as ES modules. `client.mjs` and `server.mjs` are the entry points. The bundling tool used is [rollup](https://rollupjs.org).
- `/es5` directory - This contains builds of fetch-mock which do not use any JS syntax not included in the [ES5 standard](https://es5.github.io/), i.e. excludes recent additions to the language. It contains 4 entry points:
- `client.js` and `server.js`, both of which are commonjs modules
- `client-legacy.js`, which is the same as `client.js`, but includes some babel polyfill bootstrapping to ease running it in older environments
- `client-bundle.js`, `client-legacy-bundle.js`, which are standalone [UMD](https://github.com/umdjs/umd) bundles of the es5 client code that can be included in the browser using an ordinary script tag. The bundling tool used is [rollup](https://rollupjs.org).

#### Importing the right file
The package.json file references a selection of the above built files:
```json
{
"main": "./cjs/server.js",
"browser": "./esm/client.js",
"module": "./esm/server.js",
}
```
These are intended to target the most common use cases at the moment:
- nodejs using commonjs
- nodejs using ES modules
- bundling tools such as webpack

In most cases, your environment & tooling will use the config in package.json to import the correct file when you `import` or `require` `fetch-mock` by its name only.

However, `import`/`require` will sometimes get it wrong. Below are a few scenarios where you may need to directly reference a different entry point.

- If your client-side code or tests do not use a loader that respects the `browser` field of `package.json` use `require('fetch-mock/es5/client')` or `import fetchMock from 'fetch-mock/esm/client'`.
- When not using any bundler in the browser, use one of the following as the src of a script tag: `node_modules/fetch-mock/es5/client-bundle.js`, `node_modules/fetch-mock/es5/client-legacy-bundle.js`. This loads fetch-mock into the `fetchMock` global variable.
- For Node.js 6 or lower use `require('fetch-mock/es5/server')`
---

18 changes: 7 additions & 11 deletions docs/_usage/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,16 @@ content_markdown: |-
npm install --save-dev fetch-mock
```

In most environments use one of the following in your test files
fetch-mock supports both [ES modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) and [commonjs](https://requirejs.org/docs/commonjs.html). The following should work in most environments. Check the [importing the correct version](#usageimporting) section of the docs if you experience problems.

## ES modules
```js
const fetchMock = require('fetch-mock');

// Exposes constants that will make tests more readable
const { fetchMock, MATCHED, UNMATCHED } = require('fetch-mock');
import fetchMock from 'fetch-mock';
```

Some exceptions include:

- If your client-side code or tests do not use a loader that respects the `browser` field of `package.json` use `require('fetch-mock/es5/client')`.
- If you need to use fetch-mock without commonjs, you can include the precompiled `node_modules/fetch-mock/es5/client-bundle.js` in a script tag. This loads fetch-mock into the `fetchMock` global variable.
- For server side tests running in Node.js 6 or lower use<br>
`require('fetch-mock/es5/server')`
## Commonjs
```js
const fetchMock = require('fetch-mock');
```
---

Loading