Skip to content

How to setup a Next.js Test Environment with Prisma Client, Mocha and Chai

Notifications You must be signed in to change notification settings

vwedesam/Prisma-Next.js-Api-Testing-Sample-code

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Prisma-Next.js-Api-Testing-Sample-code

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

Installation

$ 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

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

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.

Sample Code

1 code prep

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
        }

Config

add to package.json

    "mocha": {
        "spec": "test/api"
    }

to specify test directory for mocha

2 Test Suite

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
            });

    });


});

License

This project is released under the MIT license.

About

How to setup a Next.js Test Environment with Prisma Client, Mocha and Chai

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published