Chai-http not working - node.js

Getting error Uncaught TypeError: Cannot read property 'apply' of undefined
at Immediate. (node_modules/express/lib/router/index.js:618:14)
Server and test files.
// server js file
'use strict';
var express = require('express'); // call express
var app = express();
var port = process.env.PORT || 8080;
// Routes for schedule API
var router = express.Router();
router.get('/', function(req, res, next) {
res.json({"msg":'Hello, World!'});
});
app.listen(port);
console.log('Application running on port ' + port);
module.exports=router;
// test.js
var chai = require('chai');
var chaiHttp=require('chai-http');
var server = require('../server');
var should = chai.should();
chai.use(chaiHttp);
describe('Scheduler', function () {
it('should return hello world', function (done) {
chai.request(server)
.get('/')
.end(function (err, res) {
res.should.have.status(200);
done();
};
});
});
Could anybody help me to track what went wrong here?

You can use expect instead of should it's more easier
var chai = require('chai'), chaiHttp = require('chai-http');
chai.use(chaiHttp);
var expect = chai.expect;
app.get('/check', function(req, res) {
//exemple
expect(req.query.ip,"IP Adress Mismatch").to.be.an.ip;
}

Related

Chai testing TypeError: Converting circular structure to JSON

I'm a new learner express.js I want to test simple post and get operations with tdd mechanism. I created the test, route, index and db files but when I try to test POST method it gives me this error.
This is my routes/task.js
const express = require('express');
const router = express.Router();
router.post("/api/task", async (req,res) => {
try {
const task = await new Task(req.body).save();
res.send(task);
} catch (error) {
res.send(error);
}
})
This is my test/task.js
let chai = require("chai");
const chaiHttp = require("chai-http");
const { send } = require("process");
let server = require("../index");
//Assertion Style
chai.should();
chai.use(chaiHttp);
describe('Tasks API', () => {
/**
* Test the POST Route
*/
describe('POST /api/task', () => {
it("It should POST a new task", () => {
const task = {task: "Wake Up"};
chai.request(server)
.post("/api/task")
.send(task)
.end((err, response) => {
response.should.have.status(201);
response.body.should.be.a('string');
response.body.should.have.property('id');
response.body.should.have.property('task');
response.body.should.have.property('task').eq("Wake Up");
response.body.length.should.be.eq(1);
done();
});
});
});
});
This is my db.js
var sqlite3 = require('sqlite3').verbose()
const DBSOURCE = "db.sqlite"
let db = new sqlite3.Database(DBSOURCE, (err) => {
if (err) {
// Cannot open database
console.error(err.message)
throw err
}else{
console.log('Connected to the SQLite database.')
db.run(`CREATE TABLE IF NOT EXISTS todo (
id INTEGER PRIMARY KEY AUTOINCREMENT,
task text
)`,
(err) => {
if (err) {
// Table already created
console.log(err);
}
});
}
});
module.exports = db
And this is my index.js
const connection = require('./db');
const express = require('express');
const app = express();
const cors = require("cors");
const port = process.env.PORT || 8080;
app.use(express.json());
app.use(cors());
app.get('/', (req, res) => {
res.send('Hello World');
});
app.post('/api/task', (req, res) => {
res.status(201).send(req);
});
app.listen(port, () => console.log(`Listening on port ${port}...`));
module.exports = app;
The thing that I try to do is building a test case to test the post method. I think I couldn't built the correct relations the files.
Currently, just by doing a POST request to /api/task, the error will appear. That is because of these lines in index.js:
app.post('/api/task', (req, res) => {
res.status(201).send(req);
});
The req parameter is circular, hence cannot be JSON-stringified.
Solution
In routes/task.js export the router:
const express = require('express');
const router = express.Router();
router.post("/api/task", async (req,res) => {
try {
const task = await new Task(req.body).save();
res.send(task);
} catch (error) {
res.send(error);
}
})
// By adding this line you can export the router
module.exports = router
In index.js, include the routes/task.js file and pass it to app.use(...), also remove the now-obsolete /api/task route:
const connection = require('./db');
const express = require('express');
const app = express();
const cors = require("cors");
const taskRoutes = require("./routes/task")
const port = process.env.PORT || 8080;
app.use(express.json());
app.use(cors());
app.get('/', (req, res) => {
res.send('Hello World');
});
app.use(taskRoutes)
app.listen(port, () => console.log(`Listening on port ${port}...`));
module.exports = app;
This way we got rid of the circular structure stringifying and the tests should now pass.

AssertionError: expected undefined to equal 200

const express = require("express");
const app = express();
const router = express.Router();
router.get('/postp',(req,res)=>{
res.status(200);
})
app.listen(PORT, () => {
console.log(`Connected to PORT : ${PORT}`);
});
module.exports = app;
Test case :
const chai = require('chai');
const request = require('supertest');
const app = require('../app');
const post = require('../router/post');
const expect = chai.expect;
describe('Blog',function(){
it('get blogs', function(done){
const response = request(app).get('/postp');
expect(response.status).to.be.equal(200);
done();
})
})
What's the problem?
please specify, is it because I can't access app or something else.
why it's not running
Try using request from chai.
const chai = require('chai');
const request = require('supertest');
const app = require('../app');
const post = require('../router/post');
const expect = chai.expect;
describe('Blog',function(){
it('get blogs', function(done){
const response = chai.request(app).get('/postp')
.end((err, res) => {
res.should.have.status(200);
})
done();
})
})
I suggest you to check this link, it gives more details about assertion lib and chai.

Server instances, jest and 'listen EADDRINUSE :::3000'

Am new to jest, node and express, and am having a problem with testing my app.
The actual code seems to be working - it's just when passing the server instance to each of the test files (user.test.js and genres.test.js) and running jest, the port is being blocked. I assume it's because I'm creating duplicate instances of the server in each test file, without realising it.
Running jest with the flag --runInBand works, and so does only using one test file, but this doesn't help me understand exactly what's going on.
I've posted example code below, but I'm struggling to cut it down, however I do think most of the code is irrelevant, and it's just down to how I'm passing the server to each file.
Again, apologies for the length, but I think it should be very basic for anyone but me! Thanks.
index.js (.)
const express = require('express');
const app = express();
const genres = require('./routes/genres');
const users = require('./routes/users');
app.use(express.json());
app.use('/api/genres', genres);
app.use('/api/users', users);
const port = process.env.PORT || 3000;
const server = app.listen(port, () => console.log(`Listening on port ${port}...`));
console.log(typeof server);
// export server to be used in test file
module.exports = server;
genres.js (./routes)
const express = require('express');
const router = express.Router();
router.post('/', async (req, res) => {
res.send('post genre ok');
});
module.exports = router;
users.js (./routes)
const express = require('express');
const router = express.Router();
router.post('/', async (req, res) => {
res.send('post user ok');
});
module.exports = router;
genres.test.js (./tests)
const request = require('supertest');
let server;
describe('auth tests', () => {
const exec = async function(){
return await request(server)
.post('/api/genres');
};
beforeEach(() => {
server = require('../index');
});
afterEach(async () => {
await server.close();
});
describe('POST /', () => {
it('should return 200', async () => {
const res = await request(server).post('/api/genres');
expect(res.status).toBe(200);
});
});
});
user.test.js (./tests)
const request = require('supertest');
let server;
describe('user tests', () => {
const exec = async function(){
return await request(server)
.post('/api/user');
};
beforeEach(() => {
server = require('../index');
});
afterEach(async () => {
await server.close();
});
describe('POST /', () => {
it('should return 200', async () => {
const res = await request(server).post('/api/users');
expect(res.status).toBe(200);
});
});
});
Hopefully this (point 2) helps others with this question
It has worked for me, by splitting the app from the server. I'm not sure if this is the right approach, and I'm not 100% sure why it works with the app rather than the server, but all my tests are now passing.
index.js is now app.js:
const express = require('express');
const app = express();
const genres = require('./routes/genres');
const users = require('./routes/users');
app.use(express.json());
app.use('/api/genres', genres);
app.use('/api/users', users);
// export server to be used in test file
module.exports = app;
The server is separated into another file:
const app = require('./app');
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on port ${port}...`));
and then the test files import the app rather than the server. therefore each test doesn't create its own instance.
NB: I think - I don't really know how correct this is, but as mentioned, it works

Chai Unit Testing Error

I am trying to write a unit test for the following file named a.js
var express = require('express');
var router = express.Router();
router.get('/getHCQ', function (req, res) {
res.send("OK");
});
module.exports = router;
Unit test file : a.spec.js
var chai = require('chai');
var chaiHttp = require('chai-http');
var should = chai.should();
var a = require('./a');
describe('Unit Test', function () {
it('should run this', function (done) {
chai.request(a)
.get('/getHCQ')
.end(function(err, res) {
res.should.have.status(200);
done();
});
});
});
I am running it using Mocha but getting the following error :
Uncaught TypeError: Cannot read property 'apply' of undefined
at Immediate.<anonymous> (node_modules/express/lib/router/index.js:635:14)
Can someone please help me to figure out what am I doing wrong here?
Perhaps you forgot to configure chai with chai-http
var chai = require('chai');
var chaiHttp = require('chai-http');
var should = chai.should();
chai.use(chaiHttp); // <-- add this
var a = require('./a');
describe('Unit Test', function () {
it('should run this', function (done) {
chai.request(a)
.get('/getHCQ')
.end(function(err, res) {
res.should.have.status(200);
done();
});
});
});

Mocha and Chai GET request test doesn't work

I'm trying to test the GET request on the home root '/'. The route doesn't exist in my file root.js because
I want that the test fails but it always succeeds.
I don't understand why. There are 3 files: ./test/test.js, ./server/server.js and ./server/routes/index.js
/*File: ./test/test.js */
/* eslint-env mocha */
const chai = require('chai')
const chaiHttp = require('chai-http')
const { expect } = chai
const app = require('../server/server.js')
chai.use(chaiHttp)
describe('GET Home', () => {
it('Should return Not found', () => {
chai
.request(app)
.get('/', (err, res) => {
const result = res.statusCode
expect(result).to.equal(200)
})
})
})
/*File: ./server/server.js */
const express = require('express')
const bodyParser = require('body-parser')
const config = require('./config.js')
const mainRoutes = require('./routes')
const app = express()
app.set('view engine', 'jade')
app.use(bodyParser.urlencoded({ extended: false }))
app.use(mainRoutes)
app.listen(config.port, () => {
console.log(`The application is running on localhost:${config.port}`)
})
module.exports = app
/*File: ./server/routes/index.js*/
const express = require('express')
const router = express.Router()
// router.get('/', (req, res) => {
// res.render('home')
// })
module.exports = router
This is because get run async, you need to call done() for assertion expect(result).to.equal(200) to get checked, otherwise your test will always passed no matter your router exists or not. Checkout this page and search Caveat if you want to know more.
describe('GET Home', () => {
it('Should return Not found', (done) => {
chai
.request(app)
.get('/')
.end(function (err, res) {
const result = res.statusCode
expect(result).to.equal(200)
done()
})
})
})

Resources