diff --git a/helpers/json.js b/helpers/json.js index c9086ed5..7525a0c1 100644 --- a/helpers/json.js +++ b/helpers/json.js @@ -2,6 +2,9 @@ const factory = () => { return function(data) { + if(typeof data === 'undefined') { + throw new Error("Handlebars Helper 'json' does not allow value of 'undefined'"); + } return JSON.stringify(data); }; }; diff --git a/spec/helpers/json.js b/spec/helpers/json.js index f96b7886..cde24515 100644 --- a/spec/helpers/json.js +++ b/spec/helpers/json.js @@ -2,11 +2,15 @@ const Lab = require('lab'), lab = exports.lab = Lab.script(), describe = lab.experiment, it = lab.it, - testRunner = require('../spec-helpers').testRunner; + expect = require('code').expect, + testRunner = require('../spec-helpers').testRunner, + buildRenderer = require('../spec-helpers').buildRenderer, + RenderError = require('../../index').errors.RenderError; describe('json helper', function() { const context = { - object: { a: 1, b: "hello" } + object: { a: 1, b: "hello" }, + undef: undefined }; const runTestCases = testRunner({context}); @@ -19,4 +23,18 @@ describe('json helper', function() { }, ], done); }); + + it('should fail when provided undefined', function(done) { + const render = buildRenderer(); + + render.renderString('{{{json undef}}}', context).then((result) => { + return Promise.resolve(null); + }, (reason) => { + expect(reason).to.be.an.instanceof(RenderError); + return Promise.resolve(reason.message); + }).then((reason) => { + expect(reason).to.equal("Handlebars Helper 'json' does not allow value of 'undefined'"); + done(); + }); + }); });