forked from pglez82/asw2223_0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.test.ts
50 lines (44 loc) · 1.47 KB
/
api.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import request, {Response} from 'supertest';
import express, { Application } from 'express';
import * as http from 'http';
import bp from 'body-parser';
import cors from 'cors';
import api from './api';
let app:Application;
let server:http.Server;
beforeAll(async () => {
app = express();
const port: number = 5000;
const options: cors.CorsOptions = {
origin: ['http://localhost:3000']
};
app.use(cors(options));
app.use(bp.json());
app.use("/api", api)
server = app.listen(port, ():void => {
console.log('Restapi server for testing listening on '+ port);
}).on("error",(error:Error)=>{
console.error('Error occured: ' + error.message);
});
});
afterAll(async () => {
server.close() //close the server
})
describe('user ', () => {
/**
* Test that we can list users without any error.
*/
it('can be listed',async () => {
const response:Response = await request(app).get("/api/users/list");
expect(response.statusCode).toBe(200);
});
/**
* Tests that a user can be created through the productService without throwing any errors.
*/
it('can be created correctly', async () => {
let username:string = 'Pablo'
let email:string = '[email protected]'
const response:Response = await request(app).post('/api/users/add').send({name: username,email: email}).set('Accept', 'application/json')
expect(response.statusCode).toBe(200);
});
});