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);
};
};
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 beundefined
. To fix this, you'll need to explicitly declare your exports:Second thing that should be fixed: in your
testUtilities
you declare a function with a strict mode inside where some utility methods are added tothis
, but this is going to work only if you call it with anew
keyword like a constructor:which arguably is not that elegant. Additionally, it would require always carrying about passing correct
this
towriteTitleToLog()
as it usesthis
to lookup forwriteToLog()
.To fix that,
testUtilities.js
could be rewritten to export a number of utility functions:And then usage of it would be exactly same as in your question:
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: