Skip to content

Commit 064434f

Browse files
committed
feat(*) generate http fixtures via nock
npm run generate-http-fixtures will generate a test/fixtures/http.json file with all the mocked requests (containing any infos for the stub - requests headers, response headers / body ...)
1 parent 5bb7f05 commit 064434f

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

bin/nock/call-nock.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* This script records the http requests (requests / response with body and headers)
3+
* so that they could be mocked for unit test.
4+
* This task is automated, this way, you don't have to bother to do it manually ! ;-)
5+
*
6+
* For the moment, I request the github API via topheman-apis-proxy (direct request gives encoded results, which might
7+
* be because of https or gzip which aren't managed yet by nock).
8+
*/
9+
10+
const nock = require('nock');
11+
const request = require('superagent');
12+
const fs = require('fs');
13+
const path = require('path');
14+
15+
const OUTPUT_PATH = './test/fixtures/http.json';
16+
17+
nock.recorder.rec({
18+
output_objects: true,
19+
enable_reqheaders_recording: true,
20+
dont_print: true
21+
});
22+
23+
const uris = [
24+
'/users/topheman',
25+
'/users/topheman/repos?page=1&per_page=15&sort=stars',
26+
'/users/topheman/repos?page=2&per_page=15&sort=stars',
27+
'/users/topheman/repos?page=3&per_page=15&sort=stars',
28+
'/users/topheman/followers',
29+
'/search/users?q=tophe'
30+
];
31+
32+
const promises = uris.map((uri) => {
33+
return request.get('http://localhost:8000/github' + uri)
34+
});
35+
36+
Promise.all(promises)
37+
.then(() => {
38+
const nockCallObjects = nock.recorder.play();
39+
if (nockCallObjects.length > 0) {
40+
// bellow some processing to cleanup the mock so that they could be correctly used (according to your use-case, it could differ a little)
41+
const output = nockCallObjects.map((item) => {
42+
item.scope = 'http://localhost'; // change the host name (avoid CORS), and protocol (superagent-mocker considers ":" as wildcard, so you can't put a port)
43+
item.path = item.path.replace(/^\/github/,''); // remove the leading "/github" (in topheman-apis-proxy), so that relative url will be used
44+
console.log(item.path);
45+
return item;
46+
});
47+
fs.writeFileSync(OUTPUT_PATH, JSON.stringify(output));
48+
console.log('Saved in ', OUTPUT_PATH);
49+
process.exit(0);
50+
}
51+
throw new Error('No results');
52+
})
53+
.catch(error => console.log('[ERROR]', error.message));

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"webpack": "./node_modules/.bin/webpack-dev-server --progress --colors --hot --inline",
2222
"webpack-dev": "API_ROOT_URL='http://localhost:8000/github' DEVTOOLS=true npm run webpack",
2323
"webpack-mock": "DEVTOOLS=true NODE_ENV=mock npm run webpack",
24-
"webpack-dev-simple": "DEVTOOLS=true npm run webpack"
24+
"webpack-dev-simple": "DEVTOOLS=true npm run webpack",
25+
"generate-http-fixtures": "node ./bin/nock/call-nock.js"
2526
},
2627
"pre-commit": [
2728
"test"
@@ -69,6 +70,7 @@
6970
"lodash": "^3.10.1",
7071
"mocha": "^2.3.4",
7172
"moment": "^2.10.6",
73+
"nock": "^3.3.2",
7274
"node-sass": "^3.3.3",
7375
"pre-commit": "^1.1.2",
7476
"react-addons-test-utils": "^0.14.3",

test/fixtures/http.json

+1
Large diffs are not rendered by default.

webpack.config.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,13 @@ var config = {
166166
{ test: /\.(png|woff|woff2|eot|ttf|svg)$/, loader: 'url-loader?limit=100000' }
167167
]
168168
},
169-
plugins: plugins
169+
plugins: plugins,
170+
node:{
171+
console: true,
172+
fs: 'empty',
173+
net: 'empty',
174+
tls: 'empty'
175+
}
170176
};
171177

172178
module.exports = config;

0 commit comments

Comments
 (0)