Skip to content

Commit ed831d8

Browse files
committed
chore: Fixed a couple of tests and moved some files
1 parent 46d43b2 commit ed831d8

11 files changed

+94
-58
lines changed

apps/nest/src/cats/cats.controller.test.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
import { MappingModule, Supertest, TestContainer } from '@spuxx/nest-utils';
2-
import { CatsModule } from './cats.module';
1+
import { Supertest, TestContainer } from '@spuxx/nest-utils';
32
import { cats } from './cats.data';
3+
import { AppModule } from '../app.module';
44

55
describe('CatsController', () => {
66
let supertest: Supertest;
77

88
beforeEach(async () => {
9+
vi.stubEnv('AUTH_CLIENT_SECRET', 'foo');
910
const container = await TestContainer.create({
10-
imports: [CatsModule, MappingModule],
11+
imports: [AppModule],
1112
enableEndToEnd: true,
1213
});
1314
supertest = container.supertest;
1415
});
1516

17+
afterEach(() => {
18+
vi.unstubAllEnvs();
19+
});
20+
1621
describe('findMany', () => {
1722
it('should return an array of cats', async () => {
1823
const response = await supertest.get('/cats');
@@ -29,8 +34,8 @@ describe('CatsController', () => {
2934
});
3035

3136
it('should fail due to an invalid include value', async () => {
32-
const response = await supertest.get('/cats?include=invalid');
33-
expect(response.status).toBe(400);
37+
const response = await supertest.get('/cats?include=foo');
38+
expect(response.statusCode).toBe(400);
3439
});
3540
});
3641
});

apps/nest/src/cats/cats.controller.ts

+38-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,43 @@
1-
import { Controller, Get, Query, UsePipes, ValidationPipe } from '@nestjs/common';
1+
import {
2+
BadRequestException,
3+
Controller,
4+
Get,
5+
Query,
6+
UseFilters,
7+
UsePipes,
8+
ValidationPipe,
9+
} from '@nestjs/common';
210
import { ApiOperation, ApiTags } from '@nestjs/swagger';
3-
import { Cat } from './cats.models';
411
import { CatsProvider } from './cats.provider';
5-
import { CatReadResource } from './cats.resource';
612
import { Mapper } from 'packages/nest-utils/dist/main';
713
import { CatsFindManyQuery } from './cats.queries';
14+
import { CatReadResource } from './resources/cat.read.resource';
15+
import { Cat } from './models/cat.model';
16+
17+
import { Catch, ExceptionFilter, ArgumentsHost, HttpException, HttpStatus } from '@nestjs/common';
18+
import { Response } from 'express';
19+
import { ApiException } from '@nanogiants/nestjs-swagger-api-exception-decorator';
20+
21+
@Catch()
22+
export class GlobalExceptionFilter implements ExceptionFilter {
23+
catch(exception: unknown, host: ArgumentsHost) {
24+
const ctx = host.switchToHttp();
25+
const response = ctx.getResponse<Response>();
26+
27+
let status = HttpStatus.INTERNAL_SERVER_ERROR;
28+
let message = 'Internal server error';
29+
30+
if (exception instanceof HttpException) {
31+
status = exception.getStatus();
32+
message = exception.message;
33+
}
34+
35+
response.status(status).json({
36+
statusCode: status,
37+
message: message,
38+
});
39+
}
40+
}
841

942
@Controller('cats')
1043
@UsePipes(
@@ -16,7 +49,9 @@ import { CatsFindManyQuery } from './cats.queries';
1649
whitelist: true,
1750
}),
1851
)
52+
@UseFilters(GlobalExceptionFilter)
1953
@ApiTags('Cats')
54+
@ApiException(() => BadRequestException)
2055
export class CatsController {
2156
constructor(
2257
private readonly provider: CatsProvider,

apps/nest/src/cats/cats.data.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Cat, Owner } from './cats.models';
1+
import { Cat } from './models/cat.model';
2+
import { Owner } from './models/owner.model';
23

34
export const owners: Owner[] = [
45
{

apps/nest/src/cats/cats.provider.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Injectable } from '@nestjs/common';
2-
import { Cat, Owner } from './cats.models';
32
import { cats, owners } from './cats.data';
43
import { CatsFindManyQuery } from './cats.queries';
4+
import { Cat } from './models/cat.model';
5+
import { Owner } from './models/owner.model';
56

67
@Injectable()
78
export class CatsProvider {

apps/nest/src/cats/cats.models.ts renamed to apps/nest/src/cats/models/cat.model.ts

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
import { Map } from '@spuxx/nest-utils';
2-
import { Breed } from './cats.types';
3-
4-
export class Owner {
5-
@Map()
6-
name: string;
7-
@Map()
8-
age: number;
9-
@Map()
10-
cats?: Cat[];
11-
}
12-
2+
import { Breed } from '../cats.types';
3+
import { Owner } from './owner.model';
134
export class Cat {
145
@Map()
156
name: string;
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Map } from '@spuxx/nest-utils';
2+
3+
export class Owner {
4+
@Map()
5+
name: string;
6+
@Map()
7+
age: number;
8+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ApiParamOptions, ApiPropertyOptions } from '@nestjs/swagger';
2-
import { CatReadResource, OwnerReadResource } from './cats.resource';
3-
import { Breed } from './cats.types';
2+
import { Breed } from '../cats.types';
3+
import { OwnerReadResource } from './owner.read.resource';
44

55
export const catPropertyDocs = {
66
name: {
@@ -28,24 +28,3 @@ export const catPropertyDocs = {
2828
type: OwnerReadResource,
2929
} as ApiPropertyOptions,
3030
};
31-
32-
export const ownerPropertyDocs = {
33-
name: {
34-
name: 'name',
35-
description: 'The name of the owner',
36-
example: 'John Doe',
37-
} as ApiPropertyOptions & ApiParamOptions,
38-
39-
age: {
40-
name: 'age',
41-
description: 'The age of the owner',
42-
example: 30,
43-
} as ApiPropertyOptions,
44-
45-
cats: {
46-
name: 'cats',
47-
description: 'The cats owned by the owner',
48-
type: CatReadResource,
49-
isArray: true,
50-
} as ApiPropertyOptions,
51-
};

apps/nest/src/cats/cats.resource.ts renamed to apps/nest/src/cats/resources/cat.read.resource.ts

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
import { ApiProperty } from '@nestjs/swagger';
22
import { Map } from '@spuxx/nest-utils';
3-
import { catPropertyDocs, ownerPropertyDocs } from './cats.property-docs';
4-
5-
export class OwnerReadResource {
6-
@Map()
7-
@ApiProperty(ownerPropertyDocs.name)
8-
name: string;
9-
@Map()
10-
@ApiProperty(ownerPropertyDocs.age)
11-
age: string;
12-
@Map()
13-
@ApiProperty(ownerPropertyDocs.cats)
14-
cats: CatReadResource[];
15-
}
3+
import { catPropertyDocs } from './cat.property-docs';
4+
import { OwnerReadResource } from './owner.read.resource';
165

176
export class CatReadResource {
187
@Map()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ApiParamOptions, ApiPropertyOptions } from '@nestjs/swagger';
2+
3+
export const ownerPropertyDocs = {
4+
name: {
5+
name: 'name',
6+
description: 'The name of the owner',
7+
example: 'John Doe',
8+
} as ApiPropertyOptions & ApiParamOptions,
9+
10+
age: {
11+
name: 'age',
12+
description: 'The age of the owner',
13+
example: 30,
14+
} as ApiPropertyOptions,
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { Map } from '@spuxx/nest-utils';
3+
import { ownerPropertyDocs } from './owner.property-docs';
4+
5+
export class OwnerReadResource {
6+
@Map()
7+
@ApiProperty(ownerPropertyDocs.name)
8+
name: string;
9+
@Map()
10+
@ApiProperty(ownerPropertyDocs.age)
11+
age: string;
12+
}

packages/nest-utils/src/http/query-params/include/include-query-param.decorator.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('IncludeQueryParam', () => {
2828
include: [],
2929
});
3030
await expect(pipe.transform({}, transformMetadata)).resolves.toEqual({
31-
include: [],
31+
include: undefined,
3232
});
3333
});
3434

0 commit comments

Comments
 (0)