Currently we are using Jasmine for our unit tests and are in the beginning phases of migrating our whole application into Webpack. One challenge is that some of our old code grabs functions/constructors out of the window object using a string name that lives in a config. So as workaround until we can completely re-factor that code, I created a collection of the exports application-wide and use this to grab functions using a string name.
That said, what I would like to do is write a unit test that is able to:
Ensure the number of exports in Webpack matches the length of my modules collection.
Ensure that the names of functions listed in my modules collection match the names of functions that have been exported (not the alias, but actual function names).
Ensures that all in the collection are defined (not an issue currently).
Here is a truncated example of the modules collection, which builds after an event triggered once all modules are loaded:
let _classList;
window.EventEmitter.once('modulesLoaded', () => {
_classList = {
'MyClass': require('./scripts/MyClass.js').default,
'MyClass2': require('./scripts/MyClass2.js').default
};
});
export default class modules {
static get(name) {
return _classList && _classList[name];
}
//for unit tests to ensure that all modules are available to use
static allAreDefined() {
if (!_classList) {
return false;
}
for (var cls in _classList) {
if (_classList.hasOwnProperty(cls)) {
if (!_classList[cls]) {
return false;
}
}
}
return true;
}
static getLength() {
return Object.keys(_classList).length;
}
}
Ideally, I would like to write unit tests similar to this:
it('All in the collection are defined', function () {
expect(modules.allAreDefined()).toBe(true);
});
//Hmm... how to do I do this?
it('Collection length matches exports length', function () {
expect(modules.getLength() === ???.length).toBe(true);
});
//This also...?
it('The collection includes an entry for every export', function () {
expect(
(function() {
for(let i = 0; i < ???.length; i++) {
if(!modules.get(???[i].name)) {
return false;
};
}
return true;
}())
).toBe(true);
});