Pretty print json unit test error results with Jasmine




If you use the standard HTML reporter for jasmine an expect like:


would result in:



If you replace the div tag with a pre tag in the “jasmine-html.js” file.




and use the third paramater of the JSON.stringify method to pretty print the JSON,


expect(JSON.stringify(actual, null, 4)).toEqual(JSON.stringify(expected, null, 4));


the result will be:



In some cases this will simplify, spotting the error in the JSON result.

Jasmine 2.0 reporting html page.

Just an example HTML page for reporting Jasmine 2.0 tests.


<!doctype html>
<html lang="en">
<meta charset="UTF-8">
<title>Jasmine 2.0 example page.</title>
<link href="jasmine.css" rel="stylesheet" />
<script src="jasmine.js"></script>
<script src="jasmine-html.js"></script>
<script src="boot.js"></script>
// This is the function we will test.
function addOne(num) {
if (typeof num == "number") {
return num + 1
else {
return "not a number"
// These are the tests.
describe("addOne", function () {
it("should be a function", function () {
expect(typeof addOne).toEqual('function');
it("should be add one to an integer argument", function () {
describe("when argument is not a number", function () {
it("should return 'not a number' when argument is String", function () {
expect(addOne("this is a string")).toEqual("not a number");