from Jest Memo Jest Memo Day3 --- day 3
MissingAPIError: indexedDB API missing
- As with Firestore, the code that touches IndexeDB needs to be isolated and replaced by a mock.
- Last time, I did a mock where I cut out and exported the code after taking the values from Firestore as follows, and replaced getNewTalkID with a Promise that calls this directly ts
- Split Promise into three functions: read, use, and write, while specifying the type of Promise.
- Jest mocking does not affect calls within the same module, so letâs mock only the parts that touch IndexedDB by placing them in a separate module.
- User Module Mock [doc https://jestjs.io/docs/ja/manual-mocks#%E3%83%A6%E3%83%BC%E3%82%B6%E3%83%BC%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E 3%83%AB%E3%81%AE%E3%83%A2%E3%83%83%E3%82%AF]
- I see
- But Iâd like to see it implemented two ways. mocks/managePreviousTalkID.ts
- Top screen rendering test now works.
- If I donât put just.mock at the top level instead of in the test case, it doesnât mock as expected, why?
- Iâd like to test switching the behavior of multiple mocksâŚ
-
I have to put jest.mock at the top level instead of in the test case to get it mocked as expected, why?
-
docs: using ES module imports you would normally write import declarations at the top of your test files. However, you need to tell Jest to use mocks prior to the module using them. To do this, Jest automatically moves the jest.mock call to the beginning of the module (before you do the import).
- I thought, âShouldnât it be placed first?â but ESLint complains âImport in body of module; reorder to top.â
- I thought it would replace the imported one at the time of mock since it works afterwards, but that doesnât seem to be the case.
-
- I decided to do spyOn on top of it, since spyOn would certainly replace it at that time.
- The test that the menu display is different depending on whether previousTalkID exists or not worked for a while.
- However, I donât think I can determine if the menu actually doesnât exist or if the asynchronous redrawing has not yet finished.
- I could wait until it times out to make a decision, but I donât want to do that because it would slow down the test. ts
- Wrapping in act in React tests is not render but state update
- When updating the state with the result of a Promise, it is not possible to wrap the entire Promise in an act
Next time âWhy not replace useState with a mock?â
This page is auto-translated from [/nishio/JestăĄă˘ Day3](https://scrapbox.io/nishio/JestăĄă˘ Day3) using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. Iâm very happy to spread my thought to non-Japanese readers.