0

I have a plain JS file that is written with pre ES6 JS code that I want to test using a karma/jasmine test suite. The file is large but I just want to get a super simple example working.

// pluginV3.js
// This is the file I am trying to test. It is written in pre ES6 JS and has no exports
var myvar = null;

I have found a library called rewire that I think will allow me to test plain JS files like this but I am having trouble getting it to work with my karma/jasmine stack. So far I have tried this:

// var rewire = require("rewire"); // Can't use require. Test code is also pre ES6
beforeEach(module('rewire')); // I have been trying to instance "rewire" here using the exported module in rewire's index.js  

describe('V3 Helper.js', function() {
    describe('$pluginV3.myvar', function () {
        fit('Tests a certain variable in pluginV3 is set to an expected initial value', function () {
            var pluginV3 = rewire("../../../../public/static/js2/pluginV3.js");
            expect(pluginV3.myvar).toEqual(null); // expect this scope var to be something
        });
    });
});

But I get this error:

LOG: '----------------------------------------------------'
Chrome 96.0.4664.55 (Mac OS 10.15.7): Executed 0 of 30 SUCCESS (0 secs / 0 secs)
LOG: 'What is rewire?'
Chrome 96.0.4664.55 (Mac OS 10.15.7): Executed 0 of 30 SUCCESS (0 secs / 0 secs)
LOG: function rewire(filename) { ... }
Chrome 96.0.4664.55 (Mac OS 10.15.7): Executed 0 of 30 SUCCESS (0 secs / 0 secs)
Chrome 96.0.4664.55 (Mac OS 10.15.7) CN Plugin Client V3 Helper $scope.transferData Tests a certain variable in Plugin Client V3 is set to an expected initial value FAILED
    TypeError: rewireModule is not a function
        at rewire (node_modules/rewire/lib/index.js:11:12)
        at UserContext.<anonymous> (tests/helpers/cn-plugin-client-v3-test.js:25:28)
        at <Jasmine>
Chrome 96.0.4664.55 (Mac OS 10.15.7): Executed 1 of 30 (1 FAILED) (0 secs / 0.002 secs)
Chrome 96.0.4664.55 (Mac OS 10.15.7) CN Plugin Client V3 Helper $scope.transferData Tests a certain variable in Plugin Client V3 is set to an expected initial value FAILED
    TypeError: rewireModule is not a function
        at rewire (node_modules/rewire/lib/index.js:11:12)
        at UserContext.<anonymous> (tests/helpers/cn-plugin-client-v3-test.js:25:28)
        at <Jasmine>

Which makes me think the beforeEach(module('rewire')); is working but but for some reason it is not able to run rewireModule (perhaps because it is ES6+)? Here is rewire's index.js:

var rewireModule = require("./rewire.js");

/**
 * Adds a special setter and getter to the module located at filename. After the module has been rewired, you can
 * call myModule.__set__(name, value) and myModule.__get__(name) to manipulate private variables.
 *
 * @param {!String} filename Path to the module that shall be rewired. Use it exactly like require().
 * @return {*} the rewired module
 */
function rewire(filename) {
    return rewireModule(module.parent, filename);
}

module.exports = rewire;

delete require.cache[__filename];   // deleting self from module cache so the parent module is always up to date

Which is what I imported in my karmaconfig file along with the file I am trying to test:

// karma.conf.js
files: [
            './node_modules/rewire/lib/index.js',
            '../../public/static/js2/cn-plugin-client-v3.js',
    ]

Am I doing something wrong? Or am I just unable to use rewire with my pre ES6 restriction. Has anyone tested pre ES6 JS files that have no exports with karma/jasmine? Is there a different solution?

Rosey
  • 739
  • 1
  • 12
  • 27

1 Answers1

0

Turns out rewire was unnecessary. After you add the file to your karma config you can usually instance it in a test by calling it as a module:

beforeEach(module('pluginV3'));

The only drawback to this approach is you need to satisfy every dependancy. If there is a variable in pluginV3.js that comes from pluginV2.js or something, you will need to add that to the karma file imports before the file that depends on it.

I have not tested this much but I imagine this solution is still inadequate for files that have unrealistically long dependency chains and I have no idea how you would handle files with circular dependancies.

Rosey
  • 739
  • 1
  • 12
  • 27