#ValidationPipe does not transform queries in e2e-test

1 messages · Page 1 of 1 (latest)

prisma tusk
#

I am using ValidationPipe to transform request according to DTOs as follows:

app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true,
      transform: true,
      forbidNonWhitelisted: true,
      transformOptions: {
        enableImplicitConversion: true,
      },
    }),
  );

This seems to work when getting requests from the browser/frontend, but during e2e-tests it fails to transform a query like the following:

startDate = new Date().toISOString();
request(...).get(`.../?startDate=${startDate}`);

request here being from supertest

The DTO is as follows:

@IsNotEmpty()
@IsDate()
readonly startDate: Date;

I am confused since it appears to fail only during e2e-tests? Any help greatly appreciated

toxic crystal
#

Is the validation pipe bound in the e2e test too, or only in the bootstrap function that is not executed for tests?

prisma tusk
#

It should be, this is the setup used for testing:

const moduleFixture: TestingModule = await Test.createTestingModule({
    imports: [AppModule],
  }).compile();

  app = moduleFixture.createNestApplication();
  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true,
      transform: true,
      forbidNonWhitelisted: true,
      transformOptions: {
        enableImplicitConversion: true,
      },
    }),
  );
  app.use(cookieParser());
  await app.init();
sudden cedar
#

So what is happening? Looks like you've got the pipe properly bound which is good, so what's going on instead?

prisma tusk
#

During test I send a query with a date (as string naturally). If I include the validator @IsDate() on the dto, the test will fail because "startDate must be a date". If I send the same request from the browser there is no problem.

sudden cedar
#

Hmm, definitely interesting. Any chance you have this in a reproduction? There's a chance that ts-jest isn't properly reflecting the data type so class-transformer isn't aware of what to transform the string to

prisma tusk
#

Don't have any reproduction at the moment, might be able to set something up at a later stage. Any ideas of things I could try or tests to confirm what could be the issue?

prisma tusk
#

Update on this: I now believe this is due to using the MockDate package to spoof system-time in e2e tests. This library seems to prevent Date from being reflected properly. Anyone have a quick fix or recommendation for other date-mocks?

toxic crystal
#

Although I'm not sure it that wouldn't have the same problem

prisma tusk
#

I've messed around with that, but it seems to really act up in an asynchronous context (or at least I think that's the problem). Tests will time out and basically none of them will run.