Skip to content

Commit 8f421ae

Browse files
committed
Add test to show how we can redefine mounting using express server
1 parent 06ef5d3 commit 8f421ae

File tree

9 files changed

+143
-56
lines changed

9 files changed

+143
-56
lines changed

.eslintrc.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
module.exports = {
22
"extends": "airbnb-base",
3+
"env": {
4+
node: true,
5+
mocha: true
6+
},
37
"rules": {
48
"max-len": "off",
59
"func-names": "off",

.nycrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"exclude": [
3+
"examples/**/*.js",
4+
"examples/**/*.json",
5+
"examples/**/*.xml",
6+
"test/mocks/**/*.js",
7+
"test/mocks/**/*.json",
8+
"test/mocks/**/*.xml",
9+
10+
"test/**/*.spec.js"
11+
]
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const express = require('../../node_modules/express');
2+
const apiMocker = require('../../index');
3+
4+
const app = express();
5+
6+
// default response
7+
app.use('/', apiMocker('states/base'));
8+
9+
// definite state, where default response can be changed
10+
app.use('/', apiMocker('states/my-own-state'));
11+
12+
app.listen(9090);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = function (req, res, next) {
2+
next();
3+
4+
try {
5+
res.send({
6+
profile: {
7+
first_name: 'Aaron',
8+
last_name: 'Pol'
9+
}
10+
});
11+
} catch (e) {
12+
//
13+
}
14+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"profile": {
3+
"first_name": "Bryan",
4+
"last_name": "Cranston"
5+
}
6+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "A connect middleware to serve a RESTful api by some json, xml or js files",
55
"main": "index.js",
66
"scripts": {
7-
"test": "nyc --reporter=html --reporter=text mocha --exit",
7+
"test": "nyc --reporter=html --reporter=text mocha --ui bdd ./test --exit",
88
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
99
"travis-deploy-once": "travis-deploy-once",
1010
"semantic-release": "semantic-release"

test/api-mocker.spec.js

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
var request = require('supertest');
2-
var express = require('express');
3-
var apiMocker = require('../index');
4-
var fs = require('fs');
5-
var app = express();
1+
const request = require('supertest');
2+
const express = require('express');
3+
const fs = require('fs');
4+
const apiMocker = require('..');
65

7-
var deleteFolderRecursive = function(path) {
6+
const app = express();
7+
8+
const deleteFolderRecursive = function (path) {
89
if (fs.existsSync(path)) {
9-
fs.readdirSync(path).forEach(function(file, index){
10-
var curPath = path + "/" + file;
10+
fs.readdirSync(path).forEach((file) => {
11+
const curPath = `${path}/${file}`;
1112
if (fs.lstatSync(curPath).isDirectory()) { // recurse
1213
deleteFolderRecursive(curPath);
1314
} else { // delete file
@@ -20,24 +21,24 @@ var deleteFolderRecursive = function(path) {
2021

2122
app.use('/api', apiMocker('test/mocks'));
2223
app.use('/v2', apiMocker({
23-
target: 'test/mocks',
24-
nextOnNotFound: true,
25-
verbose: true
24+
target: 'test/mocks',
25+
nextOnNotFound: true,
26+
verbose: true
2627
}));
27-
app.use('/v2', function (req, res) {
28-
res.json({
29-
message: 'Fallback'
30-
})
28+
app.use('/v2', (req, res) => {
29+
res.json({
30+
message: 'Fallback'
31+
});
3132
});
3233
app.use(apiMocker('/v3', 'test/mocks'));
3334
app.use(apiMocker('/v4', {
34-
target: 'test/mocks'
35+
target: 'test/mocks'
3536
}));
3637
app.use('/notdefined', apiMocker('notdefined'));
3738
app.use(apiMocker('/xml', {
3839
target: 'test/mocks',
3940
type: 'xml',
40-
verbose: function (msg) {
41+
verbose() {
4142
// sth with message
4243
}
4344
}));
@@ -46,8 +47,8 @@ app.use(apiMocker('/dyn', {
4647
type: 'auto'
4748
}));
4849

49-
describe('Simple configuration with baseUrl', function () {
50-
it('responds for simple GET request', function (done) {
50+
describe('Simple configuration with baseUrl', () => {
51+
it('responds for simple GET request', (done) => {
5152
request(app)
5253
.get('/api/users/1')
5354
.expect('Content-Type', /json/)
@@ -57,7 +58,7 @@ describe('Simple configuration with baseUrl', function () {
5758
}, done);
5859
});
5960

60-
it('responds for simple POST request', function (done) {
61+
it('responds for simple POST request', (done) => {
6162
request(app)
6263
.post('/api/users/1')
6364
.expect('Content-Type', /json/)
@@ -67,7 +68,7 @@ describe('Simple configuration with baseUrl', function () {
6768
}, done);
6869
});
6970

70-
it('custom response will not cache', function (done) {
71+
it('custom response will not cache', (done) => {
7172
fs.mkdirSync('./test/mocks/users/2');
7273
fs.writeFileSync('./test/mocks/users/2/GET.js', fs.readFileSync('./test/mocks/users/__user_id__/GET_example1.js'));
7374

@@ -83,15 +84,15 @@ describe('Simple configuration with baseUrl', function () {
8384
.post('/api/users/2')
8485
.expect({
8586
version: 2
86-
}, function () {
87+
}, () => {
8788
done();
8889
deleteFolderRecursive('./test/mocks/users/2');
8990
});
9091
});
9192
});
9293

93-
describe('nextOnNotFound setting', function () {
94-
it('returns correct response when mock is exits', function (done) {
94+
describe('nextOnNotFound setting', () => {
95+
it('returns correct response when mock is exits', (done) => {
9596
request(app)
9697
.get('/v2/users/1')
9798
.expect('Content-Type', /json/)
@@ -101,7 +102,7 @@ describe('nextOnNotFound setting', function () {
101102
}, done);
102103
});
103104

104-
it('returns fallback when mock is not exits', function (done) {
105+
it('returns fallback when mock is not exits', (done) => {
105106
request(app)
106107
.get('/v2/non-existing-resource')
107108
.expect('Content-Type', /json/)
@@ -112,8 +113,8 @@ describe('nextOnNotFound setting', function () {
112113
});
113114
});
114115

115-
describe('Simple configuration without baseUrl', function () {
116-
it('returns correct response', function (done) {
116+
describe('Simple configuration without baseUrl', () => {
117+
it('returns correct response', (done) => {
117118
request(app)
118119
.get('/v3/users/1')
119120
.expect('Content-Type', /json/)
@@ -124,8 +125,8 @@ describe('Simple configuration without baseUrl', function () {
124125
});
125126
});
126127

127-
describe('Configuration with object and without baseUrl', function () {
128-
it('returns correct response', function (done) {
128+
describe('Configuration with object and without baseUrl', () => {
129+
it('returns correct response', (done) => {
129130
request(app)
130131
.get('/v4/users/1')
131132
.expect('Content-Type', /json/)
@@ -136,14 +137,14 @@ describe('Configuration with object and without baseUrl', function () {
136137
});
137138
});
138139

139-
describe('Wildcard feature', function () {
140-
it('works properly when no mock exist for request', function (done) {
140+
describe('Wildcard feature', () => {
141+
it('works properly when no mock exist for request', (done) => {
141142
request(app)
142143
.get('/notdefined/products/1')
143144
.expect(404, done);
144145
});
145146

146-
it('wildcard mock works properly', function (done) {
147+
it('wildcard mock works properly', (done) => {
147148
request(app)
148149
.get('/api/users/2812391232')
149150
.expect(200)
@@ -154,7 +155,7 @@ describe('Wildcard feature', function () {
154155
}, done);
155156
});
156157

157-
it('wildcard mock works properly with nested resources', function (done) {
158+
it('wildcard mock works properly with nested resources', (done) => {
158159
request(app)
159160
.get('/api/users/1/nested')
160161
.expect(200)
@@ -164,13 +165,13 @@ describe('Wildcard feature', function () {
164165
}, done);
165166
});
166167

167-
it('wildcard json methods should work on any given method', function (done) {
168+
it('wildcard json methods should work on any given method', (done) => {
168169
request(app)
169170
.get('/api/users/1/any-json-request')
170171
.expect(200)
171172
.expect({
172173
method: 'ANY'
173-
}, function() {
174+
}, () => {
174175
request(app)
175176
.post('/api/users/1/any-json-request')
176177
.expect(200)
@@ -180,13 +181,13 @@ describe('Wildcard feature', function () {
180181
});
181182
});
182183

183-
it('wildcard js methods should work on any given method', function (done) {
184+
it('wildcard js methods should work on any given method', (done) => {
184185
request(app)
185186
.get('/api/users/1/any-js-request')
186187
.expect(200)
187188
.expect({
188189
anyMethod: 'GET'
189-
}, function() {
190+
}, () => {
190191
request(app)
191192
.post('/api/users/1/any-js-request')
192193
.expect(200)
@@ -198,59 +199,57 @@ describe('Wildcard feature', function () {
198199
});
199200

200201

201-
describe('Response type config', function () {
202-
it('works properly with xml responses', function (done) {
202+
describe('Response type config', () => {
203+
it('works properly with xml responses', (done) => {
203204
request(app)
204205
.get('/xml/users/1')
205206
.expect('Content-Type', /xml/)
206207
.expect(200, done);
207208
});
208209

209-
it('works properly with auto type (xml)', function (done) {
210+
it('works properly with auto type (xml)', (done) => {
210211
request(app)
211212
.get('/dyn/users/1')
212213
.set('Accept', 'application/xml')
213214
.expect('Content-Type', /xml/)
214215
.expect(200, done);
215216
});
216217

217-
it('works properly with auto type (json)', function (done) {
218+
it('works properly with auto type (json)', (done) => {
218219
request(app)
219220
.get('/dyn/users/1')
220221
.set('Accept', 'application/json')
221222
.expect('Content-Type', /json/)
222223
.expect(200, done);
223224
});
224225

225-
it('works properly with auto type (xml not found)', function (done) {
226+
it('works properly with auto type (xml not found)', (done) => {
226227
request(app)
227228
.post('/dyn/users/2')
228229
.set('Accept', 'application/xml')
229230
.expect(404, done);
230-
231231
});
232232
});
233233

234-
describe('Handling request body', function () {
235-
it('should work with request body json', function (done) {
234+
describe('Handling request body', () => {
235+
it('should work with request body json', (done) => {
236236
request(app)
237237
.post('/api/users')
238238
.set('Content-Type', 'application/json')
239-
.send({name: 'A name'})
239+
.send({ name: 'A name' })
240240
.expect(201)
241241
.expect({
242242
name: 'A name'
243-
}, done)
243+
}, done);
244244
});
245245

246-
it('shouldnt break to capability of reading raw request body', function (done) {
246+
it('shouldnt break to capability of reading raw request body', (done) => {
247247
request(app)
248248
.patch('/api/users')
249249
.send('A text content')
250250
.expect(200)
251251
.expect({
252252
requestString: 'A text content'
253253
}, done);
254-
})
255-
256-
})
254+
});
255+
});

test/examples.spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const request = require('supertest');
2+
const express = require('express');
3+
const apiMocker = require('..');
4+
5+
const app = express();
6+
7+
app.use('/api/v1', apiMocker('examples/redefine-default-mounting/states/base'));
8+
9+
describe('Redefine default mounting', () => {
10+
it('responds for simple GET request', (done) => {
11+
request(app)
12+
.get('/api/v1/profile')
13+
.expect('Content-Type', /json/)
14+
.expect(200)
15+
.expect({
16+
profile: {
17+
first_name: 'Aaron',
18+
last_name: 'Pol'
19+
}
20+
}, done);
21+
});
22+
23+
it('redefine GET response', (done) => {
24+
app.use('/api/v1', apiMocker('examples/redefine-default-mounting/states/my-own-state'));
25+
26+
request(app)
27+
.get('/api/v1/profile')
28+
.expect('Content-Type', /json/)
29+
.expect(200)
30+
.expect({
31+
profile: {
32+
first_name: 'Bryan',
33+
last_name: 'Cranston'
34+
}
35+
}, done);
36+
});
37+
});

wallaby.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@ module.exports = function w() {
44
'index.js',
55
{ pattern: 'test/mocks/**/*.js', instrument: false },
66
{ pattern: 'test/mocks/**/*.json', instrument: false },
7-
{ pattern: 'test/mocks/**/*.xml', instrument: false }
7+
{ pattern: 'test/mocks/**/*.xml', instrument: false },
8+
{ pattern: 'examples/**/*.js', instrument: false },
9+
{ pattern: 'examples/**/*.json', instrument: false },
10+
{ pattern: 'examples/**/*.xml', instrument: false }
811
],
912
tests: [
10-
'test/**/*.spec.js',
13+
'test/**/*.spec.js'
1114
],
1215
env: {
1316
type: 'node',
14-
runner: 'node',
17+
runner: 'node'
1518
},
1619

17-
testFramework: 'mocha',
20+
testFramework: 'mocha'
1821
};
1922
};

0 commit comments

Comments
 (0)