I'm unable to functionally test my app with Crud.
Assuming the following functional test file:
import * as request from 'supertest';
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UsersService } from '../src/app/users/users.service';
import { UsersModule } from '../src/app/users/users.module';
import { Users } from '../src/app/users/users.entity';
import { config } from '../src/app/config';
import { Repository } from 'typeorm';
import { MailService } from '../src/app/services/mail/mail.service';
import { PassportModule, AuthGuard } from '@nestjs/passport';
describe('Users', () => {
let app: INestApplication;
const usersService = { findAll: () => ['test'] };
let repository: Repository<Users>;
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [
TypeOrmModule.forRoot({
...config.database,
synchronize: true,
}),
PassportModule.register({ defaultStrategy: 'jwt', session: false }),
UsersModule
],
})
.overrideProvider(UsersService)
.useValue(usersService)
.overrideProvider(MailService)
.useValue({ sendResetPassword: () => true })
.overrideGuard(AuthGuard('jwt'))
.useValue({ canActivate: () => true })
.compile();
app = moduleRef.createNestApplication();
repository = moduleRef.get('UsersRepository');
const connection = repository.manager.connection;
// dropBeforeSync: If set to true then it drops the database with all its tables and data
await connection.synchronize(true);
await app.init();
});
it(`/GET users`, async () => {
return await request(app.getHttpServer())
.get('/users')
.expect(200)
.expect({
data: usersService.findAll(),
});
});
afterAll(async () => {
await app.close();
});
});
It returns me the following error message:
FAIL e2e/users.e2e-spec.ts (75.291s)
Users
β /GET users (298ms)
β Users βΊ /GET users
expected 200 "OK", got 500 "Internal Server Error"
at Test.Object.<anonymous>.Test._assertStatus (../node_modules/supertest/lib/test.js:268:12)
at Test.Object.<anonymous>.Test._assertFunction (../node_modules/supertest/lib/test.js:283:11)
at Test.Object.<anonymous>.Test.assert (../node_modules/supertest/lib/test.js:173:18)
at Server.localAssert (../node_modules/supertest/lib/test.js:131:12)
console.log src/app/common/middlewares/logger.middleware.ts:12
Wed Apr 15 2020 12:55:45 GMT+0000 (Coordinated Universal Time) - [Request] - {}
[Nest] 1279 - 04/15/2020, 12:55:45 PM [ExceptionHandler] this.service.getMany is not a function
TypeError: this.service.getMany is not a function
at UsersController.getManyBase (/usr/src/app/node_modules/@nestjsx/crud/src/crud/crud-routes.factory.ts:203:27)
at UsersController.getMany (/usr/src/app/src/app/users/users.controller.ts:118:28)
at /usr/src/app/node_modules/@nestjs/core/router/router-execution-context.js:37:29
at processTicksAndRejections (internal/process/task_queues.js:97:5)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 75.655s, estimated 89s
Ran all test suites.
My users.controller.ts file looks like:
...
@Override('getManyBase')
@Roles('admin')
async getMany(@ParsedRequest() req: CrudRequest) {
return await this.base.getManyBase(req);
}
...
So is there any documentation (if so, I don't find it) or an "official way" to run e2e tests while using nestjsx/crud?
I thought about overriding the controller's getMany() method, but before I thought there was another "clean" way to do this, since I would like to query my test database as in dev/prod env.
this.service.getMany is not a function
const usersService = { findAll: () => ['test'] };
As you can see here, you should mock getMany function but not a findAll (there is no such function in CrudService)
Thank you @zMotivat0r, you're right. So it's not possible to call the real "test" data from my test database? I can only make fake calls?
@maximelafarie, of course, you can test against a real database. It just needs to be up and running apparently.
Here is an example of e2e tests https://github.com/nestjsx/crud/blob/master/packages/crud-typeorm/test/c.basic-crud.spec.ts
Awesome, thank you very much. Maybe can you tell me why I don't have data property on req.body but directly data in req.body? Normally the api is returning the response body with data inside, but not in the e2e test. π
Again, thank you! π€
have you set alwaysPaginate to true?
Nope, that was that! Thank you for all it's awesome! π
P.S.: I don't know if it's already done or not, but this example e2e test file should be linked in the doc, it's so awesome and simple to understand it's self-sufficient and doesn't require more explanations about how to do e2e tests with @nestjsx/crud. ππ