Search code examples
javascripttestingmocha.jssinon

mocha access variable in file which function i test


Okay i will try to explain this simple. So i have this file that has this variable and then the function i am testing

let config: Config|undefined;

export default async function onConfigEvent(event: myEvent) {

  if (isDefined(config)) {
    console.log('Ignoring config event because we already have the config.');
    return;
  }

  config = event.config as Config;

  if (!config.firstThing) {
    console.log('config miss first thing.')
    return;
  }

  if (!config.otherthing) {
    console.log('config missing second thing.');
    return;
  }
}

Then i try to test these two negative if like this

describe('OnConfigEvent', () => {

  it('should log missing second thing', () => {

    let event: ConfigEvent = {
      type: events.Config,
      config: { ["firstThing"]: false }
    }

    let spy = sinon.spy(console, 'log');

    onConfigEvent(event)

    assert(spy.calledWith('Missing first thing.'));

    spy.restore();
  });

  it('should log missing second thing', () => {

    let event: ConfigEvent = {
      type: events.Config,
      config: { ["firstThing"]: true }
    }
    let spy = sinon.spy(console, 'log');

    onConfigEvent(event)

    assert(spy.calledWith('config missing second thing.'));

    spy.restore();
  });
});

Problem here is that after first test has been run the second one will return the first if statement "Ignoring config event because we already have the config." as the config gets set during the first test. How do i access the let gameConfig from the file i am testing a function in. so i can set it to undefined before every test


Solution

  • You can use rewire to reset the value of config variable.

    index.ts:

    type Config = any;
    type myEvent = any;
    let config: Config | undefined;
    
    function isDefined(obj) {
      return obj !== undefined;
    }
    
    export default function onConfigEvent(event: myEvent) {
      if (isDefined(config)) {
        console.log('Ignoring config event because we already have the config.');
        return;
      }
    
      config = event.config as Config;
    
      if (!config.firstThing) {
        console.log('config miss first thing.');
        return;
      }
    
      if (!config.otherthing) {
        console.log('config missing second thing.');
        return;
      }
    }
    

    index.test.ts:

    import sinon from 'sinon';
    import { assert } from 'chai';
    import rewire from 'rewire';
    type ConfigEvent = any;
    const events = { Config: 'Config' };
    
    describe('OnConfigEvent', () => {
      let onConfigEvent;
      let mod;
      beforeEach(() => {
        mod = rewire('./');
        onConfigEvent = mod.default;
      });
      afterEach(() => {
        mod.__set__({ config: undefined });
      });
      it('should log missing first thing', () => {
        let event: ConfigEvent = {
          type: events.Config,
          config: { ['firstThing']: false },
        };
        let spy = sinon.spy(console, 'log');
        onConfigEvent(event);
        assert(spy.calledWith('config miss first thing.'));
        spy.restore();
      });
    
      it('should log missing second thing', () => {
        let event: ConfigEvent = {
          type: events.Config,
          config: { ['firstThing']: true },
        };
        let spy = sinon.spy(console, 'log');
        onConfigEvent(event);
        assert(spy.calledWith('config missing second thing.'));
        spy.restore();
      });
    });
    

    unit test results with coverage report:

      OnConfigEvent
    config miss first thing.
        ✓ should log missing first thing
    config missing second thing.
        ✓ should log missing second thing
    
    
      2 passing (1s)
    
    ----------|---------|----------|---------|---------|-------------------
    File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
    ----------|---------|----------|---------|---------|-------------------
    All files |   83.33 |    66.67 |     100 |   83.33 |                   
     index.ts |   83.33 |    66.67 |     100 |   83.33 | 11,12             
    ----------|---------|----------|---------|---------|-------------------