Mocha Unit Testing: Uncaught TypeError: Cannot read property 'apply' of undefined

Viewed 2324

I want to write unit testing for Restful Endpoint, but whenever I run npm test. I get the error Uncaught TypeError: Cannot read property 'apply' of undefined .

let chai = require('chai')
let chaiHttp = require('chai-http')
let home = require('../routes/home')
let user = require('../routes/users')
//Assertion Style
chai.should()

chai.use(chaiHttp)

describe('chow Api', () => {
  //  users
    describe("GET /api/chow/home", () => {
        it('it should render Welcome to Homepage', (done) =>{
            chai.request(home)
            .get("/api/chow/home")
            .end((err, response) =>{
                response.should.have.status(200)
                response.body.should.be('string')
                done()
            })
        })
    })

    describe('GET /api/chow/users', () => {
        it('it should GET all the users', (done) => {
            chai.request(user)
            .get("/api/chow/users")
            .end((err, response) =>{
                response.should.have.status(200)
                response.should.be.a('array')
                done()
            })
        })
    })
})
3 Answers

Follow changes to be done for the Mocha test to be working

in index.js, you should add this line module.exports = app; for the server to start.

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

const homeController = require("./routes/home");

app.use("/home", homeController);

const port = process.env.PORT || 5000;

app.listen(port, () => {
    console.log("Server started at port", port);
});

module.exports = app;

in test/routes/home.js, you should add .get("/home") and res.text.should.equal("Hello World!"); to pass Mocha test for /home endpoint

const chai = require("chai");
const chaiHTTP = require("chai-http");

const mainEntryPoint = require("../../index");

chai.should();
chai.use(chaiHTTP);

describe("Entry point", () => {
    context("GET /", () => {
        it("should get greeting back", (done) => {
            chai.request(mainEntryPoint)
                .get("/home")
                .end((err, res) => {
                    if (err) throw err;
                    res.text.should.equal("Hello World!");
                    done();
                });
        });
    });
});

Maybe you should try:

const app = require("./app.js")
chai.request(app).get("/api/chow/users")
chai.request(app).get("/api/chow/users")

Or

chai.request(home).get("/")
chai.request(user).get("/")

Please try:

....
.get("/") // This should be a relative path from current route
.end((err, response) =>{
    if(err) return done(err)
    response.should.have.status(200)
    response.body.should.be('string')
    done()
});

I cannot say for 100% sure without seeing the codes for routes. But, TypeError: cannot do whatever with undefined occurs when you access an object property or method which is undefined. I would suggest printing the responses to get some insight. Also, please share your route logic. Without that, it's hard to determine the source of the error

Related