0

I'm trying to test a method that contains a reference to a global variable containing some configs taken from a json file.

async function methodToTest(var1, var2, var3) {
    const functionName = 'methodToTest';
    
    //guards here
    
    try {

        if ((var1 === 0 || var2 === 0 || var3 < config.prop1.pro1_child) {
            return false;
        }

        return true;
}

And this is it's test, being serviceConfig the required config json file:

const rewire = require('rewire');
const fileToBeTested = rewire(`${ __base }methods/fileContainingMethodToTest`);

    describe('Testing methodToTest', function () {
        it('Should return false if var1 = 0 or var2 = 0 or var3 < configured value', async function () {
            const var1 = '0';
            const var2 = '1';
            const var3 = '400';

            const expectedResponse = false;

            //Create a stub for the configs file
            configStub = sinon.stub(serviceConfig, 'config').resolves(serviceConfig)

            const result = await fileToBeTested.methodToTest(var1, var2, var3);
            expect(result).to.be.equal(expectedResponse);
        });
    }

This is the conf.json file that "feeds" the config variable in the method that requires testing:

{
"p1":"test1",
"p2":"test2",
"prop1": { "pro1_child": "2000" }
...
}

But I get TypeError: Cannot stub non-existent own property config What could I be doing wrong?

These are the modules I'm using:

"mocha": "^5.0.5",
"rewire": "^4.0.1",
"sinon": "^4.5.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1"
Eunito
  • 416
  • 5
  • 22

1 Answers1

0

I eventualy figured out that if I defined a global variable "config" I could continue the tests. Can be closed

Eunito
  • 416
  • 5
  • 22