mercredi 13 mars 2019

Jest - Mock a constant property from a module for a specific test

So, I'm attempting to do something which on the surface should be very simple...

I have some constants defined in: ` //constants.js

module.exports = {
 MY_CONSTANT: "TEST"
}

` I have a file which I'm trying to test which has a branching statement like this:

`

//file to test
//...

if(CONSTANTS.MY_CONSTANT === "TEST")
{...}
...

`

And I have a test like this: `

//test


 it("Should do something when MY_CONSTANT === "TEST, () => {
    //This is fine as it is exported as TEST
    })


 it("Should do something else when MY_CONSTANT !== "TEST, () => {
    //This seems annoyingly difficult to get working...
    })

`

I've tried this - With no luck, it doesn't change the actual value

I've tried changing the constant export to export an object instead (that didn't work)

I've tried adding a jest.mock(..) for the constants in my test file and doing an unmock in the tests I don't need them mocked.

I've tried adding a jest.doMock(...) within the test function I need to change the value. (along with jest.resetModules and another require)

I've tried adding a jest.doMock(...) to a tests beforeEach (along with jest.resetModules and another require)

I'm at a loss really...literally all I want to do is change a property value before a test runs 😂

Aucun commentaire:

Enregistrer un commentaire