UNB/ CS/ David Bremner/ teaching/ cs2613/ labs/ Lab 9

Before the lab

Background


Jasmine

There are several unit testing frameworks for JavaScript; we will be using one of the more popular ones called Jasmine.

Getting Started with Jasmine

Time
10 minutes
Activity
Demo
    describe("identity",
         function() {
             it("1 === 1", function() { expect(1).toBe(1); });
             it("null === null", function() { expect(null).toBe(null); })
         });

    describe("arithmetic",
         function() {
             it("1 + 1 === 2", function() { expect(1 + 1).toBe(2); });
             it("6 * 7  === 42", function() { expect(6*7).toBe(42); });
         });

Making a test module

Time
20 minutes
Activity
Individual work
    let arith=require ("../loop-arith.js");

    describe("add",
         function() {
            it("1 + 1 = 2",
                function() {
                    expect(                ).toBe(2);
                });
             it("0 + x = x",
                function() {

                });
        });

    describe("mult",
         function() {
             it("0 * 2 = 0",   );

             it("1 * 2 = 2",
                function() {
                    expect(arith.mult(1,2)).toBe(   );
                });
             it("2 * 2 = 4", );
         });

Test Coverage

Time
20 Minutes
Activity
Demo, individual work
function minus(a,b) {
    for (let i=0; i < a; i++){
        b--;
    }
    return b;
}

Node Debugger

Time
15 minutes
Activity
Demo
let arith=require("./loop-arith.js");

console.log(arith.minus(10,0));

Debugging using Chromium DevTools

JavaScript Objects

Time
10 minutes
Activity
Demo / discussion

JavaScript objects are quite different from Java objects; in particular it is possible (and idiomatic) to use JavaScript objects without any concept of class. JavaScript objects are conceptually similar to (mutable) hash tables in Racket.

> x={ apple: 1}
> x['pear']=2
> x
> x.apple === x['apple']

The last line demonstrates so called property notation that we have already been using. A method in JavaScript is just a property that is assigned to a function

> obj = {}
> obj.hello = function () { console.log("hello world\n") }
> obj.hello()
> obj['hello']()
< obj.add1 = (x) => x + 1

JSON

Time
25 minutes
Activity
Individual work

Assignment 3 includes a question about parsing JSON. The syntax should look more familiar now that we have studied JavaScript a bit. Since we're using node.js, we can reasonably simply read the JSON from a file.

let fs = require('fs');

function read_json_file(filename) {
    let contents = fs.readFileSync(filename);

    return JSON.parse(contents);
}
exports.read_json_file=read_json_file;
let read_json_file=require("./read_json_file.js").read_json_file;

let data = null;

function by_name(name){
    if (data===null)
        data=read_json_file("ancestry.json");
    // simple linear scan

    return null;

}
exports.by_name=by_name;

The use of a module local variable here to cache data is roughly equivalent to a class variable in Java. It's not particularly flexible, and real code would likely use an object to cache data for a given file.

Here are some tests that your function should pass.

let by_name=require("../ancestry.js").by_name;
describe("by_name", function() {
    it("not-present", function() {
        expect(by_name("ronald mcdonald")).toBe(null);
        expect(by_name("Captain Ahab")).toBe(null);
    });
    it("first", function () {
        expect(by_name("Carolus Haverbeke")).toEqual({"name": "Carolus Haverbeke", "sex": "m", "born": 1832, "died": 1905, "father": "Carel Haverbeke", "mother": "Maria van Brussel"});
    });
    it("last", function () {
        expect(by_name("Jacobus Bernardus van Brussel")).toEqual({"name": "Jacobus Bernardus van Brussel", "sex": "m", "born": 1736, "died": 1809, "father": "Jan van Brussel", "mother": "Elisabeth Haverbeke"});
    });
    it("middle", function () {
        expect(by_name("Joanna de Pape")).toEqual(  {"name": "Joanna de Pape", "sex": "f", "born": 1654, "died": 1723, "father": "Vincent de Pape", "mother": "Petronella Wauters"});
    });

});

On your own: Debugging Jasmine Tests

Time
20 Minutes
Activity
Individual work, after the lab

Jasmine is itself a node.js program, so we need to debug tests a bit differently. Here are some hints. Try it out on one of your test files from this lab.

node --inspect-brk /usr/lib/nodejs/jasmine/bin/jasmine.js spec/loop-arith.spec.js