-
Notifications
You must be signed in to change notification settings - Fork 238
/
Copy pathcrud.spec.ts
55 lines (40 loc) · 1.47 KB
/
crud.spec.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
51
52
53
54
55
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import type { NestExpressApplication } from '@nestjs/platform-express';
import { Test } from '@nestjs/testing';
import supertest from 'supertest';
import { AppModule } from '../../src/app.module';
let app: NestExpressApplication | undefined;
let request: supertest.Agent;
let idx: number;
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleRef.createNestApplication<NestExpressApplication>();
await app.init();
request = supertest(app.getHttpServer());
});
test('POST: /test/crud', async () => {
const { status, body } = await request.post('/test/crud').send({ title: 'FooBar', content: 'Hello World', tags: ['new'] });
expect([200, 201]).toContain(status);
expect(body).toHaveProperty('id');
idx = body.id;
});
test('GET: /test/crud/:idx', async () => {
const { body } = await request.get(`/test/crud/${idx}`).expect(200);
expect(body).toHaveProperty('title', 'FooBar');
});
test('PUT: /test/crud/:idx', async () => {
const { body } = await request
.put(`/test/crud/${idx}`)
.send({ title: 'Blahblahblah', tags: ['update'] })
.expect(200);
expect(body).toHaveProperty('success', true);
});
test('DELETE: /test/crud/:idx', async () => {
const { body } = await request.delete(`/test/crud/${idx}`).expect(200);
expect(body).toHaveProperty('success', true);
});
afterAll(async () => {
await app?.close();
});