Skip to content

Commit

Permalink
📼 Make it work
Browse files Browse the repository at this point in the history
  • Loading branch information
exogen committed Nov 10, 2017
1 parent a66ba78 commit 8ebf2d3
Show file tree
Hide file tree
Showing 12 changed files with 4,663 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/lib
17 changes: 17 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
extends: ["standard", "prettier", "prettier/standard"],
env: {
es6: true,
node: true
},
plugins: ["prettier"],
rules: {
"prettier/prettier": [
"warn",
{
singleQuote: true,
semi: false
}
]
}
};
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,5 @@ typings/
# dotenv environment variables file
.env

# Babel output
lib/
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/lib
/.nyc_output
/fixtures
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# ava-vcr

## Modes

| VCR_MODE | Network requests? | Read fixtures? | Write fixtures? |
| :------: | :---------------------: | :---------------------: | :---------------------: |
| live | :ballot_box_with_check: | | |
| preview | :ballot_box_with_check: | :ballot_box_with_check: | |
| record | :ballot_box_with_check: | | :ballot_box_with_check: |
| cache | :ballot_box_with_check: | :ballot_box_with_check: | :ballot_box_with_check: |
| play | | :ballot_box_with_check: | |

* **live** will disable replay and recording completely. All requests will hit
the network like normal.
* **preview** will replay existing fixtures and send any other requests over the
network without recording them. This is useful if you are writing new tests
and want to make sure they pass before recording them.
* **record** will ignore existing fixtures and record new ones. All requests
will hit the network and be recorded. Note that this might leave you with
fixtures that are no longer used and should be cleaned up.
* **cache** will replay existing fixtures and record any other requests from the
network. This is useful if you have written new tests and verified that they
are correct and ready to be recorded.
* **play** will replay existing fixtures and never hit the network. Any requests
that do not have a fixture will result in an error. This is the default
behavior. It is useful if you are done writing tests and want to verify that
they all pass (like you’d do in CI, a pre-commit hook, etc.).

## Configuration

### `fixtureDir`

```ts
string | ({
protocol: string,
method: string,
host: string,
path: string,
headers: {},
body: string
}) => string
```

### `requestKey`

### `requestFixture`
26 changes: 26 additions & 0 deletions fixtures/f57d63c28c99037c8082337a31b4a9415e9e0f0b.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"request": {
"method": "GET",
"host": "musicbrainz.org",
"path": "/ws/2/artist/5b11f4ce-a62d-471e-81fc-a69a8278c7da?fmt=json",
"body": ""
},
"response": {
"statusCode": 200,
"statusMessage": "OK",
"headers": {
"date": "Fri, 10 Nov 2017 07:29:25 GMT",
"content-type": "application/json; charset=utf-8",
"content-length": "621",
"connection": "close",
"vary": "Accept-Encoding",
"x-ratelimit-limit": "1200",
"x-ratelimit-remaining": "898",
"x-ratelimit-reset": "1510298966",
"server": "Plack::Handler::Starlet",
"etag": "\"95cb0c47b9bfb3f77075154922dc05ed\"",
"access-control-allow-origin": "*"
},
"body": "{\"begin_area\":{\"sort-name\":\"Aberdeen\",\"disambiguation\":\"\",\"id\":\"a640b45c-c173-49b1-8030-973603e895b5\",\"name\":\"Aberdeen\"},\"isnis\":[\"0000000123486830\"],\"gender\":null,\"type-id\":\"e431f5f6-b5d2-343d-8b36-72607fffb74b\",\"life-span\":{\"end\":\"1994-04-05\",\"begin\":\"1988-01\",\"ended\":true},\"disambiguation\":\"90s US grunge band\",\"gender-id\":null,\"area\":{\"sort-name\":\"United States\",\"disambiguation\":\"\",\"iso-3166-1-codes\":[\"US\"],\"name\":\"United States\",\"id\":\"489ce91b-6658-3307-9877-795b68554c98\"},\"country\":\"US\",\"name\":\"Nirvana\",\"id\":\"5b11f4ce-a62d-471e-81fc-a69a8278c7da\",\"end_area\":null,\"ipis\":[],\"type\":\"Group\",\"sort-name\":\"Nirvana\"}"
}
}
62 changes: 62 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "ava-vcr",
"version": "0.0.0",
"description": "",
"keywords": ["testing", "http", "mocking", "vcr", "record", "playback"],
"author": "Brian Beck <[email protected]>",
"license": "MIT",
"main": "index.js",
"scripts": {
"build": "npm run build:lib",
"build:lib": "babel --out-dir lib src",
"clean": "rimraf lib",
"format": "npm run format:js && npm run format:other",
"format:js": "npm run lint:fix || true",
"format:other": "prettier --write \"**/*.{json,md}\"",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"prepare": "npm run clean && npm run build",
"test": "npm run lint && npm run test:coverage",
"test:cache": "VCR_MODE=cache ava",
"test:coverage": "nyc ava",
"test:live": "VCR_MODE=live ava",
"test:only": "ava",
"test:play": "VCR_MODE=play ava",
"test:preview": "VCR_MODE=preview ava",
"test:record": "VCR_MODE=record ava"
},
"ava": {
"require": ["./src/index"],
"files": ["src/**/*.test.js", "test"]
},
"dependencies": {
"debug": "^3.1.0",
"fs-extra": "^4.0.2",
"json-parse-better-errors": "^1.0.1",
"nock": "^9.1.0",
"object-hash": "^1.2.0",
"replay": "^2.1.4",
"request": "^2.83.0",
"temp-write": "^3.3.0"
},
"devDependencies": {
"ava": "^0.23.0",
"axios": "^0.17.0",
"babel-cli": "^6.26.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-register": "^6.26.0",
"eslint": "^4.10.0",
"eslint-config-prettier": "^2.7.0",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-prettier": "^2.3.1",
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-standard": "^3.0.1",
"isomorphic-fetch": "^2.2.1",
"nyc": "^11.3.0",
"prettier": "^1.8.2",
"rimraf": "^2.6.2"
}
}
45 changes: 45 additions & 0 deletions src/filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function extendFilter(left, right) {
if (right == null) {
return left
}
if (typeof right === 'object') {
return Object.keys(right).reduce((output, key) => {
const value = right[key]
if (value != null) {
output[key] = value
}
return output
}, Object.assign({}, left))
}
return right
}

function applyFilter(value, filter) {
if (filter === true || filter == null) {
return value
}
if (filter === false) {
return undefined
}
if (typeof filter === 'function') {
return filter(value)
}
let output = value
if (value != null && typeof value === 'object' && !Array.isArray(value)) {
output = {}
Object.keys(value).forEach(key => {
const keyFilter = filter[key]
const keyValue = value[key]
const outputValue = applyFilter(keyValue, keyFilter)
if (outputValue !== undefined) {
output[key] = outputValue
}
})
}
return output
}

module.exports = {
extendFilter,
applyFilter
}
45 changes: 45 additions & 0 deletions src/foo.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import test from 'ava'
import request from 'request'
import axios from 'axios'

test('using request', t => {
return new Promise((resolve, reject) => {
setTimeout(() => {
request(
{
url:
'https://musicbrainz.org/ws/2/artist/5b11f4ce-a62d-471e-81fc-a69a8278c7da?fmt=json',
headers: {
'User-Agent': 'ava-vcr/1.0 (+http://brianbeck.com/)'
}
},
(err, response, body) => {
if (err) {
reject(err)
} else {
resolve(JSON.parse(body))
}
}
)
}, 1000)
}).then(data => {
t.is(data.name, 'Nirvana')
})
})

test('using axios', t => {
return new Promise((resolve, reject) => {
setTimeout(resolve, 1000)
}).then(() =>
axios
.get(
'https://musicbrainz.org/ws/2/artist/5b11f4ce-a62d-471e-81fc-a69a8278c7da?fmt=json'
)
.then(response => {
return response.data
})
.then(data => {
t.is(data.name, 'Nirvana')
})
)
})
Loading

0 comments on commit 8ebf2d3

Please sign in to comment.