ReactでTest時にWarning出てた
フロントエンド開発のためのテスト入門 今からでも知っておきたい自動テスト戦略の必須知識で書かれてたテストコードでwarning出てた。
コード
code:typescript
test("「利用規約の同意」チェックボックスを押下すると「サインアップ」ボタンは活性化", async () => {
render(<Form />);
await user.click(screen.getByRole("checkbox"));
expect(screen.getByRole("button", { name: "サインアップ" })).toBeEnabled();
});
Warning文
code:javascript
console.error
Warning: An update to Form inside a test was not wrapped in act(...).
When testing, code that causes React state updates should be wrapped into act(...):
act(() => {
/* fire events that update state */
});
/* assert on the output */
This ensures that you're testing the behavior the user would see in the browser. Learn more at https://reactjs.org/link/wrap-tests-with-act
調べた
actでラップしろって言われてるけどラップしても変わらない。
waitFor使ったりしてみても特に変わらなかった。
もう一つのリポジトリでやってる時はエラー出てなくてよく分からん。
調べてみたら、こっちも14になってた。
解決方法
@testing-library/reactのバージョンを13.4.0 → 14.0.0にアップデートしたら治った
React 18. set state in finally throws "act" warning, though test is passing
ちゃんとみてないけど、ここで話してるのと多分おんなじことやと思われる。
みたもの
React のテストを書いてたら act で囲んでよーって言われたとき