I have a brownfield application with a large number of infamously global javascript files. As an example, I have files like this that are loaded on the various html pages:
var Class = {
create: function() {
return function () { this.initialize.apply(this, arguments); };
}
};
var Dialog = Class.create();
Dialog.id = "dvDialogLayer";
function getPopupTop (){
var win = window.top;
}
So I'd like to put these methods and objects under test as a first step to replacing them with a more modern pattern. I've installed Jasmine and Jasmine-Node and wrote a test like this, following the Jasmine style guide:
const Jasmine = require('jasmine');
const Subj = require('../../Scripts/dialog');//path to the dialog.js file
describe('Global Dialog script test', () => {
beforeEach(() => {
});
it('should create the Dialog class', () => {
expect(Dialog).not.toBe(undefined);
})
it('should return the expected height for a dialog popup', () => {
expect(getPopupTop().toBe(0));
});
});
If I run the jasmine command, I get informed that the Dialog, Subjct, and getPopupTop are all undefined . I've been searching for the proper way to set this up, but haven't had any luck yet. Any idea what I could be doing wrong here and what the proper approach would be?
Here is my guess. You're trying to use CommonJS node modules, whereas that JavaScript code is built for the browser and doesn't "export" anything. So you can't use "require".
You need to setup the module and exports properly.
It could be as simple as adding
module.exports = ..., and you just export everything you need, but legacy code is often wrapped in an IIFE which could cause some extra difficulties with exporting.