lundi 23 mars 2020

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

Aucun commentaire:

Enregistrer un commentaire