I am running into an issue with supertest when a query parameter ({count: false}) is passed NestJS treats it as a string because the dto validation is not being executed. test is
it.only('should NOT return count if count is turned off', async done => {
const result = await request(app.getHttpServer())
.get('/setting/')
.set('Content-Type', 'application/json')
.query({count:false})
.expect(200)
done();
});
dto is
export class CollectionQueryDto {
@IsOptional()
@IsNotEmpty()
afterId: any;
@IsOptional()
@IsOptional()
@Transform((count, obj, type) =>
obj.count.toLowerCase() === 'true' ? true : false,
)
count: boolean;
constructor(partial: Partial<CollectionQueryDto> = {}) {
Object.assign(this, partial);
}
}
beforeAll is setup as follows
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [
SettingModule,
SortationOrmModule,
ConfigModule,
GlobalOrmModule,
],
providers: [SettingService, ConfigService],
}).compile();
app = moduleFixture.createNestApplication();
service = moduleFixture.get<SettingService>(SettingService);
await app.init();
const rowsAffected = await loadConfigurationData(
'TST.loadConfigurationData',
);
});
Main.ts has the validation pipe
app.useGlobalPipes(
new ValidationPipe({
transform: true,
forbidUnknownValues: true,
transformOptions: {
enableImplicitConversion: true,
},
exceptionFactory: errors => new ClassValidationException(errors),
}),
);
Any help would be appreciated.
In your setup, you can add app.useGlobalPipes(new ValidationPipe())
just like you do in main.ts
so that pipes run for your e2e tests. You can do the same with interceptors, filters, and guards too.