1

Hello I was wondering is there a way to inject two mockContexts like in the solution here https://stackoverflow.com/a/55013256/8197395 but with two. Thanks I tried to add two with different names via useContext but unless it is called mockContext Jest doesn't recognise it.

Zotov
  • 265
  • 1
  • 8
  • 20
  • You're not forced to use `mockContext`. It doesn't matter how the variable is named, but it should start with `mock` prefix. See https://jestjs.io/docs/en/es6-class-mocks#calling-jestmockdocsenjest-objectjestmockmodulename-factory-options-with-the-module-factory-parameter – Estus Flask Feb 11 '21 at 16:13
  • Okay, my bad, but if I try to add a second context it doesn't work... – Zotov Feb 11 '21 at 16:18
  • If I can nest them this would be perfect! – Zotov Feb 11 '21 at 16:26
  • There's no specific problems associated with contexts in tests. The question isn't clear enough what's the problem. Jest is totally unaware of contexts and React in general. If it's specific to Enzyme's shallow rendering, you probably shouldn't use it for this. – Estus Flask Feb 11 '21 at 17:47
  • As for the answer you listed, useContext mock is supposed to be conditional, `jest.fn(Context => if (Context === MyContext) return ... else return ...)` – Estus Flask Feb 11 '21 at 17:56
  • @Zotov, could you please tell how did you implement the same? Thanks – Surbhi Jain Feb 10 '23 at 04:51

0 Answers0