nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.77k stars 7.64k forks source link

Suggestion: add mocking/fixtures library to @nestjs/testing #6342

Closed omermorad closed 3 years ago

omermorad commented 3 years ago

Feature Suggestion

Hi,

I opened this issue after a lot of time that my team and I encountered a problem (or rather process) that is systematically recurring.

Most of our microservices are developed in Nest, it gives us a complete solution and works great for us in a production environment. When it comes to tests, or to be precise, e2e testing, we tend to test the full flow, from the request to the controller to the database, which we mainly use an in-memory database.

To create reliable tests, we prepare a lot of fixtures/mocks in advance to sow them in the database before running the test and then check manipulations on this information in the database.

We recently thought of an effective solution that shortens the work of preparing the fixtures and decided to release a package called Mockingbird that allows the use of @Mock decorator on top of the DTO or the schema mongoose class which is used to design the database schema (using the @Prop decorator)

We thought it would be nice to share this with the Nest community by adding an example in the documentation or in the testing package itself, (if you find it efficient).

Here is an example use of the library with NestJS based on the example that exists in the sample of Mongoose:

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Mock, MockFactory } from 'mockingbird-ts';
import { Document } from 'mongoose';

export type CatDocument = Cat & Document;

@Schema()
export class Cat {
  @Prop()
  @Mock(faker => faker.name.firstName())
  name: string;

  @Prop()
  @Mock(faker => faker.random.number(100))
  age: number;

  @Prop()
  @Mock() // Will generate a random string
  breed: string;
}

export const CatSchema = SchemaFactory.createForClass(Cat);
export const CatFixture = MockFactory.create(Cat);

Hope this makes sense :)

jmcdo29 commented 3 years ago

Seems like this may be useful, but I'm not sure if we'd want to add it into the official sample (for the same reason the testing-nestjs repo is a separate, community-managed repository) to make sure people don't think this is the only way to do things. As the maintainer of the testing-nestjs repo I'd be happy to have this as a sample there. Similar to the @golevelup/ts-jest sample I'm using there too.

kamilmysliwiec commented 3 years ago

As @jmcdo29 said, I don't think we should be adding this to the official sample due to the reasons outlined above. The package itself seems to be very useful though so if you could publish & share it with the community, maybe write a short article on how this could be used, that would be amazing! Posting it on our Discord channel should help with "marketing" too 🙌

omermorad commented 3 years ago

Thank you guys, @kamilmysliwiec and @jmcdo29

Actually, I am working on a short article these days, and then I will post both the article and the repo (at the Discord channel). @jmcdo29 - I'm also working on the e2e example; It will take me some time because of my work but expect it soon :)

Cheers