Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Cypress e2e testing with @nestjs/swagger decorated dto. #11531

Closed
1 task done
lutangar opened this issue Apr 19, 2023 · 2 comments
Closed
1 task done

Allow Cypress e2e testing with @nestjs/swagger decorated dto. #11531

lutangar opened this issue Apr 19, 2023 · 2 comments
Labels
needs triage This issue has not been looked into type: enhancement 🐺

Comments

@lutangar
Copy link

lutangar commented Apr 19, 2023

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

Here is a minimum reproducible example attempting to run Cypress e2e tests against a nestjs api which uses a dto annotated with an @ApiProperty() decorator from @nestjs/swagger :
https://github.com/larriereguichet/nestjs-cypress

Attempting to run the tests with npm run test:cypress ends up with the following error :

Error: Webpack Compilation Error
./node_modules/@nestjs/core/nest-application.js
Module not found: Error: Can't resolve '@nestjs/microservices' in '~/workspace/nestjs-cypress/node_modules/@nestjs/core'
resolve '@nestjs/microservices' in '~/workspace/nestjs-cypress/node_modules/@nestjs/core'
# [...]
@ ./node_modules/@nestjs/core/nest-application.js 122:120-152
 @ ./node_modules/@nestjs/core/index.js
 @ ./node_modules/@nestjs/swagger/dist/swagger-explorer.js
 @ ./node_modules/@nestjs/swagger/dist/swagger-scanner.js
 @ ./node_modules/@nestjs/swagger/dist/swagger-module.js
 @ ./node_modules/@nestjs/swagger/dist/index.js
 @ ./node_modules/@nestjs/swagger/index.js
 @ ./src/dto/cat.dto.ts
 @ ./cypress/e2e/dto.cy.ts
 
    at handle (~/.cache/Cypress/12.10.0/Cypress/resources/app/node_modules/@packages/server/node_modules/@cypress/webpack-preprocessor/dist/index.js:212:23)
    at finalCallback (~/.cache/Cypress/12.10.0/Cypress/resources/app/node_modules/webpack/lib/Compiler.js:257:39)
    at ~/.cache/Cypress/12.10.0/Cypress/resources/app/node_modules/webpack/lib/Compiler.js:306:14
    at AsyncSeriesHook.eval [as callAsync] (eval at create (~/.cache/Cypress/12.10.0/Cypress/resources/app/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (~/.cache/Cypress/12.10.0/Cypress/resources/app/node_modules/tapable/lib/Hook.js:154:20)
    at ~/.cache/Cypress/12.10.0/Cypress/resources/app/node_modules/webpack/lib/Compiler.js:304:22
    at Compiler.emitRecords (~/.cache/Cypress/12.10.0/Cypress/resources/app/node_modules/webpack/lib/Compiler.js:499:39)
    at ~/.cache/Cypress/12.10.0/Cypress/resources/app/node_modules/webpack/lib/Compiler.js:298:10
    at ~/.cache/Cypress/12.10.0/Cypress/resources/app/node_modules/webpack/lib/Compiler.js:485:14
    at AsyncSeriesHook.eval [as callAsync] (eval at create (~/.cache/Cypress/12.10.0/Cypress/resources/app/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (~/.cache/Cypress/12.10.0/Cypress/resources/app/node_modules/tapable/lib/Hook.js:154:20)
    at ~/.cache/Cypress/12.10.0/Cypress/resources/app/node_modules/webpack/lib/Compiler.js:482:27
    at ~/.cache/Cypress/12.10.0/Cypress/resources/app/node_modules/neo-async/async.js:2818:7
    at done (~/.cache/Cypress/12.10.0/Cypress/resources/app/node_modules/neo-async/async.js:3522:9)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (~/.cache/Cypress/12.10.0/Cypress/resources/app/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (~/.cache/Cypress/12.10.0/Cypress/resources/app/node_modules/tapable/lib/Hook.js:154:20)
    at ~/.cache/Cypress/12.10.0/Cypress/resources/app/node_modules/webpack/lib/Compiler.js:464:33
    at ~/.cache/Cypress/12.10.0/Cypress/resources/app/node_modules/@packages/server/node_modules/graceful-fs/graceful-fs.js:143:16
    at ~/.cache/Cypress/12.10.0/Cypress/resources/app/node_modules/@packages/server/node_modules/graceful-fs/graceful-fs.js:143:16
    at ~/.cache/Cypress/12.10.0/Cypress/resources/app/node_modules/@packages/server/node_modules/graceful-fs/graceful-fs.js:61:14
    at FSReqCallback.oncomplete (node:fs:197:23)

src/dto/cat.dto.ts :

import { ApiProperty } from '@nestjs/swagger';

export class CatDto {
  @ApiProperty()
  name: string;
}

cypress/e2e/dto.cy.ts :

import { CatDto } from '../../src/dto/cat.dto';

describe('dtos (cypress  e2e)', () => {
  it('can instantiate a dto annotated with an ApiProperty decorator', () => {
    new CatDto();
  });
});

Note : This was originally an attempt of using Cypress against a nestjs api in an nx repository using @nrwl/cypress and if a solution is found it could also be used in such context.

The solution probably lie in a custom webpack configuration but I fail at finding the how and where it should be done.

Describe the solution you'd like

  1. When creating a nestjs app, there could be the choice of the e2e testing library :
    • supertest
    • cypress
  2. (or) There could be a documentation entry illustrating a way to run cypress tests when an annotated dto is involved.

Teachability, documentation, adoption, migration strategy

Which e2e testing library would you ❤️ to use?

  • supertest
  • cypress

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

Cypress is (one of) the most popular e2e testing library and there could be a solution to use it against a documented nestjs Rest API.

@lutangar lutangar added needs triage This issue has not been looked into type: enhancement 🐺 labels Apr 19, 2023
@micalevisk
Copy link
Member

Hi!

I don't think we should prompt the user to select the testing lib for e2e because supertest can be easily changed by any other as there isn't much code to remove

@kamilmysliwiec
Copy link
Member

Thanks for your suggestion!

There are no plans to implement it in the foreseeable future.

If you think your request could live outside Nest's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

@nestjs nestjs locked and limited conversation to collaborators Apr 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs triage This issue has not been looked into type: enhancement 🐺
Projects
None yet
Development

No branches or pull requests

3 participants