Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write test cases to pass Code Coverage with mocha nyc for API?

I'm trying to write a test case using mocha for testing strapi API's i tried searching in documentation but couldn't understand it i just wanted to know how exactly do we write a unit test case for testing any API. Earlier i was using below shown approach but after doing code coverage using **nyc Istanbul package it showed 0% for lines and many branches.

const request = require("co-supertest");
var assert = require("chai").assert;

const { SERVER_URL, PAYLOAD } = require("../config/config");
let JWT;
let dataId;

describe("States Module Endpoint", function () {
  before(function (done) {
   request(SERVER_URL)
    .post("/auth/local")
    .send(PAYLOAD)
    .expect(200)
    .expect("Content-Type", /json/)
    .end(function (err, res) {
      if (err) return done(err);
      const response = res.body;
      JWT = response["jwt"];
      done();
    });
});

describe("Create Method", function () {
  // case for empty,required and correct params for Create method done here
  describe("POST /crm-plugin/states/", function () {
    it("should not create an entry when empty params test case is executed", function (done)     {
      request(SERVER_URL)
        .post("/crm-plugin/states")
        .send({})
        .set("Authorization", "Bearer " + JWT)
        .expect(200)
        .end(function (err, res) {
          if (err) return done(err);
          assert.isEmpty(
            res.body,
            "Empty response is expected when params are empty"
          );
          done();
        });
    });

    it("should not create an entry when required params test case is executed", function   (done) {
      request(SERVER_URL)
        .post("/crm-plugin/states")
        .send({
          is_active: true,
        })
        .set("Authorization", "Bearer " + JWT)
        .expect(200)
        .end(function (err, res) {
          if (err) return done(err);
          assert.isEmpty(
            res.body,
            "Empty response is expected when required params are missing"
          );
          done();
        });
    });

    it("should create an entry when correct params test case is executed", function (done) {
      request(SERVER_URL)
        .post("/crm-plugin/states")
        .send({
          name: "Gujarat",
        })
        .set("Authorization", "Bearer " + JWT)
        .expect(200)
        .end(function (err, res) {
          dataId = res.body.id;
          if (err) return done(err);
          assert.strictEqual(
            res.body.name,
            "Gujarat",
            "Object in response should not differ"
          );
          dataId = res.body.id;
          done();
        });
    });
  });
});

describe("Update Method", function () {
  // case for correct params done for update method
  describe("PUT /crm-plugin/states/:id", function () {
    it("should update the data when correct params test case is executed", function (done) {
      request(SERVER_URL)
        .put("/crm-plugin/states/" + dataId)
        .send({
          name: "Goa",
        })
        .set("Authorization", "Bearer " + JWT)
        .expect(200)
        .end(function (err, res) {
          if (err) return done(err);
          assert.strictEqual(
            res.body.name,
            "Goa",
            "Object in response should not differ"
          );
          done();
        });
    });
  });
});

describe("Find Method", function () {
  // case for empty params done here
  describe("GET /crm-plugin/states", function () {
    it("responds with all records when empty params test case is executed", function (done) {
      request(SERVER_URL)
        .get("/crm-plugin/states")
        .set("Authorization", "Bearer " + JWT)
        .expect(200)
        .end(function (err, res) {
          if (err) return done(err);
          assert.isAtLeast(
            res.body.length,
            1,
            "Find method should return atleast one response."
          );
          done();
        });
    });
  });
});

describe("FindOne Method", function () {
  // case for correct params done here
  describe("GET /crm-plugin/states/:id", function () {
    it("responds with matching records when correct params test case is executed", function (done) {
      request(SERVER_URL)
        .get("/crm-plugin/states/" + dataId)
        .set("Authorization", "Bearer " + JWT)
        .expect(200)
        .end(function (err, res) {
          if (err) return done(err);
          assert.strictEqual(
            res.body.name,
            "Goa",
            "FindOne Method should return response with same name"
          );
          done();
        });
    });
  });
});

 describe("Count Method", function () {
   // case for count done here
   describe("GET /crm-plugin/states/count", function () {
    it("should return data count when correct params test case is executed", function (done) {
      request(SERVER_URL)
        .get("/crm-plugin/states/count")
        .set("Authorization", "Bearer " + JWT)
        .expect(200)
        .end(function (err, res) {
          if (err) return done(err);
          assert.isAtLeast(res.body, 1, "Count expected to be atleast 1");
          done();
        });
    });
  });
});

describe("Delete Method", function () {
  // case for correct params done here
  describe("DELETE /crm-plugin/states/:id", function () {
    it("should delete entry when correct params test case is executed", function (done) {
      request(SERVER_URL)
        .delete("/crm-plugin/states/" + dataId)
        .set("Authorization", "Bearer " + JWT)
        .expect(200)
        .end(function (err, res) {
          if (err) return done(err);
          assert.strictEqual(
            res.body.name,
            "Goa",
            "Object in response should not differ"
          );
          done();
        });
    });
  });
});

});

like image 517
suyesh tiwari Avatar asked Nov 07 '22 07:11

suyesh tiwari


1 Answers

I think this guide will help you - https://github.com/strapi/strapi/pull/6324

It's how you can create a test suite for your Strapi app.

like image 69
Jim LAURIE Avatar answered Nov 12 '22 16:11

Jim LAURIE