Skip to content

Commit 70fe3b4

Browse files
committed
feat: add express_mocha_ts example
1 parent ccf9735 commit 70fe3b4

File tree

6 files changed

+668
-9
lines changed

6 files changed

+668
-9
lines changed

examples/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.yaml
2+
*.json
23
node_modules/
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { agent as request } from "supertest";
2+
3+
import app from './src/index'
4+
5+
describe('api testing', () => {
6+
it('should able to find all', (done) => {
7+
request(app)
8+
.get('/api/projects')
9+
.set('x-api-key', 'wayne-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('/api/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('/api/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('/api/projects/404')
40+
.expect(404)
41+
.end(function(err, res) {
42+
if (err) throw err;
43+
done()
44+
});
45+
})
46+
})
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { agent as request } from "supertest";
2+
3+
import app from './src/index'
4+
5+
describe('api testing', () => {
6+
it('should able to path', (done) => {
7+
request(app)
8+
.patch('/api/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('/api/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('/api/projects/uuid-2')
40+
.expect(204)
41+
.end(function(err, res) {
42+
if (err) throw err;
43+
done()
44+
});
45+
})
46+
})
47+
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import express, { Express, Request, Response } from 'express';
2+
3+
const app: Express = express();
4+
const port = 3000;
5+
6+
app.get('/api/projects', (req: Request, res: Response) => {
7+
res.json([{
8+
id: '1',
9+
name: 'dapi v1'
10+
}, {
11+
id: '2',
12+
name: 'cominsoon'
13+
}])
14+
})
15+
16+
app.get('/api/projects/:id', (req: Request, res: Response) => {
17+
const { id } = req.params
18+
if (id === "404") {
19+
return res.sendStatus(404)
20+
}
21+
if (id === "401") {
22+
return res.status(401).json({
23+
error: {
24+
code: '123',
25+
message: '401 unauth'
26+
}
27+
})
28+
}
29+
return res.json({
30+
id: '2',
31+
name: 'cominsoon'
32+
})
33+
})
34+
35+
app.patch('/api/projects/:id', (req: Request, res: Response) => {
36+
res.json([{
37+
id: '2',
38+
name: 'cominsoon'
39+
}])
40+
})
41+
42+
app.post('/api/projects', (req: Request, res: Response) => {
43+
res.status(201).json([{
44+
id: '1',
45+
name: 'dapi v1'
46+
}])
47+
})
48+
49+
app.delete('/api/projects/:id', (req: Request, res: Response) => {
50+
res.sendStatus(204)
51+
})
52+
53+
if (process.env.NODE_ENV !== 'test') {
54+
app.listen(port, () => {
55+
console.log(`⚡️[server]: Server is running at https://localhost:${port}`);
56+
});
57+
}
58+
59+
export default app

0 commit comments

Comments
 (0)