generated from softspiders/express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
48 lines (39 loc) · 1.55 KB
/
index.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
import { spawn, ChildProcess } from 'child_process';
import axios, { AxiosInstance } from 'axios';
import waitOn from 'wait-on';
jest.setTimeout(15000);
describe('express-ts example', () => {
let start: ChildProcess;
let client: AxiosInstance;
beforeAll(async () => {
client = axios.create({ baseURL: 'http://localhost:9000', validateStatus: () => true });
start = spawn('npm', ['start'], { cwd: __dirname, detached: true, stdio: 'inherit' });
await waitOn({ resources: ['tcp:localhost:9000'] });
});
afterAll(() => process.kill(-start.pid));
test('GET /pets returns 200 with mocked result', async () => {
const res = await client.get('/pets');
expect(res.status).toBe(200);
expect(res.data).toEqual([{ id: 1, name: 'Odie' }]);
});
test('GET /pets/1 returns 200 with mocked result', async () => {
const res = await client.get('/pets/1');
expect(res.status).toBe(200);
expect(res.data).toEqual({ id: 1, name: 'Garfield' });
});
test('POST /pets returns 201 with mocked result', async () => {
const res = await client.post('/pets', {});
expect(res.status).toBe(201);
expect(res.data).toEqual({ id: 1, name: 'Garfield' });
});
test('GET /pets/1a returns 400 with validation error', async () => {
const res = await client.get('/pets/1a');
expect(res.status).toBe(400);
expect(res.data).toHaveProperty('err');
});
test('GET /unknown returns 404', async () => {
const res = await client.get('/unknown');
expect(res.status).toBe(404);
expect(res.data).toHaveProperty('err');
});
});