adding a utility namespace in JasmineJS

42 Views Asked by At

I'm starting to modify a JasmineJS test suite, and there are some logging calls I want to clean up. Specifically I want to add a utilities.js file in the directory of the test spec... and reference it throughout all my test specs.

There are now two test specs testSuite1.spec.js and testSuite2.spec.js, and one page.js which has a bunch of routines.

It seems this doesn't work. Any idea why?

testSuite1.spec.js

var page = require('./page.js');
var testUtilities = require('./testUtilities.js');
var params = browser.params;

describe("App dashboard", function () {

browser.ignoreSynchronization = true;
var params = browser.params;

describe("login", function () {

    it("should go to login page", function () {
        testUtilities.writeTitleToLog('Login');
        testUtilities.writeToLog("resizing window and going to home page");

testUtilities.js

var testUtilities = function () {
    "use strict";

    this.writeToLog = function (message, isTitleMessage) {
        if (typeof message !== 'string') {
            console.log(Date() + " TestSpec -- WARNING: messaged was not a string");
        }
        if (isTitleMessage) {
            console.log("\n" + Date() + " TestSpec - " + message.toUpperCase() + '\n');
        } else {
            console.log(Date() + " TestSpec —- " + message);
        }
    };

    this.writeTitleToLog = function (message) {
        this.writeToLog(message, true);
    };
};
1

There are 1 best solutions below

0
On BEST ANSWER

First of all, from the code you've posted it looks like testUtilities.js doesn't export anything. Without that, testUtilities in your specs are going to be undefined. To fix this, you'll need to explicitly declare your exports:

exports.testUtilities = function () {
    "use strict";
     //...
};

Second thing that should be fixed: in your testUtilities you declare a function with a strict mode inside where some utility methods are added to this, but this is going to work only if you call it with a new keyword like a constructor:

var testUtilities = new (require('./testUtilities.js'))();

which arguably is not that elegant. Additionally, it would require always carrying about passing correct this to writeTitleToLog() as it uses this to lookup for writeToLog().

To fix that, testUtilities.js could be rewritten to export a number of utility functions:

"use strict";

function writeToLog(message, isTitleMessage) {
    if (typeof message !== 'string') {
        console.log(Date() + " TestSpec -- WARNING: messaged was not a string");
    }
    if (isTitleMessage) {
        console.log("\n" + Date() + " TestSpec - " + message.toUpperCase() + '\n');
    } else {
        console.log(Date() + " TestSpec —- " + message);
    }
}

function writeTitleToLog(message) {
    writeToLog(message, true);
}

module.exports = {
    writeToLog: writeToLog,
    writeTitleToLog: writeTitleToLog
};

And then usage of it would be exactly same as in your question:

var testUtilities = require('./testUtilities.js');
describe("something", function () {
    testUtilities.writeTitleToLog("Something is being tested");
    it("should be ok to call test utilities", function () {
        testUtilities.writeToLog("Executing sanity check");
        expect(true).not.toBe(false);
    });
});

Or if you already use ES2015 syntax in your project, it could be simplified (in my opinion) with destructuring assignment's syntax sugar to this:

const {writeToLog, writeTitleToLog} = require('./testUtilities.js');
describe("something", function () {
    writeTitleToLog("Something is being tested");
    it("should be ok to call test utilities", function () {
        writeToLog("Executing sanity check");
        expect(true).not.toBe(false);
    });
});