fork of hey-api/openapi-ts because I need some additional things
1import type { INestApplication } from '@nestjs/common';
2import { Test } from '@nestjs/testing';
3import type { AddressInfo } from 'net';
4import { AppModule } from 'src/app.module';
5import { createPet, getInventory, listPets, showPetById } from 'src/client';
6import { client } from 'src/client/client.gen';
7
8let app: INestApplication;
9
10beforeAll(async () => {
11 const moduleRef = await Test.createTestingModule({
12 imports: [AppModule],
13 }).compile();
14
15 app = moduleRef.createNestApplication();
16 await app.init();
17 await app.listen(0);
18
19 const address = app.getHttpServer().address() as AddressInfo;
20 const baseUrl = `http://localhost:${String(address.port)}`;
21 client.setConfig({ baseUrl });
22});
23
24afterAll(async () => {
25 await app.close();
26});
27
28describe('PetsController', () => {
29 test('listPets', async () => {
30 const result = await listPets({ client });
31 expect(result.response.status).toBe(200);
32 expect(Array.isArray(result.data)).toBe(true);
33 });
34
35 test('showPetById', async () => {
36 const result = await showPetById({
37 client,
38 path: { petId: '1' },
39 });
40 expect(result.response.status).toBe(200);
41 });
42
43 test('createPet', async () => {
44 const result = await createPet({
45 body: { name: 'Buddy' },
46 client,
47 });
48 expect(result.response.status).toBe(201);
49 expect(result.data).toMatchObject({ name: 'Buddy' });
50 });
51});
52
53describe('StoreController', () => {
54 test('getInventory', async () => {
55 const result = await getInventory({ client });
56 expect(result.response.status).toBe(200);
57 });
58});