I'm trying to test a function that reads in a file and returns a promise with the file's contents.
function fileContents(){
return new Promise(function(resolve, reject) {
fs.readFile(filename, function(err, data){
if (err) { reject(err); }
else { resolve(data); }
});
});
}
The unit test for the above
describe('Testing fileContents', function () {
afterEach(function () {
fs.readFile.restore();
});
it('should return the contents of the fallBack file', function () {
let fileContents = '<div class="some-class">some text</div>';
sinon.stub(fs, 'readFile').returns(function(path, callback) {
callback(null, fileContents);
});
let fileContentsPromise = fileContents();
return fileContentsPromise
.then(data => {
expect(data).to.eventually.equal(fileContents);
});
});
The above test errs with
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
I also tried
describe('Testing fileContents', function () {
afterEach(function () {
fs.readFile.restore();
});
it('should return the contents of the fallBack file', function (done) {
let fileContents = '<div class="some-class">some text</div>';
sinon.stub(fs, 'readFile').returns(function(path, callback) {
callback(null, fileContents);
});
let fileContentsPromise = fileContents();
fileContentsPromise.then(function(data){
expect(data).to.equal(fileContents);
done();
});
});
and got the same error. The function is working in my local site, but I don't know how to write a test for it. I'm new to js. What am I missing?
There are multiple problems with your code. For instance, you redeclare fileContents in your test and assign it a string value, which of course won't work with doing fileContents() in the same test. I'm going to concentrate on two conceptual problems rather than the "duh"-type mistakes like this one.
The two conceptual problems are:
To have fs.readFile call your callback with fake values you must use .yields. Using .returns changes the return value, which you do not use. So stub it like this:
sinon.stub(fs, 'readFile').yields(null, fakeContents);
You are using the .eventually functionality provided by chai-as-promised on a non-promise, but you have to use it on a promise for it to work properly so your test should be:
return expect(fileContentsPromise).to.eventually.equal(fakeContents);
Here's code that works:
const sinon = require("sinon");
const fs = require("fs");
const chai = require("chai");
const chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
const expect = chai.expect;
// We need to have filename defined somewhere...
const filename = "foo";
function fileContents(){
return new Promise(function(resolve, reject) {
fs.readFile(filename, function(err, data){
if (err) { reject(err); }
else { resolve(data); }
});
});
}
describe('Testing fileContents', function () {
afterEach(function () {
fs.readFile.restore();
});
it('should return the contents of the fallBack file', function () {
let fakeContents = '<div class="some-class">some text</div>';
sinon.stub(fs, 'readFile').yields(null, fakeContents);
let fileContentsPromise = fileContents();
return expect(fileContentsPromise).to.eventually.equal(fakeContents);
});
});
Related
I want to just make sure that my server is running before I create PR's locally to test that nothing is broken.
worker.js
My server exports a promise
module.exports = Promise.all([
hbsPartialsLoaded,
])
.then(() => {
let server;
if (!isTestEnvironment) {
server = app.listen(config.port);
}
return { server, app };
});
tests
let { expect, request } = require('chai')
.use(require('chai-http'))
.use(require('chai-cheerio'));
const config = require('../../app/config');
const worker = require('../../app/worker');
describe('server response', function () {
worker.then(() => {
before(function () {
server.listen(config.port);
});
it('should return 200', function (done) {
request.get(`http://localhost:${config.port}`, function (err, res, body){
expect(res.statusCode).to.equal(200);
done();
});
});
after(function () {
worker.close();
});
})
});
Running Tests
NODE_ENV=test mocha --timeout 20000 --recursive test/ --compilers js:babel-core/register
0 passing (1ms)
My tests are never run. How can I get it so I wait for sever.js to finish its promises before the tests are run?
You didn't export the promise correctly, so the calling is bad, because you may not get callback if the require("worker.js") finished before calling .then, it should be worker().then not worker.then, but that's not the only problem.
The worker.js should return a function that launches the Promise.all. So change the module.exports = Promise.all... to module.exports = () => Promise.all...
Use before to launch the server and call it synchronously. Inside it you can use the asynchronous functions as you want.
The server.listen has a callback on second argument, that tells you when it finishes, so you can use promise and resolve it there.
An example:
var expect = require('chai').expect;
describe('x', () => {
before(() => new Promise((resolve, reject) => setTimeout(resolve, 500)));
it('should work', (done) => setTimeout(() => expect(0).to.be.above(0), 500));
});
Better example for your case:
var expect = require('chai').expect;
var app = require('express')();
describe('x', () => {
before(() => new Promise((resolve, reject) => {
app.listen(3000, () => {
console.log('The server launched');
resolve();
});
}));
it('should work', (done) => {
console.log('I got called after the server got launched! :)');
setTimeout(() => expect(1).to.be.above(0), 500)
});
});
it() functions should be called directly inside describe(). Nested inside a promise doesn't read well nor follow the standard testing process
let { expect, request } = require('chai')
.use(require('chai-http'))
.use(require('chai-cheerio'));
const config = require('../../app/config');
const worker = require('../../app/worker');
describe('server response', function () {
before(function () {
server.listen(config.port);
});
it('should return 200', function (done) {
worker.then(() => {
request.get(`http://localhost:${config.port}`, function (err, res, body){
expect(res.statusCode).to.equal(200);
done();
});
})
});
after(function () {
worker.close();
});
});
I've been trying to test the following code using Mocha, but I always get the error.
Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test
The code I want to test is as follows.
'use strict'
const Promise = require('bluebird');
const successResponse = {status: 'OK'};
const failResponse = {status: 'FAIL'};
function dbStatusSuccess () {
return new Promise(function(resolve, reject) {
setTimeout(() => {
resolve(successResponse);
}, 2010);
});
}
function dbStatusFail () {
return new Promise(function(resolve, reject) {
setTimeout(() => {
reject(failResponse);
}, 2000);
});
}
module.exports = {
dbStatusSuccess,
dbStatusFail
}
and here are my tests.
'use strict'
const Promise = require('bluebird');
const chai = require('chai')
chai.use(require('chai-string'))
chai.use(require('chai-as-promised'));
const expect = chai.expect;
chai.should();
const healthyCheck = require('./healthyCheck');
const resp = {status:'OK'};
const resp2 ={status: 'FAIL'};
describe('healthy-check end point', () => {
it('should return successful response when connected to database', () => {
return healthyCheck.dbStatusSuccess()
.then((res) => {
console.log(JSON.stringify(res, undefined, 2));
return expect(res).to.equal(resp);
}).catch( (err) => {
console.log(err);
return expect(err).to.deep.equal(resp2);
});
});
});
I also get an error { AssertionError: expected { status: 'OK' } to equal { status: 'OK' } in the console. Which I believe is from loggin the err from the .catch function.
EDIT 1.
Removed the reject function from dbStatusSuccess function.
The issue is with the promises taking 2 seconds to complete/fail. If the time set in setTimeout is less than 2 seconds, the tests will pass.
The default timeout in your test seems to be 2000ms. Your code obviously takes longer to complete. Therefore, you have to up the timeout limit. As noted here you shouldn't use arrow functions so you can safely access this.
Then you can increase your timeout like so:
'use strict'
const Promise = require('bluebird');
const chai = require('chai')
chai.use(require('chai-string'))
chai.use(require('chai-as-promised'));
const expect = chai.expect;
chai.should();
const healthyCheck = require('./healthyCheck');
const resp = {status:'OK'};
const resp2 ={status: 'FAIL'};
describe('healthy-check end point', () => {
it('should return successful response when connected to database', function() {
this.timeout(3000);
return healthyCheck.dbStatusSuccess()
.then((res) => {
console.log(JSON.stringify(res, undefined, 2));
return expect(res).to.equal(resp);
}).catch( (err) => {
console.log(err);
return expect(err).to.deep.equal(resp2);
});
});
});
Then your test should run as expected.
'use strict'
const Promise = require('bluebird');
const chai = require('chai');
chai.use(require('chai-string'));
chai.use(require('chai-as-promised'));
const expect = chai.expect;
chai.should();
const healthyCheck = require('./healthyCheck');
describe('healthy-check end point', function() {
it('should return successful response when connected to database', function(done) {
const resp = {status: 'OK'};
healthyCheck.dbStatusSuccess()
.then((res) => {
console.log(JSON.stringify(res, undefined, 2));
expect(res).to.equal(resp);
done();
}).catch(done);
});
});
In code example I don't return promise, so I need use callback. Usage callback in your async tests helps avoid Error: timeout of 2000ms exceeded
Don't use arrow function in descibe and it. More info
You should use the done callback, for example:
it('reads some file', function(done) {
fs.readFile('someFile.json', function(err, data) {
if (err) return done(err);
assert(data != null, "File should exist.");
done();
});
});
What's happening is the test ('it' function) returns before your promise resolves; using done means the test won't finish until you call done() when the promises resolves.
See
http://tobyho.com/2015/12/16/mocha-with-promises/
and
https://mochajs.org/#working-with-promises
Well, I just found the problem, your test is tricky.
You set the timeout timer to 2010ms but Mocha default execution time is 2000ms, so you will always get the error from Mocha.
I still think you shouldn't create .catch block in the returned promise chain, it will stop the promise chain to propagate.
describe('healthy-check end point', () => {
it('should return successful response when connected to database', () => {
return healthyCheck.dbStatusSuccess()
.then((res) => {
console.log(JSON.stringify(res, undefined, 2));
return expect(res).to.equal(resp);
});
}).timeout(2500); //tell Mocha to wait for 2500ms
});
I have this piece of code:
function getMsg() {
return new Promise(function (resolve, reject) {
var input = [];
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function (cmd) {
if (cmd.trim()) {
input.push(cmd);
} else {
rl.close();
}
});
rl.on('close', function () {
rl.close();
resolve(input.join('\n'));
});
rl.on('SIGINT', reject);
});
}
I'm trying to test this function, my attempt, so far, is this:
it('should reject if SIGINT is sent', function () {
sandbox.stub(readline, 'createInterface', function () {
return {
on: function (action, callback) {
callback();
},
prompt: function () {},
close: function () {}
};
});
return getMsg().then(null).catch(function () {
expect(true).to.be.equal(true);
});
});
But of course, that doesn't simulate a SIGINT, how do I do this?
I think you need a different setup:
const EventEmitter = require('events').EventEmitter
...
it('should reject if SIGINT is sent', function () {
let emitter = new EventEmitter();
sandbox.stub(readline, 'createInterface', function () {
emitter.close = () => {};
return emitter;
});
let promise = getMsg().then(function() {
throw Error('should not have resolved');
}, function (err) {
expect(true).to.be.equal(true);
});
emitter.emit('SIGINT');
return promise;
});
The object returned by readline.createInterface() inherits from EventEmitter, so that's what the stub will return. The additional close function is simply added to it to prevent errors when it gets called.
You can't return the promise returned by getMsg directly, because that doesn't give you a chance to emit the SIGINT "signal" (really just an event, but for testing purposes it'll work just fine). So the promise is stored.
The test should fail with the "fulfilled" handler gets called, which had to be done explicitly, otherwise Mocha thinks the test passed.
Next, the SIGINT is sent (which should trigger the rejection handler as intended), and the promise is returned.
I am using pg-promise. i am learnner please excuse if it seems trivial to you. how can i wrtie unit test for. it errors out data is undefined. i have been making connection in js file and export that module.another js file use to query against database and fetch result set. code is working as expected having issue how can i write unit test with mocha and chai.
test1.js
var dbConn= pgp(connUrl);
module.exports = {
getconnect: function () {
return dbConn;
}
};
test2.js
module.exports = {
getData: function (req, res) {
db.getconnect().query(sqlStr, true)
.then(function (data) {
console.log("DATA:", data);
return data;
} } }
unittest.js
describe("Test Cases", function (done) {
it('retrieve response', function (done) {
var req = {};
var res = {};
test2.getData(req, res);
// how would i retrieve value of data from test2.js so i can test
done();
});
});
how would i retrieve "data" value from test2.js inthe unittest.js
Your getData must return the promise. Client code will be able to recognize the moment it's finished(resolved).
module.exports = {
getData: function (req, res) {
return db.getconnect().query(sqlStr, true)
.then(function (data) {
console.log("DATA:", data);
return data;
} } }
test:
describe("Test Cases", function () {
it('retrieve response', function (done) {
var req = {};
var res = {};
test2.getData(req, res).then(function(data){
// test of data returned
done(); // finish test
}).catch(done);// report about error happened
});
});
If you don't need to any process of data in your module, you can to remove whole .then section without any functionality changes.
But if you want to preprocess data - don't forget to return it from each chained .then.
If your test library requires stubs for async stuff, you can use async/await feature to deal with it.
it('retrieve response', async function(){
try {
var data = await test2.getData(req, res);
// test data here
} catch (e) {
// trigger test failed here
}
});
Or stub it, something like this:
var dbStub = sinon.stub(db, 'getConnect');
dbStub.yields(null, {query: function(){/*...*/}});
If you have a function returning promise then you can use await in your tests:
describe("Test Cases", function (done) {
it('retrieve response', async function (done) {
try {
var data = await test2.getData();
// check data constraints ...
...
} catch(err) {
...
}
done(); // finish test
});
});
I'm trying to add some mocha testing to a node module I have, but I'm new to it, and my lack of concreteness in terms of callbacks is hampering me.
I have tried to pare things back to the most straightforward example, but it's still not working.
So my main.js is
var async = require('async');
var myObject = {};
myObject.test = function(params) {
async.waterfall([
async.apply(test, params)
],
function(err, result){
if (err) {
console.log('Error: ' + err);
} else {
if (result === 200) {
return result;
}
}
});
};
function test(params, callback) {
if(params) {
callback(null, 200);
}
}
module.exports = myObject;
Then my test file
var assert = require("assert");
var myObject = require('./main');
describe('test', function(){
it("should return 200", function(done){
myObject.test({test: 'such test'}, function(err, res) {
if (err) return done(err);
assert.equal(res, 200);
done(res);
});
})
});
If I just run mocha it times out so I'm suspicious about that! Trying mocha --timeout 15000 also just stalls. Any direction you can provide would be really appreciated!
I got this far using this answer but can't get any further.
OK, I think I sorted it, but would still appreciate some feedback to see if I'm approaching it correctly, rather than just managing to get my test to pass.
var async = require('async');
var myObject = {};
myObject.test = function(params, callback) {
async.waterfall([
async.apply(test, params)
],
function(err, result){
if (err) {
console.log('Error: ' + err);
} else {
if (result === 200) {
callback(result);
}
}
});
};
function test(params, callback) {
if(params) {
callback(null, 200);
}
}
module.exports = myObject;
and the test file is
var assert = require("assert");
var myObject = require('./main');
describe('test', function(){
it("should return 200", function(done){
myObject.test({test: 'such test'}, function(res) {
assert.equal(res, 200);
done();
});
})
});
You fixed your main issue but your code still is broken. When you have an async method that takes a callback, you must always invoke the callback exactly once in all cases or your program's control flow will break. If you write an if/else clause, both branches must invoke the callback function. Both of your if statements above violate the callback contract. Check out understanding error-first-callbacks from The Node Way for a good explanation.