Skip to content

Commit 79376a4

Browse files
committed
🧪 Add tests
1 parent b6b3231 commit 79376a4

9 files changed

+1807
-57
lines changed

.eslintignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/dist
2-
*.
2+
*.
3+
/tests

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
22
.env
33
dist
4-
.nyc_output
4+
.nyc_output
5+
.vscode

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"start": "node dist/server.js",
99
"lint": "eslint src",
1010
"build": "tsc",
11-
"dev": "yarn build && yarn start"
11+
"dev": "yarn build && yarn start",
12+
"test": "tap --no-coverage"
1213
},
1314
"author": "Nosesisaid",
1415
"license": "MIT",
@@ -19,9 +20,11 @@
1920
},
2021
"devDependencies": {
2122
"@types/node": "17.0.33",
23+
"@types/tap": "^15.0.7",
2224
"@typescript-eslint/eslint-plugin": "^5.23.0",
2325
"@typescript-eslint/parser": "^5.23.0",
2426
"eslint": "^8.15.0",
27+
"tap": "^16.2.0",
2528
"typescript": "^4.6.4"
2629
}
2730
}

src/App.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import fastify, { FastifyServerOptions } from 'fastify';
2+
import fastifyMongodb from '@fastify/mongodb';
3+
import './config/env';
4+
function build(opts:FastifyServerOptions={}) {
5+
const App = fastify(opts);
6+
App.register(fastifyMongodb, {
7+
url: process.env.MONGO_URL
8+
});
9+
10+
App.get('/', async (req, res) => {
11+
const fumos = await App.mongo.db?.collection('fumos').find({}).toArray();
12+
13+
res.status(200).send(fumos);
14+
});
15+
16+
App.get('/fumo/:id', async (req, res) => {
17+
18+
const id = new App.mongo.ObjectId((req as any).params.id);
19+
const fumo = await (await App.mongo.db?.collection('fumos'))?.findOne({_id: id});
20+
21+
res.status(200).send(fumo);
22+
});
23+
24+
App.get('/random', async (req,res)=> {
25+
const fumos = await App.mongo.db?.collection('fumos').find({}).toArray();
26+
if (!fumos) return res.status(400).send('No fumo :( (server error)');
27+
const fumo = fumos[Math.floor(Math.random() * fumos?.length)];
28+
29+
res.status(200).send(fumo);
30+
});
31+
32+
App.get('/fumos', async(req, res) => {
33+
const fumos = await App.mongo.db?.collection('fumos').find({}).toArray();
34+
35+
res.status(200).send(fumos);
36+
});
37+
return App;
38+
}
39+
export default build;

src/server.ts

+3-35
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,9 @@
1-
import fastify from 'fastify';
2-
import fastifyMongodb from '@fastify/mongodb';
31
import {PORT} from './config/env';
2+
import build from './App';
43

5-
const App = fastify();
6-
App.register(fastifyMongodb, {
7-
url: process.env.MONGO_URL
8-
});
4+
const server = build();
95

10-
App.get('/', async (req, res) => {
11-
const fumos = await App.mongo.db?.collection('fumos').find({}).toArray();
12-
13-
res.status(200).send(fumos);
14-
});
15-
16-
App.get('/fumo/:id', async (req, res) => {
17-
18-
const id = new App.mongo.ObjectId((req as any).params.id);
19-
const fumo = await (await App.mongo.db?.collection('fumos'))?.findOne({_id: id});
20-
21-
res.status(200).send(fumo);
22-
});
23-
24-
App.get('/random', async (req,res)=> {
25-
const fumos = await App.mongo.db?.collection('fumos').find({}).toArray();
26-
if (!fumos) return res.status(400).send('No fumo :( (server error)');
27-
const fumo = fumos[Math.floor(Math.random() * fumos?.length)];
28-
29-
res.status(200).send(fumo);
30-
});
31-
32-
App.get('/fumos', async(req, res) => {
33-
const fumos = await App.mongo.db?.collection('fumos').find({}).toArray();
34-
35-
res.status(200).send(fumos);
36-
});
37-
38-
App.listen(PORT, err => {
6+
server.listen(PORT, err => {
397
if (err) throw err;
408
console.log('Server listening on port ' + PORT);
419
});

tests/random.test.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const build = require("../dist/App.js").default;
2+
const {test} = require("tap");
3+
test('call `/random` route', async (t) => {
4+
t.plan(2)
5+
const app = build();
6+
const response = await app.inject({
7+
method: 'GET',
8+
url:'/random'
9+
});
10+
11+
t.teardown(() => app.close())
12+
13+
const content = JSON.parse(response.payload);
14+
t.equal(response.statusCode, 200, 'status code is 200');
15+
t.ok(typeof content === 'object', 'response is an object');
16+
17+
})

tests/root.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const build = require("../dist/App.js").default;
2+
const { test } = require("tap");
3+
4+
test('call `/` route', async (t) => {
5+
t.plan(2);
6+
7+
const app = build();
8+
const response = await app.inject({
9+
method: 'GET',
10+
url:'/'
11+
});
12+
13+
t.teardown(() => app.close());
14+
15+
t.equal(response.statusCode, 200, 'status code is 200');
16+
t.ok(Array.isArray(JSON.parse(response.payload)), 'response is an array');
17+
18+
})
19+

tests/specific.test.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const build = require("../dist/App.js").default;
2+
const {test} = require("tap");
3+
4+
test('call `/fumo/id` route', async (t) => {
5+
t.plan(3)
6+
const app = build();
7+
const id = '6128c5578caf0cf9a83f73e8';
8+
const response = await app.inject({
9+
method: 'GET',
10+
url:'/fumo/'+id
11+
});
12+
13+
t.teardown(() => app.close())
14+
15+
const content = JSON.parse(response.payload);
16+
t.equal(response.statusCode, 200, 'status code is 200');
17+
t.ok(typeof content === 'object', 'response is an object');
18+
t.ok(content._id === id, 'response has the correct id');
19+
20+
})

0 commit comments

Comments
 (0)