Pretty print json unit test error results with Jasmine

 

 

 

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

expect(JSON.stringify(actual)).toEqual(JSON.stringify(expected));

would result in:

 

image

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

image

 

 

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

like:

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

 

the result will be:

image

 

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">
<head>
<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>
</head>
<body>
<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 () {
expect(addOne(1)).toEqual(2);
});
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");
});
})
})
</script>
</body>
</html>

image