Node.js + Chai/Mocha/Should: running multiple tests against the same response - javascript

I'm using something very similar to the following to execute a series of API tests using Mocha. This is great, but it requires making a separate API call for each test. I want to use the same API call and run multiple tests against that response. I've been reading that you can use before to do it, but none of the examples around the web actually show it working with API calls?
var chai = require('chai');
var request = require('request');
var async = require('async');
var assert = chai.assert,
expect = chai.expect,
should = chai.should();
describe('/', function () {
it('should return 200', function (done) {
request.get('http://localhost:8000', function (err, res, body) {
res.should.have.status(200);
done();
});
});
it('should say "Hello, world!"', function (done) {
request.get('http://localhost:8000', function (err, res, body) {
body.should.have.property('type', 'aType');
done();
});
});
});

You could do this with a before function like so...
var chai = require('chai');
var request = require('request');
var async = require('async');
var assert = chai.assert,
expect = chai.expect,
should = chai.should();
describe('/', function () {
var firstRequest;
before(function(done) {
request.get('http://localhost:8000', function(err, res, body) {
firstRequest = {
err:err,
res:res,
body:body
};
done();
});
});
it('should return 200', function (done) {
firstRequest.res.should.have.status(200);
done();
});
it('should say "Hello, world!"', function (done) {
firstRequest.body.should.have.property('type','aType');
done();
});
});
However, unless you have a really good reason to do this, I think you're better off just combining the tests.
var chai = require('chai');
var request = require('request');
var async = require('async');
var assert = chai.assert,
expect = chai.expect,
should = chai.should();
describe('/', function () {
it('should return 200 and say "Hello, world!"', function (done) {
request.get('http://localhost:8000', function (err, res, body) {
res.should.have.status(200);
body.should.have.property('type', 'aType');
done();
});
});
});
If the test fails Mocha will report the specific reason why it failed even though there are two assertions.

Related

Jest await default error handler of express

Given the following setup:
const express = require("express");
const app = express();
app.get("/", function(req, res, next) {
// explicitly return an error
return next("my error");
});
// made this middleware for the example,
// solution should typically also work for express default error handling
app.use(function(error, req, res, next) {
if (error) {
res.status(500).send({ error });
throw new Error(error); // <- how to test this?
}
next();
});
app.listen(8080, function() {
console.log("server running on 8080");
}); //the server object listens on port 8080
And for the test:
const request = require("supertest");
const app = require("../../app.js");
const spy = jest.spyOn(global.console, "error").mockImplementation();
it("throws an error", async done => {
const res = await request(app).get("/");
expect(res.status).toBe(500);
expect(res.error.text).toContain("my error");
expect(spy).toHaveBeenCalled(); // nothing...
done();
});
Made a Codesandbox with this example code. Not sure how to run a node test in that though.
async shouldn't be used with done, this results in test timeout in case done() cannot be reached.
First of all, error handler shouldn't re-throw an error, unless it's reusable router instance that is supposed to be augmented with another handler. If it's the last one in a row, it should catch both synchronous and asynchronous errors that can happen inside of it.
The problem is that default error handler is triggered asynchronously so it should be specifically awaited:
it("throws an error", async () => {
const spy = jest.spyOn(global.console, "error");
const res = await request(app).get("/");
expect(res.status).toBe(500);
expect(res.error.text).toContain("my error");
await new Promise(resolve = > setTimeout(resolve));
expect(spy).not.toHaveBeenCalled(); // it really shouldn't
});
A more correct way to approach this is to make sure the error is handled:
it("throws an error", async () => {
const defaultErrorHandler = jest.fn((err, req, res, next) => {});
app.use(defaultErrorHandler);
const res = await request(app).get("/");
expect(res.status).toBe(500);
expect(res.error.text).toContain("my error");
expect(defaultErrorHandler).not.toHaveBeenCalled();
});

Unit testing using Mocha with Express Rest API

I'm learning unit testing. So far I was able to run simple tests like "Add two numbers and test if they are above 0", but I want to build a REST API using TDD. So far I have this:
My routes/index.js file:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function (req, res, next) {
res.send({val: true});
});
module.exports = router;
My index.test.js file:
var mocha = require('mocha');
var assert = require('chai').assert;
var index = require('../routes/index');
describe('Index methods', () => {
it('Returns true', done => {
index
.get('http://localhost:3000')
.end(function (res) {
expect(res.status).to.equal(200);
done();
})
})
});
I user a tutorial to do this, but when I try to run this I get:
TypeError: index.get(...).end is not a function
So I'm guessing there is something wrong, but have no idea what. That's my first day learning TDD so if you see anything stupid please let me know.
Doing this:
it('Returns true', done => {
var resp = index.get('http://localhost:3000/');
assert.equal(resp.val === true);
done();
})
Also results in an error:
AssertionError: expected false to equal undefined
1. Install the dev dependencies for mocha
chai: assertion library for node and browser,
chai-http: HTTP Response assertions for the Chai Assertion Library.
2. You need to export your server,
'use strict';
/*eslint no-console: ["error", { allow: ["warn", "error", "log"] }] */
const express = require('express');
const app = express();
//...
const config = require('config');
const port = process.env.PORT || config.PORT || 3000;
//....
app.listen(port);
console.log('Listening on port ' + port);
module.exports = app;
3. Write your tests as:
If your test script is users.spec.js,it should start by:
// During the rest the en variable is set to test
/* global describe it beforeEach */
process.env.NODE_ENV = 'test';
const User = require('../app/models/user');
// Require the dev-dependencies
const chai = require('chai');
const chaiHttp = require('chai-http');
// You need to import your server
const server = require('../server');
const should = chai.should();
// Set up the chai Http assertion library
chai.use(chaiHttp);
// Your tests
describe('Users', () => {
beforeEach((done) => {
User.remove({}, (err) => {
done();
});
});
/**
* Test the GET /api/users
*/
describe('GET /api/users', () => {
it('it should GET all the users', (done) => {
chai.request(server)
.get('/api/users')
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('array');
res.body.length.should.be.eql(0);
done();
});
});
});
// More test...
});
You can take a look at my repository, Github - Book Store REST API
const chai = require('chai');
const expect = require('chai').expect;
const chaiHttp = require('chai-http');
chai.use(chaiHttp);
first install chai
it('Returns true', done => {
return chai.request(index)
.get('/')
.then(function (res) {
expect(res.status).to.equal(200);
done();
})
})
var mocha = require('mocha');
var assert = require('chai').assert;
var index = require('./index');
var req = require('supertest');
describe('Index methods', () => {
it('Returns true', done => {
req(index)
.get('/')
.end(function (res) {
expect(res.status).to.equal(200);
done();
})
})
});
also in your terminal type npm i supertest --save-dev
simple test case to check if the server is running properly.
const chai = require('chai'),
chaiHttp = require('chai-http'),
server = require('../app'),
faker = require('faker'),
should = chai.should();
chai.use(chaiHttp);
describe('Init', function () {
it('check app status', function (done) {
chai.request(server).get('/').end((err, res) => {
should.not.exist(err);
res.should.have.status(200);
done();
})
});
});
Test Cases for get API
describe('/Get API test', function () {
it('Check the api without user id parameter', function (done) {
chai.request(server).get('/post-list').end((err, res) => {
should.not.exist(err);
res.should.have.status(401);
res.body.should.be.a('object');
res.body.should.have.property('message');
res.body.should.have.property('message').eql('User Id parameter is missing');
done();
})
});
it('Check the api with user id. Success', function (done) {
chai.request(server).get('/post-list?user_id=1').end((err, res) => {
should.not.exist(err);
res.should.have.status(200);
res.body.should.be.a('object');
res.body.should.have.property('userId');
res.body.should.have.property('title');
res.body.should.have.property('body');
done();
})
});
});
Test Case for Post API
describe('/POST API test', function () {
it('Check the api without parameters . failure case', function (done) {
chai.request(server).post('/submit-data').send({}).end((err, res) => {
should.not.exist(err);
res.should.have.status(401);
res.body.should.be.a('object');
res.body.should.have.property('message');
res.body.should.have.property('message').eql('Mandatory params are missing!');
done();
})
});
it('Check the API with valid parameters. Success', function (done) {
chai.request(server).post('/submit-data').send({name:faker.name.firstName(),email:faker.internet.email()}).end((err, res) => {
should.not.exist(err);
res.should.have.status(200);
res.body.should.be.a('object');
res.body.should.have.property('message');
res.body.should.have.property('message').eql('data saved successfully');
done();
})
});
});
Add test cases as per the different case available in your API.
Find here the basic terminologies and complete sample application to proceed: https://medium.com/#pankaj.itdeveloper/basics-about-writing-tests-in-nodejs-api-application-4e17a1677834

Change a predefined value on server before a functional test

I'm looking to change requestHandler.value to 5 for my functional styled tests.
When running the suite, creating 1000 documents in the db is not really an option, so is it possible to change it's value programmatically before running the suite and then reset it afterwards? I can create 5 documents in db before the test to work with.
Of coarse I can stub countDocumentsInDb() in unit tests to return what I need, but I've simplified logic below for the sake of the question.
app.js:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var requestHandler = require('./lib/requestHandler.js');
var PORT = 4000;
app.route('/validate')
.get(function(req, res) {
requestHandler.process(req, res);
});
if (!module.parent) {
server.listen(PORT, function(err) {
if (err) {
return;
}
});
}
module.exports = app;
requestHandler.js:
var requestHandler = {
value: 1000,
process: function(req, res) {
numberOfDocumentsInDb = countDocumentsInDb();
if (numberOfDocumentsInDb === this.value) {
res.send(true);
} else {
res.send(false);
}
}
};
module.exports = requestHandler;
FVT style test ..
var Promise = require('promise');
var request = require('supertest');
var chai = require('chai');
chai.should();
var server = require('../../app.js');
describe('app.js', function() {
describe('/validate', function() {
it('should return true if number of documents in db matches pre-defined value', function(done) {
var fvtPromise = new Promise(function(fulfill) {
request(server)
.get('/validate')
.expect(200)
.end(function(err, res) {
if (err) {
throw err;
}
res.body.should.equal(true);
fulfill(null);
});
});
fvtPromise.done(function() {
done();
});
});
});
});
You can play with the require.cache, that will allow you to modify the values of requestHandler.
Is just an example I hope you get the idea.
- In the before each modify the require cache and set your test values
-In the after each set back the original values
-Please notice that the const server = require('./app.js'); is inside the test, so it will take the cache vales
e.g.
describe('test with cache', function(){
require('./requestHandler');
let originalValues;
beforeEach(function() {
originalValues = require.cache[ require.resolve('./requestHandler') ].exports;
require.cache[ require.resolve('./requestHandler') ].exports = {
value:5,
process: function(req, res) {
//other stuff
}
};
});
afterEach(function() {
require.cache[ require.resolve('./requestHandler') ].exports = originalValues;
});
it('should pass', function(){
const server = require('./app.js');
var fvtPromise = new Promise(function(fulfill) {
request(server)
.get('/validate')
.expect(200)
.end(function(err, res) {
if (err) {
throw err;
}
res.body.should.equal(true);
fulfill(null);
});
});
fvtPromise.done(function() {
done();
});
expect(true).to.be.true;
});
});

Stubbing a constructor of a third party library when testing express app with Supertest

I've got a simple express app that looks like this:
var SendMandrillTemplate = require('send-mandrill-template');
var sendMandrillTemplate = new SendMandrillTemplate('api-key-goes-here');
var app = require('express')();
app.get('/', function(req, res, next) {
sendMandrillTemplate.sendTemplate(
'template-name-goes-here',
'email#here.com', {
value: 123
},
function(err) {
if (err) {
res.send('ERR - ', err)
} else
res.send('DONE')
});
});
module.exports = app;
I export the app object, so I can mount this in a separate server.js like this -
var app = require('./app')
app.listen(1234, function() {
console.log('Running on port 1234');
});
This is to enable me to use supertest a bit easier.
Here's my test so far:
var app = require('./app')
var request = require('supertest')
var SendMandrillTemplate = require('send-mandrill-template');
describe('GET /', function() {
var sendTemplateStub;
before(function() {
//I think i need to setup a spy on the created instance of SendMandrillTemplate.sendTemplate
//sendTemplateStub = //?
});
it('calls sendTemplate on sendMandrillTemplate instance', function(done) {
request(app)
.get('/')
.expect(200)
.end(function(err, res) {
if (err) throw err;
//assert sendTemplateStub was called with 'template-name-goes-here'
//etc...
done();
})
})
})
As you can see, I'm having trouble stubbing the SendMandrillTemplate constructor
If I wasn't newing up an instance of SendMandrillTemplate I could do something like:
sendTemplateStub = sinon.stub(SendMandrillTemplate, 'sendTemplate')
But of course, in this scenario this won't work...
You can get away with something as simple as
var SendMandrillTemplate = require('send-mandrill-template');
sinon.stub(SendMandrillTemplate.prototype, 'sendTemplate');

ChaiJS: Cannot cause test to fail

I am trying to get my second spec to fail but it won't. I am returning a statusCode 203 from my server, and the test receives it as 203, but for some reason the expect block still does not throw an error. What is going on here?
spec.js
var chai = require('chai');
var expect = chai.expect;
var request = require('request');
var localServerUri = 'http://127.0.0.1:3000/';
describe('server http routing', function(done){
it('should receive a status code 200 after GET request', function() {
request(localServerUri + 'pull', function (error, response, body) {
expect(response.statusCode).to.be.at(200);
done();
});
});
it('should receive a status code 201 after POST request', function() {
request.post({url:localServerUri + 'push', form: {key:'value'}}, function(err, response, body) {
//why wont this fail?
console.log(response.statusCode === 201);
expect(response.statusCode === 201).to.be.true;
done();
});
});
});
app.js
var express = require('express');
var app = express();
app.get('/pull', function (req, res) {
res.send('works');
});
app.post('/push', function(req, res) {
res.status(203);
res.send('works');
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at port', host, port);
});
You need to pass done into the it blocks, not describe.
var chai = require('chai');
var expect = chai.expect;
var request = require('request');
var localServerUri = 'http://127.0.0.1:3000/';
describe('server http routing', function(){
it('should receive a status code 200 after GET request', function(done) {
request(localServerUri + 'pull', function (error, response, body) {
expect(response.statusCode).to.be.at(200);
done();
});
});
it('should receive a status code 201 after POST request', function(done) {
request.post({url:localServerUri + 'push', form: {key:'value'}}, function(err, response, body) {
expect(true).to.be.true;
done();
});
});
});
It's pretty simple. The callbacks you pass to both of your it calls should be function (done) {... instead of function () {.... Right now, Mocha thinks these tests are synchronous, so it does not wait for them to complete.
The callback you pass to describe takes a done parameter but this is useless.

Categories