Comments (7)
@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.
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.
PS: I don't have any idea how I put the "bug" label. Could you remove it? Thanks.
from testing-nestjs.
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.
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.
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.
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)
- About import {DocumentQuery} from 'mongoose' at mongo-sample
- Suggestion: convert testing-nestjs into monorepo with Lerna HOT 3
- [Question] How I mock another service method? HOT 8
- [FAILING TEST] Unit testing a NestJS Service that consumes a TypeORM Repository HOT 6
- [NEW TEST] Testing for interceptor failure HOT 3
- [NEW TEST] KnexJs Sample test HOT 1
- Multiple mocked dependencies with the same content HOT 5
- [NEW TEST] Bull HOT 4
- Prisma transactions and rollbacks now available HOT 2
- [NEW TEST] - (Contract Testing) - Incorporate NestJS Consumer Driven Contract testing with nestjs-pact HOT 6
- README refers to non-existing cat.integration.spec.ts HOT 3
- Test against Solidity smart contracts
- [NEW TEST] Tests for mikro-orm
- [NEW TEST] Sequelize transactions HOT 1
- Add code comment for documenting the tests. HOT 1
- Unable to commit the file in file-up-and-down-sample HOT 1
- Can't push to sidebranch for PR - getting permission denied HOT 1
- I can't seem to make the tests fails anywhere so far HOT 3
- npm install doesn't work
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from testing-nestjs.