mercredi 7 avril 2021

Integration testing of nodejs, express api routes using jest, supertest

I am an absolute beginner in testing and I need to show evidence of testing of my nodejs app with unit , integration and system testing techniques.

There are 10 .ejs modules in the app, each modules has currently its own script linked to its own .js file containing functions.

I am now trying to show evidence of integration testing by testing api routes of the app, i followed the instructions on the website: https://medium.com/@pojotorshemi/integration-test-on-express-restful-apis-using-jest-and-supertest-4cf5d1414ab0

I have started testing the first module task1.ejs but something is not right as I am getting an error : TypeError: Router.use() requires a middleware function but got a Object

52 | app.use("../public/views/task1", serverRoutes); //routes

Could you please help with this? Any feedback and suggestion on better way to perform unit, integration and system testing on the app is highly appreciated!! Thanks a lot!!

IntegrationTesting.test.js

/* Testing express web apis/endpoints or routes or node js app is form of integration testing. */ // Integration Test script const express = require("express"); // import express const serverRoutes = require("../app"); //import file we you have to test api / routes or something const request = require("supertest"); // supertest is a framework that allows to easily test web apis in jest

const app = express(); //an instance of an express app, a 'fake' express app for jest

app.use("../public/views/task1", serverRoutes); //routes
describe('task list API Integration Tests', function () {
    describe('#GET / tasks', function () { // Describe is block contains multiple test for single section or scope or file 
        test('should get all tasks', function (done) {
            request(app).get('/task1')
               request(app).get('/task2')
              .end(function (err, res) {
                  // Test 1
                  expect(res.statusCode).to.equal(200);
                  // Test 2
                  expect(res.body).to.be.an('array'); // check whatever return string or object or array
                  // Test 3
                  expect(res.body).to.be.empty;
                  done();
              });
        });
    });
});

APP.JS

const express = require("express");
const app = express();

const ejs = require("ejs");

app.use("/public", express.static(__dirname + "/public"));
app.use("/public", express.static(__dirname + "/"));

app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.set("view engine", "ejs");

app.set("views", __dirname + "/public/views");

app.get("/", (req, res) => {
  res.render("index");
});

app.get("/task1", function (req, res) {
  res.render("task1");
});

app.get("/task2", function (req, res) {
  res.render("task2");
});

app.get("/task3", function (req, res) {
  res.render("task3");
});

app.get("/task4", function (req, res) {
  res.render("task4");
});

app.get("/task5", function (req, res) {
  res.render("task5");
});

app.get("/task6", function (req, res) {
  res.render("task6");
});

app.get("/task7", function (req, res) {
  res.render("task7");
});

app.get("/task8", function (req, res) {
  res.render("task8");
});

app.get("/task9", function (req, res) {
  res.render("task9");
});


app.get("/task10", function (req, res) {
  res.render("task10");
});

let port = process.env.PORT;
if (port == null || port == "") {
  port = 3000;
}

app.listen(port, function () {
  console.log("Server started on port 3000");
});

Aucun commentaire:

Enregistrer un commentaire