Ghost/core/test/unit/errorHandling_spec.js

124 lines
3.6 KiB
JavaScript
Raw Normal View History

2013-05-26 22:51:58 +04:00
/*globals describe, beforeEach, it*/
var should = require('should'),
when = require('when'),
sinon = require('sinon'),
errors = require('../../server/errorHandling'),
// storing current environment
currentEnv = process.env.NODE_ENV;
2013-05-26 22:51:58 +04:00
describe("Error handling", function () {
2013-05-26 22:51:58 +04:00
// Just getting rid of jslint unused error
should.exist(errors);
2013-05-26 22:51:58 +04:00
it("throws error objects", function () {
var toThrow = new Error("test1"),
runThrowError = function () {
errors.throwError(toThrow);
};
2013-05-26 22:51:58 +04:00
runThrowError.should['throw']("test1");
});
2013-05-26 22:51:58 +04:00
it("throws error strings", function () {
var toThrow = "test2",
runThrowError = function () {
errors.throwError(toThrow);
};
2013-05-26 22:51:58 +04:00
runThrowError.should['throw']("test2");
});
2013-05-26 22:51:58 +04:00
it("throws error even if nothing passed", function () {
var runThrowError = function () {
errors.throwError();
};
2013-05-26 22:51:58 +04:00
runThrowError.should['throw']("An error occurred");
});
2013-05-26 22:51:58 +04:00
it("logs errors", function () {
var err = new Error("test1"),
logStub = sinon.stub(console, "log");
// give environment a value that will console log
process.env.NODE_ENV = "development";
errors.logError(err);
// Calls log with message on Error objects
logStub.calledWith("Error occurred: ", err.message).should.equal(true);
2013-05-26 22:51:58 +04:00
logStub.reset();
2013-05-26 22:51:58 +04:00
err = "test2";
2013-05-26 22:51:58 +04:00
errors.logError(err);
2013-05-26 22:51:58 +04:00
// Calls log with string on strings
logStub.calledWith("Error occurred: ", err).should.equal(true);
2013-05-26 22:51:58 +04:00
logStub.restore();
process.env.NODE_ENV = currentEnv;
});
2013-05-26 22:51:58 +04:00
it("logs promise errors with custom messages", function (done) {
var def = when.defer(),
prom = def.promise,
logStub = sinon.stub(console, "log");
2013-05-26 22:51:58 +04:00
// give environment a value that will console log
process.env.NODE_ENV = "development";
prom.then(function () {
throw new Error("Ran success handler");
}, errors.logErrorWithMessage("test1"));
prom.otherwise(function () {
logStub.calledWith("Error occurred: ", "test1").should.equal(true);
2013-05-26 22:51:58 +04:00
logStub.restore();
done();
});
prom.ensure(function () {
// gives the environment the correct value back
process.env.NODE_ENV = currentEnv;
});
def.reject();
});
2013-05-26 22:51:58 +04:00
it("logs promise errors and redirects", function (done) {
var def = when.defer(),
prom = def.promise,
req = null,
res = {
redirect: function () {
return;
}
},
logStub = sinon.stub(console, "log"),
redirectStub = sinon.stub(res, "redirect");
// give environment a value that will console log
process.env.NODE_ENV = "development";
prom.then(function () {
throw new Error("Ran success handler");
}, errors.logErrorWithRedirect("test1", "/testurl", req, res));
prom.otherwise(function () {
logStub.calledWith("Error occurred: ", "test1").should.equal(true);
logStub.restore();
2013-05-26 22:51:58 +04:00
redirectStub.calledWith('/testurl').should.equal(true);
redirectStub.restore();
2013-05-26 22:51:58 +04:00
done();
2013-05-26 22:51:58 +04:00
});
prom.ensure(function () {
// gives the environment the correct value back
process.env.NODE_ENV = currentEnv;
});
def.reject();
2013-05-26 22:51:58 +04:00
});
});