Nest: Unit testing with Pipes and service

Created on 9 Mar 2018  路  4Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

This is based on the 01-cats example cats create with sequelize, when executing a unit test in related the cats controller, it seems the Validation pipe is ignored, as well as a error is shown "Model not initialized".

Expected behavior


When the test in related to trigger the validation should show the validation error message, as well as the create unit test should insert the data

Minimal reproduction of the problem with instructions

https://github.com/opiuq/unittest-nestjs-cats-app-help

What is the motivation / use case for changing the behavior?


It would be great to have a reference on the docs on how to run unit test on pipes as we as controllers which are dependent on model's.

Environment


Nest version: 4.3.0

question 馃檶

Most helpful comment

This is not a bug. Unit testing is useful for testing classes without any bounded dynamic metadata (in this case, without pipes). You shouldn't test pipe logic inside your controller .spec files. 馃檪

All 4 comments

This is not a bug. Unit testing is useful for testing classes without any bounded dynamic metadata (in this case, without pipes). You shouldn't test pipe logic inside your controller .spec files. 馃檪

@kamilmysliwiec righty looks like my mistake, thanks for the clarification

@kamilmysliwiec, hi.
Can you show some examples(may be link?) for e2e test for controller with Pipe(), which makes external requests
I mean whether it is possible to somehow change(replace) the pipe through the DI ?

@kamilmysliwiec, hi.
Can you show some examples(may be link?) for e2e test for controller with Pipe(), which makes external requests
I mean whether it is possible to somehow change(replace) the pipe through the DI ?

This is how I mock pipe.

const mockSomeService = () => ({
  create: jest.fn(),
});

const mockSomePipe = jest.fn();

const module = await Test.createTestingModule({
      providers: [
        SomeResolver,
        {
          provide: SomeService,
          useFactory: mockSomeService,
        },
      ],
})
.overridePipe(SomePipe)
.useValue(mockSomePipe)
.compile();
Was this page helpful?
0 / 5 - 0 ratings

Related issues

2233322 picture 2233322  路  3Comments

JulianBiermann picture JulianBiermann  路  3Comments

rlesniak picture rlesniak  路  3Comments

artaommahe picture artaommahe  路  3Comments

janckerchen picture janckerchen  路  3Comments