Skip to content

Commit 33ccb5e

Browse files
committed
use different env for test unit; new testunit "users/me"
and test unit and "users/me/logout"
1 parent 8f2d53d commit 33ccb5e

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"main": "index.js",
55
"scripts": {
66
"start": "env-cmd -f ./.env nodemon src/app.js",
7-
"test": "env-cmd -f ./.env jest --testTimeout=10000 --testEnvironment=node --coverage"
7+
"test": "env-cmd -f ./.env-testunit jest --testTimeout=10000 --testEnvironment=node --coverage"
88
},
99
"author": "Frank Atukunda",
1010
"license": "MIT",

src/app.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ const app = express()
88
app.use(express.json())
99
app.use(userRouter)
1010

11-
app.listen(port, () => {
12-
console.log(`Server running on port ${port}`)
13-
})
11+
if (process.env && process.env.NODE_ENV != 'test') {
12+
app.listen(port, () => {
13+
console.log(`Server running on port ${port}`)
14+
})
15+
}
16+
1417

1518
module.exports = app

tests/users.profile.test.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const request = require('supertest')
2+
const app = require('../src/app')
3+
4+
describe('GET /users/me', () => {
5+
let name = "jane doe";
6+
let email = Math.random().toString(36).substring(7) + "@mail.com";
7+
let pass = "pass123";
8+
let token = "";
9+
10+
beforeEach( async () => {
11+
const res = await request(app)
12+
.post('/users')
13+
.send({
14+
name:name,
15+
email: email,
16+
password: pass,
17+
})
18+
// problem with this check
19+
// expect(res.statusCode).toEqual(201)
20+
return true;
21+
});
22+
23+
afterAll(async () => {
24+
// still error : https://github.com/visionmedia/supertest/issues/520
25+
await app.close()
26+
});
27+
28+
it('should successfuly login', async () => {
29+
const res = await request(app)
30+
.post('/users/login')
31+
.send({
32+
email: email,
33+
password: pass,
34+
})
35+
token = res.body.token; // save the token!
36+
expect(res.statusCode).toEqual(200)
37+
expect(res.body).toHaveProperty('token')
38+
})
39+
40+
it('should successfuly get profile', async () => {
41+
const res = await request(app)
42+
.get('/users/me')
43+
.set('Authorization', `Bearer ${token}`)
44+
.send()
45+
expect(res.statusCode).toEqual(200)
46+
expect(res.body).toHaveProperty('name')
47+
})
48+
49+
it('should successfuly logout', async () => {
50+
const res = await request(app)
51+
.post('/users/me/logout')
52+
.set('Authorization', `Bearer ${token}`)
53+
.send()
54+
expect(res.statusCode).toEqual(200)
55+
})
56+
})

0 commit comments

Comments
 (0)