4

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?

niemelsa
  • 125
  • 2
  • 5
  • 1
    Been googling this for a while, got excited when I read a question that matches my exact scenario, then my heart broke when I saw it's ghosted 3 – TheLebDev Dec 19 '22 at 07:18

0 Answers0