mardi 14 janvier 2020

How to mock a global variable in a class

I have a class file, someClass.js that looks like:

//assigns globalVar from some file
const globalVar = getFromFile()

export default class SomeClass {
  ...
  public static someFunc() {
     if (globalVar == "Hello"){
       return 2
     }
     else {
       return 3
     }
  }

And in my test file, I'm importing it as so:

import SomeClass from './someClass';

In my tests I want to overwrite the globalVar with different values for each test like this:

test('call someFunc with globalVar set to Hello', async () => {
  //set globalVar to "Hello"--how can I do this?
  const result = SomeClass.someFunc()
  expect(result).toBe(2)
}

How can I do this? Is there any way to mock a const?

Aucun commentaire:

Enregistrer un commentaire