mardi 17 décembre 2019

node.js function testinv scope versus it's module scope

Consider following bits of code with tests created "retroactively" (i.e. after code was actually written and deployed):

Module we are going to test (file.js):

const config = require('./../config');

module.exports = {
    checkFeatureFlagInternal(flagName) {
        return config.featureFlags[flagName]
    }

And the test are (test.js):

const envHelpers = require('./../helpers/env-helper');

function getMockFF() {
    return {
        featureFlags: {
            trueFF: true,
            falseFF: false,
        }
    };
}

let config;

// eslint-disable-next-line no-undef
describe('env-helper #checkFeatureFlagInternal()', function() {

    before(() => {
        config = getMockFF();
    });

    it('Shall return true for existing true feature flag', function() {
        expect(envHelpers.checkFeatureFlagInternal('trueFF')).to.be.true;
    });
});

During the test execution, when checkFeatureFlagInternal is invoked inside test.js, the config variable is taken from file.js, not the mocked one from test.js. So the questions:

  • Does this issue implies, that scope of parameters is too narrow and actual signature shall looks like (flagName, configToCheck): boolean not (flagName): boolean to make it testable against mock configurations?
  • What would be preferred and most technically correct way of bypassing this issue?
  • I'm adding mocha tag, because maybe context object of it is suitable for usage?

Aucun commentaire:

Enregistrer un commentaire