dimanche 11 avril 2021

How do you mock a module-scoped variable in Jest?

Consider the following function:

let dictionary = {
  there: "there"
}

function sayHi(word){
  if (dictionary.hasOwnProperty(word)){
    return "hello " + dictionary[word] 
  }
}

If I wanted to test the sayHi function, how would I mock the dictionary variable in a Jest test? I've tried importing everything from the module and overwriting the dictionary object but that hasn't worked, likewise I've tried mocking it as a function, but still can't get it to work. Any help would be really appreciated.

Thanks.

Aucun commentaire:

Enregistrer un commentaire