3
1

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でdataProviderを使う

Last updated at Posted at 2021-01-03

やりたいこと

Jestでテストをする際に、テスト内容は同じで与えられるデータだけが異なる場合に、重複した処理を書かずに済ませたい。

PHPUnitでテストを書いている時に使ったことのある、data providerという仕組みがJestにもないか調べてみた。

実装

以下のように、describe.each()を使えばdata providerと同じようなことができる。

interface Datum {
  hoge: string,
}

const data: Datum[] = [
  {
    hoge: 'hoge'
  },
  .
  .
  .
]

describe.each(data)('hogeのユニットテスト', (datum: Datum) => {
  const { hoge } = datum

  it(`${hoge}の場合。`, () => {
    // テストの実装
  })
})

参考記事

Using data provider pattern for javascript unit tests

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?