Override Jest global property that's set in setupTests.js for specific tests (CRA, Jest, RTL)

Viewed 496

I've set up some global properties inside my setupTests.js for mocking window properties that are needed for my tests.

// setupTests.js

global.CONFIG = {
  property1: "something",
  property2: "something"
};

global.DATA = {
  property1: "something",
  property2: "something"
};

This works fine, but the problem is that I can't seem to override these globals inside specific tests. Ideally, I would like to re-set these properties inside specific tests like this:

// some.test.js

it('should have different global property for this test case', async () => {
  global.DATA = {
    property1: "changed",
    property2: "changed"
  }

  renderWithProviders(<SomeComponent />);
  
  ...
}

Most examples of mocking globals I've found have been about mocking methods, so they don't seem to work for me. (https://stackoverflow.com/a/40460395/13968820) This answer gives me the impression that this should be a working way to do it, but it doesn't work. What can I do to make these globals available to all my tests, and also be able to override them when needed?

0 Answers
Related