question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Documentation or official way to e2e test with Crud?

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
maximelafariecommented, Apr 19, 2020

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. 😉👌

1reaction
michaelyalicommented, Apr 16, 2020

have you set alwaysPaginate to true?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Write CRUD E2E tests with Angular and Cypress
First Test Case: Login. In this section, I'm going to show you how to write our first cypress test to check the login...
Read more >
REST API E2E Test Automation for CRUD Operations - YouTube
... CHANNEL: https://bit.ly/2YGU6JM In this SoapUI Tutorial we will learn how to automate REST API end-to-end test case for CRUD operations.
Read more >
Easy E2E tests for CRUD apps with puppeteer-tools - Medium
In this article we will see how to test a supposed Client form which contains only basic information (name and email).
Read more >
REST API E2E Test Automation for CRUD Operations
In this SoapUI Tutorial we will learn how to automate REST API end-to-end test case for CRUD operations. This is part 2 of...
Read more >
REST API E2E Test Automation for CRUD Operations
In this SoapUI Tutorial, we will learn how to automate the REST API end-to-end test case for CRUD operations. This is part 2...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found