Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

testing json.parse with mocha

testing json.parse with mocha


why does the following code not pass when using Mocha for testing?

my example module:

module.exports = function(){
  JSON.parse("this is not json")

and my test.js:

var should = require("should")
var module = require("./module")

describe("error testing", function(){
  it("should throw an error", function(done){

I expect the test to pass but running mocha gives me the following:

✖ 1 of 1 tests failed:

1) error testing should throw an error:
   SyntaxError: Unexpected token h
    at Object.parse (native)
    at module.js:9:8
    at Context. (test.js:6:5)
    at (/usr/local/lib/node_modules/mocha/lib/runnable.js:145:15)
    at Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:270:10)
    at /usr/local/lib/node_modules/mocha/lib/runner.js:314:12
    at next (/usr/local/lib/node_modules/mocha/lib/runner.js:198:14)
    at /usr/local/lib/node_modules/mocha/lib/runner.js:207:7
    at next (/usr/local/lib/node_modules/mocha/lib/runner.js:157:23)
    at Array.0 (/usr/local/lib/node_modules/mocha/lib/runner.js:175:5)
    at EventEmitter._tickCallback (node.js:192:40)
Problem courtesy of: zemirco


should.throw() expects to be called on a Function, not the result of a function. Change this:


to this:

Solution courtesy of: jrajav


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

testing json.parse with mocha


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription