|
| 1 | +import { INestApplication } from '@nestjs/common'; |
| 2 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 3 | +import * as pactum from 'pactum'; |
| 4 | +import { AppModule } from '../src/app.module'; |
| 5 | + |
| 6 | +const testCatName = 'Test Cat 1'; |
| 7 | +const testCatBreed = 'Test Breed 1'; |
| 8 | +const russianBlue = 'Russian Blue'; |
| 9 | +const maineCoon = 'Maine Coon'; |
| 10 | +const badRequest = 'Bad Request'; |
| 11 | + |
| 12 | +describe('AppController (e2e) {Pactum}', () => { |
| 13 | + let app: INestApplication; |
| 14 | + let url: string; |
| 15 | + beforeAll(async () => { |
| 16 | + const moduleFixture: TestingModule = await Test.createTestingModule({ |
| 17 | + imports: [AppModule], |
| 18 | + }).compile(); |
| 19 | + |
| 20 | + app = moduleFixture.createNestApplication(); |
| 21 | + await app.listen(0); |
| 22 | + url = await app.getUrl(); |
| 23 | + pactum.request.setBaseUrl(url.replace('[::1]', 'localhost')); |
| 24 | + }); |
| 25 | + |
| 26 | + afterAll(async () => { |
| 27 | + await app.close(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('/ (GET)', () => { |
| 31 | + return pactum.spec().get('/').expectStatus(200).expectBody('Hello World!'); |
| 32 | + }); |
| 33 | + describe('/cat/ GET', () => { |
| 34 | + it('should return an array of cats', () => { |
| 35 | + return pactum |
| 36 | + .spec() |
| 37 | + .get('/cat') |
| 38 | + .withHeaders('authorization', 'auth') |
| 39 | + .expectStatus(200) |
| 40 | + .expectBody({ |
| 41 | + data: [ |
| 42 | + { id: 1, name: 'Ventus', breed: russianBlue, age: 3 }, |
| 43 | + { id: 2, name: 'Terra', breed: 'Siberian', age: 6 }, |
| 44 | + { id: 3, name: 'Aqua', breed: maineCoon, age: 5 }, |
| 45 | + ], |
| 46 | + }); |
| 47 | + }); |
| 48 | + }); |
| 49 | + describe('/cat/:id GET', () => { |
| 50 | + it('should return a 400 for a bad id', () => { |
| 51 | + return pactum |
| 52 | + .spec() |
| 53 | + .get('/cat/badId') |
| 54 | + .withHeaders('authorization', 'auth') |
| 55 | + .expectStatus(400) |
| 56 | + .expectBody({ |
| 57 | + statusCode: 400, |
| 58 | + error: badRequest, |
| 59 | + message: 'Id parameter should be a number.', |
| 60 | + }); |
| 61 | + }); |
| 62 | + it('should return an acutal cat', () => { |
| 63 | + return pactum |
| 64 | + .spec() |
| 65 | + .get('/cat/2') |
| 66 | + .withHeaders('authorization', 'auth') |
| 67 | + .expectStatus(200) |
| 68 | + .expectBody({ |
| 69 | + data: { |
| 70 | + id: 2, |
| 71 | + name: 'Terra', |
| 72 | + breed: 'Siberian', |
| 73 | + age: 6, |
| 74 | + }, |
| 75 | + }); |
| 76 | + }); |
| 77 | + }); |
| 78 | + describe('cat/new POST', () => { |
| 79 | + it('should throw an error for a bad age', () => { |
| 80 | + return pactum |
| 81 | + .spec() |
| 82 | + .post('/cat/new') |
| 83 | + .withHeaders('authorization', 'auth') |
| 84 | + .withJson({ |
| 85 | + name: testCatName, |
| 86 | + breed: testCatBreed, |
| 87 | + }) |
| 88 | + .expectStatus(400) |
| 89 | + .expectBody({ |
| 90 | + statusCode: 400, |
| 91 | + error: badRequest, |
| 92 | + message: |
| 93 | + 'Incoming cat is not formatted correctly. Age must be a number.', |
| 94 | + }); |
| 95 | + }); |
| 96 | + it('should throw an error for a bad name', () => { |
| 97 | + return pactum |
| 98 | + .spec() |
| 99 | + .post('/cat/new') |
| 100 | + .withHeaders('authorization', 'auth') |
| 101 | + .withJson({ |
| 102 | + age: 5, |
| 103 | + breed: testCatBreed, |
| 104 | + }) |
| 105 | + .expectStatus(400) |
| 106 | + .expectBody({ |
| 107 | + statusCode: 400, |
| 108 | + error: badRequest, |
| 109 | + message: |
| 110 | + 'Incoming cat is not formatted correctly. Name must be a string.', |
| 111 | + }); |
| 112 | + }); |
| 113 | + it('should throw an error for a bad breed', () => { |
| 114 | + return pactum |
| 115 | + .spec() |
| 116 | + .post('/cat/new') |
| 117 | + .withHeaders('authorization', 'auth') |
| 118 | + .withJson({ |
| 119 | + age: 5, |
| 120 | + name: testCatName, |
| 121 | + }) |
| 122 | + .expectStatus(400) |
| 123 | + .expectBody({ |
| 124 | + statusCode: 400, |
| 125 | + error: badRequest, |
| 126 | + message: |
| 127 | + 'Incoming cat is not formatted correctly. Breed must be a string.', |
| 128 | + }); |
| 129 | + }); |
| 130 | + it('should return the new cat with id', () => { |
| 131 | + return pactum |
| 132 | + .spec() |
| 133 | + .post('/cat/new') |
| 134 | + .withHeaders('authorization', 'auth') |
| 135 | + .withJson({ |
| 136 | + age: 5, |
| 137 | + name: testCatName, |
| 138 | + breed: testCatBreed, |
| 139 | + }) |
| 140 | + .expectStatus(201) |
| 141 | + .expectBody({ |
| 142 | + data: { |
| 143 | + id: 4, |
| 144 | + age: 5, |
| 145 | + name: testCatName, |
| 146 | + breed: testCatBreed, |
| 147 | + }, |
| 148 | + }); |
| 149 | + }); |
| 150 | + }); |
| 151 | + describe('/cat/:id DELETE', () => { |
| 152 | + it('should return false for a not found id', () => { |
| 153 | + return pactum |
| 154 | + .spec() |
| 155 | + .delete('/cat/633') |
| 156 | + .withHeaders('authorization', 'auth') |
| 157 | + .expectStatus(200) |
| 158 | + .expectBody({ data: false }); |
| 159 | + }); |
| 160 | + it('should return true for a found id', () => { |
| 161 | + return pactum |
| 162 | + .spec() |
| 163 | + .delete('/cat/2') |
| 164 | + .withHeaders('authorization', 'auth') |
| 165 | + .expectStatus(200) |
| 166 | + .expectBody({ data: true }); |
| 167 | + }); |
| 168 | + }); |
| 169 | +}); |
0 commit comments