0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

jestで要素が存在しないことをテストしたい

Posted at

jestである条件下で表示されない要素があり、その場合に該当の要素が存在しないことをテストしたい。

初期のコード

.jsx
it ('テスト', () => {
  const sampleComponent = ~~;
  const sampleComponent.getByText('非表示アイテム');

  expect(sampleComponent).not.toBeInTheDocument();
});

これだと、getByTextでその要素は取得できないよって怒られてしまう。

変更したコード

.jsx
it ('テスト', () => {
  const sampleComponent = ~~;
  const sampleComponent.queryByText('非表示アイテム');

  expect(sampleComponent).not.toBeInTheDocument();
});

queryByTextを使った。
こうすればテストができる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?