Skip to content

Commit

Permalink
V6 (#228)
Browse files Browse the repository at this point in the history
* try new node-fetch

* more prototypey implementation

* removed ability to pas Promise to sandbox

* set implementations on the config object

* set implementations on the config object

* fix remaining tests for config options

* remove configure and setImplementations methods

* fix order of things on server

* Override Matcher Feature

Why:
* To provide to ability to override the response for a given matcher

How:
* Added logic to check for duplicate matchers when adding them. If a
  duplicate is found, then delete the old one and add the new one.
* Added unit tests to verify overriding behavior for string, regex,
  and function matchers.
* Removed the test for falling-back to previous matcher because it
  directly conflicts with the overriding feature.
* Modified the route name creation to include the method so that
  GET and POST mocks don't register as the same matcher
* Refactored compile-route.js to provide two named exports

* change a few options

* times -> repeat

* more thoughts on fallback warnings

* deprecate ^

* fix sandbox docs

* fix a load of tests

* create lib directory

* a little file splitting

* more file splitting

* more file splitting

* simplify header matching

* refactor compile-route

* simplify url matching

* simplified route creation

* simplified route creation

* stuff

* simpler url matcher again

* revert

* tidy up and adding isMock

* introduce some async to fetchMock

* tidying of holding promises for flush

* implement warn on fallback

* sensible reorg of building a response

* response builder is a class

* tidied new response builder implementation

* some renaming in sandbox internals

* rename a few files

* a little reorganising of top level tests

* sketched out structure for tests

* some tests for methods

* allocated all tests to new test groups

* re-enabled tests for .mock(), .get() etc

* another little test tidyup

* testing behaviour when mocking global fetch

* seandbox tests and forking

* a few routing tests

* tidied up a few routing tests

* get rid of unecessary promise.all

* method matchig

* unmatched calls

* final few routing tests

* placeholder tests for route overriding

* changed call filtering behaviour

* fixed tests to use new inspection regime

* updated flush tests

* tweak behaviour when inspecting calls made using Request

* tests for repeat and done

* tests for generating responses

* fixing a few test niggles

* start work on custom implementations tests

* tests for using custom Promise, Headers etc...

* added placeholder tests for remaining options

* tests for fallbackToNetwork

* tests for fallbackToNetwork

* tests for fallbackToNetwork

* reinstated basic content length tests

* json tests, and json response sets content-type

* more json tests

* dryer promise/function race resolution

* cleaner response builder initialisation

* rename test files

* tests for includeContentLength

* fix some docs for options

* test for sendAsJson

* added failing tests for overwriting routes

* fix all tests to deal with overwriing behaviour

* setting overwrite behaviour globally

* more forgiving of routes that don't share a method

* failing test for whitelisting non-json response props

* whitelisting of body props implemented

* tidy header setting

* call ordering

* simpler .mock() implementation

* less nesting in router code

* tests for call order

* filter calls by exact url

* fix browser tests

* lint

* simplify compile-route function

* update node version

* drop phantomjs

* formatting

* docs

* beta 2

* fix bug matching Requests with a function

* 6.0.0-beta.3

* query string matching

* 6.0.0-beta.4

* documentation

* inspecting: filter by method

* update version in README.md

* 6.0.0-beta.5

* tweak badges

* 6.0.0-beta.6

* 6.0.0-beta.7

* a little more testing and documentation

* slight tidy of spy()

* simplify route overriding

* fix jobs

* fix node-fetch 1 tests

* no firefox tests

* Update config.yml

* only deploy on tags

* npmrc

* tighten tag regex

* enable build on tag

* extend properly

* more extension

* rename overrideRoutes to overwriteRoutes (#247)

* Update README.md
  • Loading branch information
wheresrhys authored Jan 18, 2018
1 parent a7995c4 commit f6b027d
Show file tree
Hide file tree
Showing 38 changed files with 2,825 additions and 2,414 deletions.
57 changes: 35 additions & 22 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
version: 2.0
jobs:
checkout_code:
references:
triggerable-by-tag: &triggerable-by-tag
# For a tag push unaffected by any filters, CircleCI skips the job
# https://circleci.com/docs/2.0/workflows/#git-tag-job-execution
filters:
tags:
only: /.*/
env: &env
docker:
- image: circleci/node:8.9.0
env-browsers: &env-browsers
docker:
- image: circleci/node:8.9.0-browsers

jobs:
checkout_code:
<<: *env
steps:
- checkout
- run: npm install
Expand All @@ -12,26 +25,23 @@ jobs:
- node_modules

lint:
docker:
- image: circleci/node:8.9.0
<<: *env
steps:
- checkout
- restore_cache:
key: dependency-cache-{{ checksum "package.json" }}
- run: make lint

nodefetch2:
docker:
- image: circleci/node:8.9.0
<<: *env
steps:
- checkout
- restore_cache:
key: dependency-cache-{{ checksum "package.json" }}
- run: make coverage-report

nodefetch1:
docker:
- image: circleci/node:8.9.0
<<: *env
steps:
- checkout
- restore_cache:
Expand All @@ -40,17 +50,15 @@ jobs:
- run: make test-unit

chrome:
docker:
- image: circleci/node:8.9.0-browsers
<<: *env-browsers
steps:
- checkout
- restore_cache:
key: dependency-cache-{{ checksum "package.json" }}
- run: make test-chrome

firefox:
docker:
- image: circleci/node:8.9.0-browsers
<<: *env-browsers
steps:
- checkout
- restore_cache:
Expand All @@ -59,44 +67,49 @@ jobs:
- run: make test-firefox

deploy:
docker:
- image: circleci/node:8.9.0
<<: *env
steps:
- checkout
- restore_cache:
key: dependency-cache-{{ checksum "package.json" }}
- run: 'echo "//registry.npmjs.org/:_authToken=${NPM_AUTH_TOKEN}" > ${HOME}/.npmrc'
- run: npm version --no-git-tag-version ${CIRCLE_TAG}
- run: npm publish --access public

workflows:
version: 2
checkout_and_test:
jobs:
- checkout_code
- checkout_code: *triggerable-by-tag
- nodefetch1:
<<: *triggerable-by-tag
requires:
- checkout_code
- nodefetch2:
<<: *triggerable-by-tag
requires:
- checkout_code
- chrome:
<<: *triggerable-by-tag
requires:
- checkout_code
- firefox:
requires:
- checkout_code
# firefox version too old for sw, so postpone cross browser tests till updated in circleci
# - firefox:
# requires:
# - checkout_code
- lint:
<<: *triggerable-by-tag
requires:
- checkout_code
- deploy:
requires:
- nodefetch1
- nodefetch2
- chrome
- firefox
# - firefox
- lint
filters:
branches:
ignore: /.*/
tags:
only: /v.*/


only: /^v?\d+\.\d+\.\d+(?:-beta\.\d+)?$/
10 changes: 4 additions & 6 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
"mocha": true,
"node": true
},
"ecmaFeatures": {
"modules": true,
"jsx": true
"parserOptions": {
"ecmaVersion": 2017
},
"rules": {
"no-unused-vars": 2,
"no-undef": 2,
"eqeqeq": 2,
"no-underscore-dangle": 0,
"guard-for-in": 2,
"guard-for-in": 0,
"no-extend-native": 2,
"wrap-iife": 2,
"new-cap": 2,
Expand All @@ -30,7 +29,6 @@
"no-console": 1
},
"globals": {
"fetch": true,
"requireText": true
"fetch": true
}
}
10 changes: 4 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/bower_components/
/test/fixtures/built-sw.js
/es5/
package-lock.json
/node_modules/
npm-debug.log
/es5/
/dist/
test/fixtures/built-sw.js
_site
.sass-cache
.jekyll-metadata
coverage/
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sudo: false
language: node_js
script: npm test
node_js:
- '8'
before_install:
- export CHROME_BIN=chromium-browser
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.PHONY: test

build-sw:
browserify test/fixtures/sw.js > test/fixtures/built-sw.js
./node_modules/.bin/browserify test/fixtures/sw.js > test/fixtures/built-sw.js

test-dev: build-sw
./node_modules/karma/bin/karma start
Expand All @@ -16,8 +16,11 @@ test-unit:
./node_modules/.bin/mocha test/server.js

lint:
./node_modules/.bin/eslint src test
./node_modules/.bin/eslint --ignore-pattern test/fixtures/* src test

coverage-report:
istanbul cover node_modules/.bin/_mocha --report=lcovonly 'test/server.js'
./node_modules/.bin/istanbul cover node_modules/.bin/_mocha --report=lcovonly 'test/server.js'
cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js

local-coverage:
./node_modules/.bin/istanbul cover node_modules/.bin/_mocha 'test/server.js'
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# fetch-mock
Mock http requests made using fetch

Mock http requests made using fetch
[Documentation](http://www.wheresrhys.co.uk/fetch-mock/)

[![npm version](https://badge.fury.io/js/fetch-mock.svg)](https://badge.fury.io/js/fetch-mock)
Try v6 beta - `npm install [email protected]`

[![npm version](https://badge.fury.io/js/fetch-mock.svg)](https://badge.fury.io/js/fetch-mock)
[![maintainability](https://api.codeclimate.com/v1/badges/7f8abbf54ec9f3d07df3/maintainability)](https://codeclimate.com/github/wheresrhys/fetch-mock/maintainability)
[![Build Status](https://travis-ci.org/wheresrhys/fetch-mock.svg?branch=master)](https://travis-ci.org/wheresrhys/fetch-mock)
[![CircleCI](https://circleci.com/gh/wheresrhys/fetch-mock.svg?style=svg)](https://circleci.com/gh/wheresrhys/fetch-mock)
[![Coverage Status](https://coveralls.io/repos/github/wheresrhys/fetch-mock/badge.svg?branch=rhys%2Fcoveralls)](https://coveralls.io/github/wheresrhys/fetch-mock?branch=rhys%2Fcoveralls)

[![bitHound Overall Score](https://www.bithound.io/github/wheresrhys/fetch-mock/badges/score.svg)](https://www.bithound.io/github/wheresrhys/fetch-mock)
Expand Down
8 changes: 4 additions & 4 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Replaces `fetch` with a stub which records its calls, grouped by route, and opti
* `glob:http://*.*` to match glob patterns
* `express:/user/:user` to match [express style paths](https://www.npmjs.com/package/path-to-regexp)
* `RegExp`: A regular expression to test the url against
* `Function(url, opts)`: A function (returning a Boolean) that is passed the url and opts `fetch()` is called with (or, if `fetch()` was called with one, the `Request` instance)
* `Function(url, opts)`: A function (returning a Boolean) that is passed the url and opts `fetch()` is called with (or, if `fetch()` was called with one, the `Request` instance)

* `response`: Configures the http response returned by the mock. Can take any of the following values (or be a `Promise` for any of them, enabling full control when testing race conditions etc.)
* `Response`: A `Response` instance - will be used unaltered
Expand Down Expand Up @@ -94,13 +94,13 @@ Chainable method that clears all data recorded for `fetch()`'s calls. *It will n
### Filtering
Most of the methods below accept two parameters, `(filter, method)`
- `filter` Enables filtering fetch calls for the most commonly use cases. It can be:
- the name of a route
- the name of a route
- The value of `matcher` or `matcher.toString()` for any unnamed route. You _can_ pass in the original regex or function as a matcher, but they will be converted to strings and used to look up values in fetch-mock's internal maps of calls, _not_ used as regexes or functions executed on teh url
- An exact url
- If `filter` is a string, and it does not match any routes, it is asumed the string is a url, and calls to `fetch` made with that url are returned
- `true` for matched calls only
- `false` for unmatched calls only
- `undefined` for all calls to fetch
- `method` A http method to filter by
- `method` A http method to filter by

#### `called(filter, method)`
Returns a Boolean indicating whether fetch was called and a route was matched. If `filter` is specified it only returns `true` if that particular route was matched.
Expand Down
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ As well as shorthand methods for the simplest use cases, it offers a flexible AP

## These docs are for the v6 beta

**Please try v6 beta -** `npm install [email protected].1`
**Please try v6 beta -** `npm install [email protected].6`

- [v5 docs](/fetch-mock/v5)
- [V5 - V6 upgrade guide](/fetch-mock/v5-v6-upgrade)
Expand All @@ -15,4 +15,4 @@ As well as shorthand methods for the simplest use cases, it offers a flexible AP
- [Installation and usage ](/fetch-mock/installation)
- [API documentation](/fetch-mock/api)
- [Troubleshooting](/fetch-mock/troubleshooting)
- [Examples](/fetch-mock/examples)
- [Examples](/fetch-mock/examples)
8 changes: 6 additions & 2 deletions docs/v5-v6-upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ Feedback gratefully received when in beta - please raise an issue :-)

To try it out:
```
<<<<<<< HEAD
npm install [email protected]
=======
npm install [email protected]
>>>>>>> a7995c45182455fa46f83d0a21ae057cb6a25c22
```
(Bear in mind that the docs may still include a few inaccuracies)

Expand All @@ -24,7 +28,7 @@ npm install [email protected]

## Routing
- `^` is no longer a valid way of matching the beginnig of strings; use `begin:` instead
- `overrideRoutes` option allows for existing routes in a mock to be overwritten. It's also possible to define multiple routes with 'the same' matcher. Default behaviour is to error
- `overwriteRoutes` option allows for existing routes in a mock to be overwritten. It's also possible to define multiple routes with 'the same' matcher. Default behaviour is to error

## Responses
- `Content-Length` header generated by default for all responses (can be configured to not do so globally or per response.)
Expand All @@ -38,4 +42,4 @@ npm install [email protected]
+ `true` for matched calls only
+ `false` for unmatched calls only
+ `undefined` includes all calls to `fetch` in order
- When `fetch` was last called with a `Request` object, `lastUrl()` and `lastOpts()` give easier access to the `url` and full `Request` object
- When `fetch` was last called with a `Request` object, `lastUrl()` and `lastOpts()` give easier access to the `url` and full `Request` object
3 changes: 1 addition & 2 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function(karma) {
debug: true,
transform: [
['babelify', {
'presets': ['es2015'],
'presets': ['es2017'],
'plugins': ['transform-object-assign']
}]
]
Expand All @@ -31,4 +31,3 @@ module.exports = function(karma) {
karma.set(configuration);

};

26 changes: 13 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "fetch-mock",
"version": "5.13.1",
"version": "6.0.0-beta.7",
"description": "Mock http requests made using fetch (or isomorphic-fetch)",
"main": "src/server.js",
"browser": "es5/client.js",
"scripts": {
"test": "make test",
"browserify": "browserify -s fetchMock es5/client.js > es5/client-browserified.js",
"prepublish": "babel src --out-dir es5 --plugins transform-object-assign --presets es2015 && npm run browserify"
"test": "make lint test",
"browserify": "browserify -s fetchMock es5/client.js > es5/client-bundle.js",
"prepublish": "babel src --out-dir es5 --plugins transform-object-assign --presets es2017 && npm run browserify"
},
"repository": {
"type": "git",
Expand All @@ -30,29 +30,29 @@
"homepage": "http://www.wheresrhys.co.uk/fetch-mock",
"dependencies": {
"glob-to-regexp": "^0.3.0",
"node-fetch": "^1.3.3",
"path-to-regexp": "^1.7.0"
},
"devDependencies": {
"babel": "^6.0.15",
"babel-cli": "^6.1.2",
"babel-plugin-transform-object-assign": "^6.8.0",
"babel-preset-es2015": "^6.1.2",
"babel-preset-es2017": "^6.1.2",
"babelify": "^7.3.0",
"bluebird": "^3.4.6",
"browserify": "^13.1.0",
"chai": "^2.3.0",
"chai": "^4.1.2",
"coveralls": "^2.11.16",
"eslint": "^1.10.3",
"istanbul": "^0.4.5",
"eslint": "^4.14.0",
"istanbul": "^1.1.0-alpha.1",
"karma": "^1.3.0",
"karma-browserify": "^5.1.0",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^2.0.0",
"karma-chrome-launcher": "^2.2.0",
"karma-mocha": "^1.2.0",
"mocha": "^2.2.4",
"mockery": "^1.4.0",
"sinon": "^1.17.0",
"mocha": "^4.0.1",
"node-fetch": "^2.0.0-alpha.9",
"sinon": "^4.1.3",
"sinon-chai": "^2.14.0",
"watchify": "^3.7.0",
"whatwg-fetch": "^0.10.1"
}
Expand Down
8 changes: 4 additions & 4 deletions src/client.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
'use strict';

const FetchMock = require('./fetch-mock');
const statusTextMap = require('./status-text');
const FetchMock = require('./lib/index');
const statusTextMap = require('./lib/status-text');
const theGlobal = typeof window !== 'undefined' ? window : self;

FetchMock.global = theGlobal;
FetchMock.statusTextMap = statusTextMap;

FetchMock.setImplementations({
FetchMock.config = Object.assign(FetchMock.config, {
Promise: theGlobal.Promise,
Request: theGlobal.Request,
Response: theGlobal.Response,
Headers: theGlobal.Headers
});

module.exports = new FetchMock()
module.exports = FetchMock.createInstance();
Loading

0 comments on commit f6b027d

Please sign in to comment.