The Sample Code shows how to setup an environment for testing your Next.js Api endpoints with Prisma Client
this Setup uses mocha: feature-rich JavaScript test framework and chai: assertion library
$ npm install create-next-app book-api
$ cd book-api
book-api/$ npm install --save-dev mocha
book-api/$ npm install --save-dev chai chai-http
Prisma is an open source next-generation ORM. It consists of the following parts:
- Prisma Client - Prisma Migrate - Prisma Studio
Prisma Client can be used in any Node.js (supported versions) or TypeScript backend application, or anything else that needs a database.
Next.js is an open-source React front-end development web framework that enables functionality such as server-side rendering and generating static websites for React based web applications.
test/index.js
const chai = require("chai")
let chaiHttp = require("chai-http");
const prisma = require("../../prisma")
const server = 'http://localhost:3002';
chai.use(chaiHttp);
const expect = chai.expect;
const should = chai.should();
module.exports = {
chai,
server,
expect,
should,
prisma
}
add to package.json
"mocha": {
"spec": "test/api"
}
to specify test directory for mocha
test/api/book.js
const { server, chai, expect, prisma } = require("../index.js")
describe("book", () => {
// USing Before Hook
// IGNORE FK CONSTRAINTS AND TRUNCATE BOOKS TABLE
before( async function(done) {
// MYSQL
prisma.$executeRaw('SET FOREIGN_KEY_CHECKS = 0;')
.then(done());6
prisma.$executeRaw('TRUNCATE TABLE books;')
.then(done());
// POSTGRESQL
prisma.$executeRaw('TRUNCATE TABLE books cascade;')
then(done());
});
it(" fetch all books ", async () => {
chai.request(server)
.get('/api/book')
.send({})
.end(function (err, res) {
expect(err).to.be.null;
expect(res).to.have.status(200);
expect(res.body).to.have.property('status').with.lengthOf(7) // success
});
});
it(" Add book ", async () => {
chai.request(server)
.post('/api/book')
.send({
title: " dead men tells no tale ",
isbn: "157-jhfc",
author: "vwedesam",
year: 2002
})
.end(function (err, res) {
expect(err).to.be.null;
expect(res).to.have.status(200);
expect(res.body).to.have.property('status').with.lengthOf(7) // success
});
});
});
This project is released under the MIT license.