Skip to content

Commit e7877ec

Browse files
committed
Trnslate testing-recipes
1 parent 42b7358 commit e7877ec

File tree

2 files changed

+53
-53
lines changed

2 files changed

+53
-53
lines changed

content/docs/nav.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@
128128
- title: Testing
129129
items:
130130
- id: testing
131-
title: Testing Overview
131+
title: テスト概要
132132
- id: testing-recipes
133-
title: Testing Recipes
133+
title: テストのレシピ集
134134
- id: testing-environments
135135
title: Testing Environments
136136
- title: Contributing

content/docs/testing-recipes.md

+51-51
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
---
22
id: testing-recipes
3-
title: Testing Recipes
3+
title: テストのレシピ集
44
permalink: docs/testing-recipes.html
55
prev: testing.html
66
next: testing-environments.html
77
---
88

9-
Common testing patterns for React components.
9+
React コンポーネントのための一般的なテストのパターン集です。
1010

11-
> Note:
11+
> 補足:
1212
>
13-
> This page assumes you're using [Jest](https://jestjs.io/) as a test runner. If you use a different test runner, you may need to adjust the API, but the overall shape of the solution will likely be the same. Read more details on setting up a testing environment on the [Testing Environments](/docs/testing-environments.html) page.
13+
> このページではテストランナーとして [Jest](https://jestjs.io/) を使用することを前提としています。もし別のテストランナーを使う場合は API を修正する必要があるかもしれませんが、やり方の全体的な見た目についてはおそらく同じようなものになるはずです。テスト環境のセットアップについては [テスト環境](/docs/testing-environments.html) のページをご覧ください。
1414
15-
On this page, we will primarily use function components. However, these testing strategies don't depend on implementation details, and work just as well for class components too.
15+
このページでは主に関数コンポーネントを利用します。しかし以下に述べるテスト手法は実装の詳細には依存しませんので、クラスコンポーネントでも全く同様に動作します。
1616

17-
- [Setup/Teardown](#setup--teardown)
17+
- [セットアップ/後始末](#setup--teardown)
1818
- [`act()`](#act)
19-
- [Rendering](#rendering)
20-
- [Data Fetching](#data-fetching)
21-
- [Mocking Modules](#mocking-modules)
22-
- [Events](#events)
23-
- [Timers](#timers)
24-
- [Snapshot Testing](#snapshot-testing)
25-
- [Multiple Renderers](#multiple-renderers)
26-
- [Something Missing?](#something-missing)
19+
- [レンダー](#rendering)
20+
- [データの取得](#data-fetching)
21+
- [モジュールのモック化](#mocking-modules)
22+
- [イベント](#events)
23+
- [タイマー](#timers)
24+
- [スナップショットテスト](#snapshot-testing)
25+
- [複数のレンダラ](#multiple-renderers)
26+
- [何かが足りない?](#something-missing)
2727

2828
---
2929

30-
### Setup/Teardown {#setup--teardown}
30+
### セットアップ/後始末 {#setup--teardown}
3131

32-
For each test, we usually want to render our React tree to a DOM element that's attached to `document`. This is important so that it can receive DOM events. When the test ends, we want to "clean up" and unmount the tree from the `document`.
32+
それぞれのテストにおいて、通常われわれは React ツリーを `document` に結びついた DOM 要素として描画することになります。これは DOM イベントを受け取れるようにするために重要です。テストが終了した際に「クリーンアップ」を行い、`document` からツリーをアンマウントします。
3333

34-
A common way to do it is to use a pair of `beforeEach` and `afterEach` blocks so that they'll always run and isolate the effects of a test to itself:
34+
このためによく行うのは `beforeEach` `afterEach` ブロックのペアを使い、あるテストの副作用がそれ自身にとどまるようにすることです。
3535

3636
```jsx
3737
import { unmountComponentAtNode } from "react-dom";
@@ -51,13 +51,13 @@ afterEach(() => {
5151
});
5252
```
5353

54-
You may use a different pattern, but keep in mind that we want to execute the cleanup _even if a test fails_. Otherwise, tests can become "leaky", and one test can change the behavior of another test. That makes them difficult to debug.
54+
他のパターンを利用しても構いませんが、*仮にテストが失敗した場合でも*クリーンアップコードを実行するようにするべき、ということを覚えておいてください。さもなくば、テストは「穴の開いた」ものとなってしまい、あるテストが他のテストの挙動に影響するようになってしまいます。これはデバッグを困難にします。
5555

5656
---
5757

5858
### `act()` {#act}
5959

60-
When writing UI tests, tasks like rendering, user events, or data fetching can be considered as "units" of interaction with a user interface. React provides a helper called `act()` that makes sure all updates related to these "units" have been processed and applied to the DOM before you make any assertions:
60+
UI テストを記述する際、レンダー、ユーザイベント、データの取得といったタスクはユーザインターフェースへのインタラクションの「ユニット ("unit")」であると考えることができます。React が提供する `act()` というヘルパーは、あなたが何らかのアサーションを行う前に、これらの「ユニット」に関連する更新がすべて処理され、DOM に反映されていることを保証します。
6161

6262
```js
6363
act(() => {
@@ -66,19 +66,19 @@ act(() => {
6666
// make assertions
6767
```
6868

69-
This helps make your tests run closer to what real users would experience when using your application. The rest of these examples use `act()` to make these guarantees.
69+
これによりあなたのテストは、実際のユーザがアプリケーションを使う時に体験するのと近い状況で実行されるようになります。以下の例ではこのような保証を得るために `act()` を利用しています。
7070

71-
You might find using `act()` directly a bit too verbose. To avoid some of the boilerplate, you could use a library like [React Testing Library](https://testing-library.com/react), whose helpers are wrapped with `act()`.
71+
直接 `act()` を使うのはちょっと冗長だと感じるかもしれません。ボイラープレートの記述を軽減するために、[React Testing Library](https://testing-library.com/react) のようなライブラリを使うこともできます。このライブラリのヘルパは `act()` でラップされています。
7272

73-
> Note:
73+
> 補足:
7474
>
75-
> The name `act` comes from the [Arrange-Act-Assert](http://wiki.c2.com/?ArrangeActAssert) pattern.
75+
> `act` という名称は [Arrange-Act-Assert](http://wiki.c2.com/?ArrangeActAssert) パターンから来ています。
7676
7777
---
7878

79-
### Rendering {#rendering}
79+
### レンダー {#rendering}
8080

81-
Commonly, you might want to test whether a component renders correctly for given props. Consider a simple component that renders a message based on a prop:
81+
与えられた props に対してコンポーネントが正しくレンダーされているかをテストしたいことがよくあります。props の内容に応じてメッセージをレンダーする以下のシンプルなコンポーネントを考えてください。
8282

8383
```jsx
8484
// hello.js
@@ -94,7 +94,7 @@ export default function Hello(props) {
9494
}
9595
```
9696

97-
We can write a test for this component:
97+
このコンポーネントのテストは以下のように書くことができます:
9898

9999
```jsx{24-27}
100100
// hello.test.js
@@ -139,9 +139,9 @@ it("renders with or without a name", () => {
139139

140140
---
141141

142-
### Data Fetching {#data-fetching}
142+
### データの取得 {#data-fetching}
143143

144-
Instead of calling real APIs in all your tests, you can mock requests with dummy data. Mocking data fetching with "fake" data prevents flaky tests due to an unavailable backend, and makes them run faster. Note: you may still want to run a subset of tests using an ["end-to-end"](/docs/testing-environments.html#end-to-end-tests-aka-e2e-tests) framework that tells whether the whole app is working together.
144+
あなたのテスト内で本物の API を呼ぶかわりに、リクエストをモック化してダミーのデータを返すようにできます。データ取得をモック化して「フェイク」のデータを使うことで、バックエンドが利用できないせいでテストが不安定にならずに済み、テストの動作が高速になります。とはいえ、テストの一部については、アプリケーション全体が協調して動作しているかを確認できる ["end-to-end"](/docs/testing-environments.html#end-to-end-tests-aka-e2e-tests) のフレームワークを利用して実行したいかもしれません。
145145

146146
```jsx
147147
// user.js
@@ -175,7 +175,7 @@ export default function User(props) {
175175
}
176176
```
177177

178-
We can write tests for it:
178+
このコンポーネントに対するテストは以下のように書けます:
179179

180180
```jsx{23-33,44-45}
181181
// user.test.js
@@ -228,11 +228,11 @@ it("renders user data", async () => {
228228

229229
---
230230

231-
### Mocking Modules {#mocking-modules}
231+
### モジュールのモック化 {#mocking-modules}
232232

233-
Some modules might not work well inside a testing environment, or may not be as essential to the test itself. Mocking out these modules with dummy replacements can make it easier to write tests for your own code.
233+
モジュールによってはテスト環境でうまく動かないものや、テスト自体にとって本質的でないものがあります。これらのモジュールをモック化してダミーに置き換えることで、あなた自身のコードに対するテストが記述しやすくなることがあります。
234234

235-
Consider a `Contact` component that embeds a third-party `GoogleMap` component:
235+
サードパーティーの `GoogleMap` を埋め込んでいる `Contact` というコンポーネントがあるとします:
236236

237237
```jsx
238238
// map.js
@@ -271,7 +271,7 @@ function Contact(props) {
271271
}
272272
```
273273

274-
If we don't want to load this component in our tests, we can mock out the dependency itself to a dummy component, and run our tests:
274+
我々のテスト中でこのコンポーネントをロードしたくない場合、この依存モジュール自体をダミーのコンポーネントにモック化した上でテストを実行することができます:
275275

276276
```jsx{10-18}
277277
// contact.test.js
@@ -337,9 +337,9 @@ it("should render contact information", () => {
337337

338338
---
339339

340-
### Events {#events}
340+
### イベント {#events}
341341

342-
We recommend dispatching real DOM events on DOM elements, and then asserting on the result. Consider a `Toggle` component:
342+
DOM 要素に対して本物の DOM イベントをディスパッチし、その結果に対してアサーションを行うことをお勧めします。以下の `Toggle` コンポーネントを考えてみましょう:
343343

344344
```jsx
345345
// toggle.js
@@ -362,7 +362,7 @@ export default function Toggle(props) {
362362
}
363363
```
364364

365-
We could write tests for it:
365+
これに対するテストは以下のように書けます:
366366

367367
```jsx{13-14,35,43}
368368
// toggle.test.js
@@ -416,17 +416,17 @@ it("changes value when clicked", () => {
416416
});
417417
```
418418

419-
Different DOM events and their properties are described in [MDN](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent). Note that you need to pass `{ bubbles: true }` in each event you create for it to reach the React listener because React automatically delegates events to the document.
419+
その他の DOM イベントやそれらのプロパティは [MDN](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent) で解説されています。React のリスナにイベントが到達するように、作成するそれぞれのイベントに対して `{ bubbles: true }` を指定する必要があることに気を付けてください。React ではイベントは自動的に document にデリゲートする形で処理されるためです。
420420

421-
> Note:
421+
> 補足:
422422
>
423-
> React Testing Library offers a [more concise helper](https://testing-library.com/docs/dom-testing-library/api-events) for firing events.
423+
> React Testing Library にはイベントを発火するための[より簡便なヘルパ](https://testing-library.com/docs/dom-testing-library/api-events)があります。
424424
425425
---
426426

427-
### Timers {#timers}
427+
### タイマー {#timers}
428428

429-
Your code might use timer-based functions like `setTimeout` to schedule more work in the future. In this example, a multiple choice panel waits for a selection and advances, timing out if a selection isn't made in 5 seconds:
429+
あなたのコードでは、未来に行う作業をスケジュールするために `setTimeout` のようなタイマーベースの関数を使っているかもしれません。以下の例では、選択肢のパネルを表示してどれかが選択されるまで待ちますが、5 秒以内に選択が起きなければタイムアウトするようになっています。
430430

431431
```jsx
432432
// card.js
@@ -455,7 +455,7 @@ export default function Card(props) {
455455
}
456456
```
457457

458-
We can write tests for this component by leveraging [Jest's timer mocks](https://jestjs.io/docs/en/timer-mocks), and testing the different states it can be in.
458+
このコンポーネントに対するテストは、[Jest のタイマーモック](https://jestjs.io/docs/en/timer-mocks)を活用し、可能性のある状態のそれぞれをテストする、というやりかたで書くことができます。
459459

460460
```jsx{7,31,37,49,59}
461461
// card.test.js
@@ -537,15 +537,15 @@ it("should accept selections", () => {
537537
});
538538
```
539539

540-
You can use fake timers only in some tests. Above, we enabled them by calling `jest.useFakeTimers()`. The main advantage they provide is that your test doesn't actually have to wait five seconds to execute, and you also didn't need to make the component code more convoluted just for testing.
540+
フェイクタイマーは一部のテストでのみ使うようにすることができます。上記の例では、`jest.useFakeTimers()` を呼ぶことでフェイクタイマーを有効化しています。これによる主な利点は、実行されるまで実際に 5 秒待つ必要がないということと、テストのためだけにコンポーネントのコードをより複雑にする必要がない、ということです。
541541

542542
---
543543

544-
### Snapshot Testing {#snapshot-testing}
544+
### スナップショットテスト {#snapshot-testing}
545545

546-
Frameworks like Jest also let you save "snapshots" of data with [`toMatchSnapshot` / `toMatchInlineSnapshot`](https://jestjs.io/docs/en/snapshot-testing). With these, we can "save" the rendered component output and ensure that a change to it has to be explicitly committed as a change to the snapshot.
546+
Jest のようなフレームワークでは、[`toMatchSnapshot` / `toMatchInlineSnapshot`](https://jestjs.io/docs/en/snapshot-testing) を使ってデータの「スナップショット」を保存することができます。これを使うことで、レンダーされたコンポーネントの出力を「セーブ」しておき、変更がスナップショットへの変更として明示的にコミットされるよう保証することができます。
547547

548-
In this example, we render a component and format the rendered HTML with the [`pretty`](https://www.npmjs.com/package/pretty) package, before saving it as an inline snapshot:
548+
以下の例では、コンポーネントをレンダーし、作成された HTML [`pretty`](https://www.npmjs.com/package/pretty) パッケージで整形し、インラインスナップショットとして保存します:
549549

550550
```jsx{29-31}
551551
// hello.test.js, again
@@ -598,13 +598,13 @@ it("should render a greeting", () => {
598598
});
599599
```
600600

601-
It's typically better to make more specific assertions than to use snapshots. These kinds of tests include implementation details so they break easily, and teams can get desensitized to snapshot breakages. Selectively [mocking some child components](#mocking-modules) can help reduce the size of snapshots and keep them readable for the code review.
601+
一般的には、スナップショットを使うよりもより個別的なアサーションを行う方がベターです。このようなテストは実装の詳細を含んでいるために壊れやすく、チームはスナップショットが壊れても気にならないようになってしまうかもしれません。選択的に[一部の子コンポーネントをモックする](#mocking-modules)ことで、スナップショットのサイズを減らし、コードレビューで読みやすく保つことができるようになります。
602602

603603
---
604604

605-
### Multiple Renderers {#multiple-renderers}
605+
### 複数のレンダラ {#multiple-renderers}
606606

607-
In rare cases, you may be running a test on a component that uses multiple renderers. For example, you may be running snapshot tests on a component with `react-test-renderer`, that internally uses `ReactDOM.render` inside a child component to render some content. In this scenario, you can wrap updates with `act()`s corresponding to their renderers.
607+
稀なケースにおいて、複数のレンダラを使うコンポーネントに対するテストを実行することがあるかもしれません。例えば、`react-test-renderer` を使ってコンポーネントのスナップショットテストを走らせているが、その内部で子コンポーネントが何かを表示するのに `ReactDOM.render` が使われている、ということがあるかもしれません。このようなシナリオでは、それらのレンダラーに対応する複数の `act()` で更新をラップすることができます。
608608

609609
```jsx
610610
import { act as domAct } from "react-dom/test-utils";
@@ -621,6 +621,6 @@ expect(root).toMatchSnapshot();
621621

622622
---
623623

624-
### Something Missing? {#something-missing}
624+
### 何かが足りない? {#something-missing}
625625

626-
If some common scenario is not covered, please let us know on the [issue tracker](https://github.com/reactjs/reactjs.org/issues) for the documentation website.
626+
もしよくあるシナリオがカバーされていない場合は、ドキュメント用ウェブサイトの[イシュートラッカ](https://github.com/reactjs/reactjs.org/issues)でお知らせください。

0 commit comments

Comments
 (0)