Skip to content

Commit e81a3a3

Browse files
author
Devendra
committed
trying nock for recording
1 parent dfe2f7a commit e81a3a3

File tree

3 files changed

+110
-45
lines changed

3 files changed

+110
-45
lines changed

node.js/tests/record.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var nock = require('nock');
2+
var path = require('path');
3+
var fs = require('fs');
4+
5+
module.exports = function (name, options) {
6+
// options tell us where to store our fixtures
7+
options = options || {};
8+
var test_folder = options.test_folder || 'test';
9+
var fixtures_folder = options.fixtures_folder || 'fixtures';
10+
var fp = path.join(test_folder, fixtures_folder, name + '.js');
11+
// `has_fixtures` indicates whether the test has fixtures we should read,
12+
// or doesn't, so we should record and save them.
13+
var has_fixtures = !!process.env.NOCK_RECORD;
14+
15+
return {
16+
// starts recording, or ensure the fixtures exist
17+
before: function () {
18+
if (!has_fixtures) try {
19+
require('./' + fp);
20+
has_fixtures = true;
21+
} catch (e) {
22+
console.log(JSON.stringify(e));
23+
nock.recorder.rec({
24+
dont_print: true
25+
});
26+
} else {
27+
has_fixtures = false;
28+
nock.recorder.rec({
29+
dont_print: true
30+
});
31+
}
32+
},
33+
// saves our recording if fixtures didn't already exist
34+
after: function (done) {
35+
if (!has_fixtures) {
36+
has_fixtures = nock.recorder.play();
37+
var text = "var nock = require('nock');\n" + has_fixtures.join('\n');
38+
fs.writeFile(fp, text, done);
39+
} else {
40+
done();
41+
}
42+
}
43+
}
44+
};

node.js/tests/test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
var nock = require('nock');
2+
3+
nock.recorder.rec({
4+
dont_print: true
5+
});
6+
7+
8+
19
var pubnub = PUBNUB.init({
210
publish_key : 'ds',
311
subscribe_key : 'ds',

0 commit comments

Comments
 (0)