|
| 1 | +import * as supertest from 'supertest'; |
| 2 | +import app from '../main'; |
| 3 | + |
| 4 | +const superTestApp = supertest(app); |
| 5 | + |
| 6 | +describe('Posts Endpoints', () => { |
| 7 | + it('GET /posts should return an array of posts', async () => { |
| 8 | + const res = await superTestApp.get('/posts?skip=0&take=10'); |
| 9 | + |
| 10 | + expect(res.status).toStrictEqual(200); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should transform skip and take into numbers', async () => { |
| 14 | + const res = await superTestApp.get('/posts?skip=0&take=10'); |
| 15 | + |
| 16 | + expect(res.status).toStrictEqual(200); |
| 17 | + expect(res.body.skip).toStrictEqual(0); |
| 18 | + expect(res.body.take).toStrictEqual(10); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should error if a required query param is missing', async () => { |
| 22 | + const res = await superTestApp.get('/posts?skip=0'); |
| 23 | + |
| 24 | + expect(res.status).toStrictEqual(400); |
| 25 | + expect(res.body).toStrictEqual({ |
| 26 | + issues: [ |
| 27 | + { |
| 28 | + code: 'invalid_type', |
| 29 | + expected: 'string', |
| 30 | + message: 'Required', |
| 31 | + path: ['take'], |
| 32 | + received: 'undefined', |
| 33 | + }, |
| 34 | + ], |
| 35 | + name: 'ZodError', |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should error if body is incorrect', async () => { |
| 40 | + const res = await superTestApp.post('/posts').send({ |
| 41 | + title: 'Good title', |
| 42 | + content: 123, |
| 43 | + }); |
| 44 | + |
| 45 | + expect(res.status).toStrictEqual(400); |
| 46 | + expect(res.body).toStrictEqual({ |
| 47 | + issues: [ |
| 48 | + { |
| 49 | + code: 'invalid_type', |
| 50 | + expected: 'string', |
| 51 | + message: 'Expected string, received number', |
| 52 | + path: ['content'], |
| 53 | + received: 'number', |
| 54 | + }, |
| 55 | + ], |
| 56 | + name: 'ZodError', |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should transform body correctly', async () => { |
| 61 | + const res = await superTestApp.post('/posts').send({ |
| 62 | + title: 'Title with extra spaces ', |
| 63 | + content: 'content', |
| 64 | + }); |
| 65 | + |
| 66 | + expect(res.status).toStrictEqual(201); |
| 67 | + expect(res.body.title).toStrictEqual('Title with extra spaces'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should format params using pathParams correctly', async () => { |
| 71 | + const res = await superTestApp.get('/test/123/name'); |
| 72 | + |
| 73 | + expect(res.status).toStrictEqual(200); |
| 74 | + expect(res.body).toStrictEqual({ |
| 75 | + id: 123, |
| 76 | + name: 'name', |
| 77 | + }); |
| 78 | + }); |
| 79 | +}); |
0 commit comments