-1

I have this

jest.spyOn(store.customerData, "isValid", "get").mockReturnValue(true);

that I'd like to refactor to use a typical jest.fn() mock like this:

store.customerData.isValid = jest.fn().mockResolvedValue(true)

but I get an error claiming that the property is read only.

Lin Du
  • 88,126
  • 95
  • 281
  • 483
Luís Soares
  • 5,726
  • 4
  • 39
  • 66

1 Answers1

0

Check for the ObjectProperties of your method.

If the configurable attribute is set to true, you can try using Object.defineProperty():

Object.defineProperty(store['customerData'], 'isValid', {writable: true});
// And then put your code
store.customerData.isValid = jest.fn().mockResolvedValue(true);

If it's set to false, I'm afraid I don't have any solution. I had a problem close to yours a few months ago (that I did not resolve) and I asked a question on SO, you can check what I tried, see if anything works for you: Override Object Property for unit testing purposes

Reyedy
  • 918
  • 2
  • 13
  • 36