Skip to content

Commit 0d47a9b

Browse files
committed
feat: prevent polluting normal testing
1 parent 072f3cb commit 0d47a9b

File tree

9 files changed

+159
-31
lines changed

9 files changed

+159
-31
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
lib/
2-
bin/
2+
bin/
3+
examples/

examples/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.yaml

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
##
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const request = require('supertest');
2+
3+
const app = require('./index.js');
4+
5+
describe('api testing', () => {
6+
it('should able to find all', (done) => {
7+
request(app)
8+
.get('/projects')
9+
.set('x-api-key', 'outdoc-test')
10+
.expect(200)
11+
.end(function(err, res) {
12+
if (err) throw err;
13+
done();
14+
});
15+
});
16+
17+
it('should able to find one', (done) => {
18+
request(app)
19+
.get('/projects/uuid-1234')
20+
.expect(200)
21+
.end(function(err, res) {
22+
if (err) throw err;
23+
done();
24+
});
25+
});
26+
27+
it('should able to find one 401', (done) => {
28+
request(app)
29+
.get('/projects/401')
30+
.expect(401)
31+
.end(function(err, res) {
32+
if (err) throw err;
33+
done();
34+
});
35+
});
36+
37+
it('should able to find one 404', (done) => {
38+
request(app)
39+
.get('/projects/404')
40+
.expect(404)
41+
.end(function(err, res) {
42+
if (err) throw err;
43+
done();
44+
});
45+
});
46+
47+
describe('users', () => {
48+
it('should able to find one user', (done) => {
49+
request(app)
50+
.get('/users/user-wayne')
51+
.expect(200)
52+
.end(function(err, res) {
53+
if (err) throw err;
54+
done();
55+
});
56+
});
57+
58+
it('should able to post', (done) => {
59+
request(app)
60+
.post('/users')
61+
.set('content-type', 'application/json')
62+
.send({
63+
name: 'user wayne'
64+
})
65+
.expect(201)
66+
.end(function(err, res) {
67+
if (err) throw err;
68+
done();
69+
});
70+
});
71+
});
72+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const request = require('supertest');
2+
3+
const app = require('./index.js');
4+
5+
describe('api testing', () => {
6+
it('should able to path', (done) => {
7+
request(app)
8+
.patch('/projects/uuid-1234')
9+
.set('Accept', 'application/json')
10+
.send({
11+
project: {
12+
tags: ['2016', '2017']
13+
}
14+
})
15+
.expect(200)
16+
.end(function(err, res) {
17+
if (err) throw err;
18+
done();
19+
});
20+
});
21+
22+
it('should able to post', (done) => {
23+
request(app)
24+
.post('/projects')
25+
.set('content-type', 'application/json')
26+
.set('x-api-key', 'wayne-test')
27+
.send({
28+
name: 'john'
29+
})
30+
.expect(201)
31+
.end(function(err, res) {
32+
if (err) throw err;
33+
done();
34+
});
35+
});
36+
37+
it('should able to delete', (done) => {
38+
request(app)
39+
.delete('/projects/uuid-2')
40+
.expect(204)
41+
.end(function(err, res) {
42+
if (err) throw err;
43+
done();
44+
});
45+
});
46+
});
Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const express = require('express')
2-
const app = express()
3-
const port = 3000
1+
const express = require('express');
2+
const app = express();
3+
const port = 3000;
44

55
if (process.env.NODE_ENV === "test") {
6-
const { OutDoc } = require('outdoc')
7-
OutDoc.init()
6+
const { OutDoc } = require('../../lib/index');
7+
OutDoc.init();
88
}
99

1010
app.get('/projects', (req, res) => {
@@ -14,67 +14,63 @@ app.get('/projects', (req, res) => {
1414
}, {
1515
id: '2',
1616
name: 'cominsoon'
17-
}])
18-
})
17+
}]);
18+
});
1919

2020
app.get('/projects/:id', (req, res) => {
21-
const { id } = req.params
21+
const { id } = req.params;
2222
if (id === "404") {
23-
return res.sendStatus(404)
23+
return res.sendStatus(404);
2424
}
2525
if (id === "401") {
2626
return res.status(401).json({
2727
error: {
2828
code: '123',
2929
message: '401 unauth'
3030
}
31-
})
31+
});
3232
}
3333
return res.json({
3434
id: '2',
3535
name: 'cominsoon'
36-
})
37-
})
36+
});
37+
});
3838

3939
app.patch('/projects/:id', (req, res) => {
4040
res.json([{
4141
id: '2',
4242
name: 'cominsoon'
43-
}])
44-
})
43+
}]);
44+
});
4545

4646
app.post('/projects', (req, res) => {
4747
res.status(201).json([{
4848
id: '1',
4949
name: 'dapi v1'
50-
}])
51-
})
50+
}]);
51+
});
5252

5353
app.delete('/projects/:id', (req, res) => {
54-
res.sendStatus(204)
55-
})
54+
res.sendStatus(204);
55+
});
5656

5757
app.get('/users/:id', (req, res) => {
58-
const { id } = req.params
58+
const { id } = req.params;
5959
return res.json({
6060
id: '1',
6161
name: 'wayne user'
62-
})
63-
})
62+
});
63+
});
6464

6565
app.post('/users', (req, res) => {
6666
res.status(201).json([{
6767
id: '1',
6868
name: 'user v1'
69-
}])
70-
})
71-
72-
// app.listen(port, () => {
73-
// console.log(`Example app listening on port ${port}`)
74-
// })
69+
}]);
70+
});
7571

7672
if (process.env.NODE_ENV !== 'test') {
77-
app.listen(port, () => console.log(`Listening on port ${port}`))
73+
app.listen(port, () => console.log(`Listening on port ${port}`));
7874
}
7975

8076
module.exports = app;

examples/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
"version": "1.0.0",
44
"description": "examples for outdoc",
55
"main": "index.js",
6-
"scripts": {},
6+
"scripts": {
7+
"express-mocha:test": "mocha -b --timeout 10000 --reporter spec --exit express+mocha/*.test.js",
8+
"express-mocha": "outdoc npm run express-mocha:test"
9+
},
710
"dependencies": {
811
"express": "^4.18.1"
912
},

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ type serverResType = {
3333

3434
export class OutDoc {
3535
public static init (): void {
36+
// Prevent pollute running normal testing
37+
if (process.env.IS_OUTDOC !== 'true') return
38+
3639
const asyncHook = async_hooks.createHook({
3740
init: (
3841
asyncId: number,

src/runner.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ export async function runner (
5252

5353
const childProcess = spawn(args[0], args.slice(1), {
5454
detached: true,
55-
stdio: ["inherit", "inherit", "pipe"]
55+
stdio: ["inherit", "inherit", "pipe"],
56+
env: {
57+
...process.env,
58+
NODE_ENV: 'test',
59+
IS_OUTDOC: 'true'
60+
}
5661
});
5762

5863
childProcess.stderr.on('data', (data) => {

0 commit comments

Comments
 (0)