React + Jest + jsdomでfetchが絡むテストを実行するとTypeErrorが出る

node_modules/jsdom/lib/jsdom/browser/Window.js:376
      return idlUtils.wrapperForImpl(idlUtils.implForWrapper(window._document)._location);
                                                                              ^

TypeError: Cannot read properties of null (reading '_location')

React + Jestでテストを書いているとこういうエラーが出た。スタックトレースを見ると、mswjsから来てる。 問題切り分けてると非同期処理(fetch)があるときに、waitForで再描画待たずにテストを終わらせるとこれが出るっぽい。


追記

これが出るときもあれば出ないときもある不安定な状態になった・・・


さらに追記

`whatwg-fetch` requests don't clear when server is `close()`d, leading to an error · Issue #1684 · mswjs/msw · GitHub

これを踏んでたっぽい。自分しか使わないプロダクトなので一旦

node v18 で導入された global fetch を jsdom 環境下の jest で利用する方法 - Qiita

これで回避することにした。