Skip to content

Commit 578e9b5

Browse files
committed
Initial commit
0 parents  commit 578e9b5

File tree

11 files changed

+7600
-0
lines changed

11 files changed

+7600
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
pkg

.prettierignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
pkg
3+
4+
CHANGELOG.md
5+
README.md
6+
package.json

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js: 'node'
3+
before_install:
4+
- curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.16.0
5+
- export PATH="$HOME/.yarn/bin:$PATH"

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
## [0.1.0] - 2019-08-01
11+
12+
- Initial release

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Brent Mealhouse
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Axios Record Replay Adapter 🎥 🎬 🍿
2+
3+
> Sit back, relax, and automatically record/replay your HTTP requests
4+
5+
## Installation
6+
7+
```sh
8+
yarn add axios-record-replay-adapter --dev
9+
npm i axios-record-replay-adapter --save-dev
10+
```
11+
12+
## Usage
13+
14+
### Use `axios-record-replay-adapter` with default options
15+
16+
```js
17+
useAxiosRecordReplayAdapter()
18+
```
19+
20+
#### Default options
21+
22+
```js
23+
{
24+
axiosInstance: axios,
25+
recordingsDir: './recordings',
26+
createRequest(axiosRequestConfig) {
27+
return {
28+
method: requestConfig.method,
29+
url: new URL(requestConfig.url).pathname,
30+
data: requestConfig.data,
31+
}
32+
},
33+
createResponse(axiosResponse) {
34+
return {
35+
status: response.status,
36+
statusText: response.statusText,
37+
data: response.data,
38+
}
39+
}
40+
}
41+
```
42+
43+
### Use `axios-record-replay-adapter` with custom options
44+
45+
```js
46+
const customAxiosInstance = axios.create()
47+
useAxiosRecordReplayAdapter({
48+
axiosInstance: customAxiosInstance,
49+
recordingsDir: './tests/recordings',
50+
})
51+
```
52+
53+
### Ignore recordings directory in your test runner
54+
55+
> When running tests in watch mode, the recordings directory needs to be ignored to prevent recording files from triggering tests to re-run.
56+
57+
#### Jest example
58+
59+
```json
60+
{
61+
"jest": {
62+
"watchPathIgnorePatterns": [
63+
"<rootDir>/recordings"
64+
]
65+
}
66+
}
67+
```

package.json

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
{
2+
"name": "axios-record-replay-adapter",
3+
"version": "0.0.1",
4+
"description": "Sit back, relax, and automatically record/replay your HTTP requests",
5+
"main": "index.js",
6+
"repository": "https://github.com/bmealhouse/axios-record-replay-adapter",
7+
"author": "Brent Mealhouse <[email protected]>",
8+
"license": "MIT",
9+
"private": false,
10+
"@pika/pack": {
11+
"pipeline": [
12+
[
13+
"@pika/plugin-ts-standard-pkg"
14+
],
15+
[
16+
"@pika/plugin-build-node"
17+
]
18+
]
19+
},
20+
"husky": {
21+
"hooks": {
22+
"pre-commit": "lint-staged",
23+
"pre-push": "yarn test"
24+
}
25+
},
26+
"jest": {
27+
"preset": "ts-jest",
28+
"testEnvironment": "node",
29+
"watchPathIgnorePatterns": [
30+
"<rootDir>/recordings"
31+
]
32+
},
33+
"lint-staged": {
34+
"*.ts": [
35+
"xo",
36+
"prettier --write",
37+
"git add"
38+
]
39+
},
40+
"prettier": {
41+
"useTabs": false,
42+
"semi": false,
43+
"singleQuote": true,
44+
"trailingComma": "all",
45+
"bracketSpacing": false
46+
},
47+
"xo": {
48+
"envs": [
49+
"jest"
50+
],
51+
"prettier": true,
52+
"plugins": [
53+
"filenames"
54+
],
55+
"extends": "xo-typescript",
56+
"etensions": [
57+
"ts"
58+
],
59+
"rules": {
60+
"capitalized-comments": "off",
61+
"no-unused-vars": [
62+
"error",
63+
{
64+
"ignoreRestSiblings": true,
65+
"argsIgnorePattern": "^_",
66+
"varsIgnorePattern": "^_"
67+
}
68+
],
69+
"@typescript-eslint/indent": [
70+
"error",
71+
2
72+
],
73+
"filenames/match-exported": [
74+
"error",
75+
"kebab",
76+
"^__"
77+
]
78+
}
79+
},
80+
"devDependencies": {
81+
"@pika/pack": "0.4.0",
82+
"@pika/plugin-build-node": "0.5.1",
83+
"@pika/plugin-ts-standard-pkg": "0.5.1",
84+
"@types/jest": "24.0.16",
85+
"@typescript-eslint/eslint-plugin": "1.13.0",
86+
"@typescript-eslint/parser": "1.13.0",
87+
"axios": "0.19.0",
88+
"eslint-config-xo-typescript": "0.15.0",
89+
"eslint-plugin-filenames": "1.3.2",
90+
"husky": "3.0.2",
91+
"jest": "24.8.0",
92+
"lint-staged": "9.2.1",
93+
"ts-jest": "24.0.2",
94+
"typescript": "3.5.3",
95+
"xo": "0.24.0"
96+
},
97+
"peerDependencies": {
98+
"axios": "^0.19.0"
99+
},
100+
"scripts": {
101+
"build": "pack build",
102+
"test": "jest"
103+
}
104+
}

src/index.test.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import fs from 'fs'
2+
import axios from 'axios'
3+
import useAxiosRecordReplayAdapter from '.'
4+
5+
const format = (data: string): string => {
6+
return data.replace(/"/g, "'")
7+
}
8+
9+
afterAll(() => {
10+
const recordings = fs
11+
.readdirSync('./recordings')
12+
.map(filename => `./recordings/${filename}`)
13+
14+
recordings.forEach(fs.unlinkSync)
15+
fs.rmdirSync('./recordings')
16+
})
17+
18+
test('creates the default "./recordings" directory', () => {
19+
useAxiosRecordReplayAdapter({
20+
axiosInstance: axios.create(),
21+
})
22+
23+
expect(fs.existsSync('./recordings')).toBeTruthy()
24+
})
25+
26+
test('creates a custom "./src/recordings" directory', () => {
27+
const recordingsDir = './src/recordings'
28+
useAxiosRecordReplayAdapter({
29+
axiosInstance: axios.create(),
30+
recordingsDir,
31+
})
32+
33+
expect(fs.existsSync(recordingsDir)).toBeTruthy()
34+
fs.rmdirSync(recordingsDir)
35+
})
36+
37+
test('creates a recording when calling https://jsonplaceholder.typicode.com/todos/1', async () => {
38+
const axiosInstance = axios.create()
39+
useAxiosRecordReplayAdapter({axiosInstance})
40+
41+
const {data} = await axiosInstance.get(
42+
'https://jsonplaceholder.typicode.com/todos/1',
43+
)
44+
45+
const [recording] = fs.readdirSync('./recordings')
46+
expect(recording).toMatchInlineSnapshot(
47+
`"todos-1_3439973d2c1ff6cc118d7af4cf797551.json"`,
48+
)
49+
50+
const recordingContents = fs.readFileSync(`./recordings/${recording}`, 'utf8')
51+
expect(JSON.parse(recordingContents).response.data).toEqual(data)
52+
53+
expect(format(recordingContents)).toMatchInlineSnapshot(`
54+
"{
55+
'request': {
56+
'method': 'get',
57+
'path': '/todos/1'
58+
},
59+
'response': {
60+
'status': 200,
61+
'statusText': 'OK',
62+
'data': {
63+
'userId': 1,
64+
'id': 1,
65+
'title': 'delectus aut autem',
66+
'completed': false
67+
}
68+
}
69+
}"
70+
`)
71+
})

0 commit comments

Comments
 (0)