Giter VIP home page Giter VIP logo

Comments (7)

tak1n avatar tak1n commented on July 22, 2024 3

@QimatLuo there are a few discussion threads open on prisma side on how todo testing:

For e2e tests we use a similar approach like in Rails (I'm coming from a ruby/rails background). We basically have a test database (dbname-test) in which we apply the schema through prisma migrate dev, after that we just execute the e2e tests which truncates the tables after each test case (Rails is using transactions for this, but currently long running transactions are not supported by prisma).

Here some examples of e2e tests:

import request from 'supertest';
import { Test } from '@nestjs/testing';
import { INestApplication, ValidationPipe } from '@nestjs/common';
import { PrismaService } from '../src/prisma.service';
import { ProductsModule } from '../src/products/products.module';

describe('Products', () => {
  let app: INestApplication;
  let prisma: PrismaService;

  beforeAll(async () => {
    const moduleRef = await Test.createTestingModule({
      imports: [ProductsModule, PrismaService],
    }).compile();

    app = moduleRef.createNestApplication();
    prisma = moduleRef.get<PrismaService>(PrismaService);

    app.useGlobalPipes(new ValidationPipe({ whitelist: true }));

    await app.init();
  });

  afterAll(async () => {
    await app.close();
  });

  afterEach(async () => {
    // TODO: use transactions and transaction rollback once prisma supports it
    await prisma.truncate();
    await prisma.resetSequences();
  });

  it(`/products (GET)`, async () => {
    await prisma.product.createMany({
      data: products,
    });

    await request(app.getHttpServer())
      .get('/products')
      .expect(200)
      .expect((res) => {
        expect(res.body).toStrictEqual(
          expect.arrayContaining([expect.objectContaining(productShape)]),
        );
      });
  });

  it(`/products/:uuid (GET)`, async () => {
    const storedProduct = await prisma.product.create({
      data: product,
    });

    await request(app.getHttpServer())
      .get(`/products/${storedProduct.id}`)
      .expect(200)
      .expect((res) => {
        expect(res.body).toStrictEqual(expect.objectContaining(productShape));
      });
  });
});

Here the truncate method of the prisma service:

  async truncate() {
    for (const { tablename } of await this
      .$queryRaw`SELECT tablename FROM pg_tables WHERE schemaname='public'`) {
      if (tablename !== '_prisma_migrations') {
        // eslint-disable-next-line no-await-in-loop
        await this.$queryRaw(`TRUNCATE TABLE "public"."${tablename}" CASCADE;`);
      }
    }
  }

Hope this helps you or someone else :)
I could also try to craft a PR once I have some time.

from testing-nestjs.

jmcdo29 avatar jmcdo29 commented on July 22, 2024 1

Thanks for all the info on this. I plan on getting back to maintenance on this repo as of next week once I get back from vacation.

from testing-nestjs.

felinto-dev avatar felinto-dev commented on July 22, 2024

PS: I don't have any idea how I put the "bug" label. Could you remove it? Thanks.

from testing-nestjs.

jmcdo29 avatar jmcdo29 commented on July 22, 2024

I could definitely see some use for it. Would you like to try your hand at a PR for this, following the examples for TypeORM, Mongoose, and Sequelize?

from testing-nestjs.

felinto-dev avatar felinto-dev commented on July 22, 2024

I love to contribute to open-source projects, but the reason I'm suggesting this is partly because I don't have a clue how to do it.

from testing-nestjs.

jmcdo29 avatar jmcdo29 commented on July 22, 2024

Fair enough! I'll try to get to this when I have some time then. I've got some other improvements I'd like to make too

from testing-nestjs.

QimatLuo avatar QimatLuo commented on July 22, 2024

I need prisma example too.
I'm not really understand how to write test with prisma in nestjs even though prisma official website have example without nestjs.

from testing-nestjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.