Skip to content

Commit dd270b6

Browse files
38elementseddyerburgh
authored andcommitted
docs: update docs/ja (vuejs#352)
* docs: update docs/ja * Update testing-async-components.md
1 parent cd214ce commit dd270b6

13 files changed

+172
-20
lines changed

docs/ja/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [テストランナを選ぶ](guides/choosing-a-test-runner.md)
1010
* [Jest による単一ファイルコンポーネントのテスト](guides/testing-SFCs-with-jest.md)
1111
* [Mocha + webpack による単一ファイルコンポーネントのテスト](guides/testing-SFCs-with-mocha-webpack.md)
12+
* [非同期動作のテスト](guides/testing-async-components.md)
1213
* [Vue Router と一緒に使う](guides/using-with-vue-router.md)
1314
* [Vuex と一緒に使う](guides/using-with-vuex.md)
1415
* [API](api/README.md)

docs/ja/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* [テストランナを選ぶ](guides/choosing-a-test-runner.md)
88
* [Jest による単一ファイルコンポーネントのテスト](guides/testing-SFCs-with-jest.md)
99
* [Mocha + webpack による単一ファイルコンポーネントのテスト](guides/testing-SFCs-with-mocha-webpack.md)
10+
* [非同期動作のテスト](guides/testing-async-components.md)
1011
* [Vue Router と一緒に使う](guides/using-with-vue-router.md)
1112
* [Vuex と一緒に使う](guides/using-with-vuex.md)
1213
* [API](api/README.md)

docs/ja/api/config.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ vue-test-utils にはオプションを定義するための `config` オプシ
2121
```js
2222
import VueTestUtils from '@vue/test-utils'
2323

24-
VueTestUtils.config.stubs['my-compomnent'] = '<div />'
24+
VueTestUtils.config.stubs['my-component'] = '<div />'
2525
```

docs/ja/api/mount.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313

1414
- **使い方:**
1515

16-
最初の DOM ノードまたは Vue コンポーネント一致セレクタの [`Wrapper`](./wrapper/README.md) を返します。
17-
18-
有効な[セレクタ](./selectors.md)を使用してください。
16+
マウントされて描画された Vue コンポーネントを含む [`Wrapper`](./wrapper/README.md) を生成します。
1917

2018
**オプションなし:**
2119

docs/ja/api/selectors.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
Vue コンポーネントもセレクタとして有効です。
2525

26-
vue-test-utils は `name` プロパティを使用して、一致する Vue コンポーネントのインスタンスツリーを検索します。
27-
2826
```js
2927
// Foo.vue
3028

@@ -44,9 +42,18 @@ expect(wrapper.is(Foo)).toBe(true)
4442

4543
## find メソッドのオプションオブジェクト
4644

45+
### name
46+
47+
find メソッドのオプションオブジェクトを使用すると、Wrapper コンポーネント内にあるコンポーネントの `name` に一致する要素を取得することができます。
48+
49+
```js
50+
const buttonWrapper = wrapper.find({ name: 'my-button' })
51+
buttonWrapper.trigger('click')
52+
```
53+
4754
### ref
4855

49-
find メソッドのオプションオブジェクトを使用すると、`Wrapper` コンポーネントの `$ref` プロパティに一致する要素を取得することができます。
56+
find メソッドのオプションオブジェクトを使用すると、Wrapper コンポーネントの `$ref` プロパティに一致する要素を取得することができます。
5057

5158
```js
5259
const buttonWrapper = wrapper.find({ ref: 'myButton' })

docs/ja/api/shallow.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@
2222

2323
- **使い方:**
2424

25-
最初の DOM ノードまたは Vue コンポーネント一致セレクタの [`Wrapper`](./wrapper/) を返します。
26-
27-
全ての子コンポーネントをスタブします。
28-
29-
有効な[セレクタ](./selectors.md)を使用してください。
25+
[`mount`](mount.md)のようにマウントされて描画された Vue コンポーネントを含む [`Wrapper`](./wrapper/README.md) を生成しますが、  
26+
子コンポーネントはスタブされたコンポーネントです。
3027

3128
**オプションなし:**
3229

docs/ja/guides/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
* [テストランナを選ぶ](./choosing-a-test-runner.md)
77
* [Jest による単一ファイルコンポーネントのテスト](./testing-SFCs-with-jest.md)
88
* [Mocha + webpack による単一ファイルコンポーネントのテスト](./testing-SFCs-with-mocha-webpack.md)
9+
* [非同期動作のテスト](./testing-async-components.md)
910
* [Vue Router と一緒に使う](./using-with-vue-router.md)
1011
* [Vuex と一緒に使う](./using-with-vuex.md)

docs/ja/guides/common-tips.md

+3
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ mount(Component, {
109109
})
110110
```
111111

112+
**Vue Router のようなプラグインはグローバルの Vue コンストラクタに read-only なプロパティを追加します。
113+
これは localVue コンストラクタにそのプラグインを再びインストールすることや read-only なプロパティに対するモックを追加することを不可能にします。**
114+
112115
## モックの注入
113116

114117
単純なモックを注入するための別の戦略として `mocks` オプションで行うことができます:

docs/ja/guides/dom-events.md

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ wrapper.find('button').trigger('click')
2222

2323
`trigger` メソッドはオプションで `options` オブジェクトを引数として取ります。`options` オブジェクトのプロパティはイベントオブジェクトのプロパティに追加されます。
2424

25+
target を `options` オブジェクトに追加することができないことに注意してください。
26+
2527
```js
2628
const wrapper = mount(MyButton)
2729

docs/ja/guides/testing-SFCs-with-jest.md

+38-4
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ webpack で `babel-preset-env` を使用するとした場合、webpack は ES M
123123
}
124124
```
125125

126-
### スナップショットテスト
126+
## スナップショットテスト
127127

128128
[`vue-server-renderer`](https://github.com/vuejs/vue/tree/dev/packages/vue-server-renderer) を使ってコンポーネントを文字列に描画して保存することができます。[Jest のスナップショットテスト](https://facebook.github.io/jest/docs/en/snapshot-testing.html) のスナップショットとして表示されます。
129129

@@ -148,13 +148,47 @@ npm install --save-dev jest-serializer-vue
148148
}
149149
```
150150

151-
### テストファイルの配置
151+
## テストファイルの配置
152152

153153
デフォルトでは、Jest はプロジェクト全体で `.spec.js` または `.test.js` 拡張子を持つすべてのファイルを再帰的に取得します。これがあなたのニーズに合わない場合は、`package.json` ファイルの config セクションで[testRegex を変更する](https://facebook.github.io/jest/docs/en/configuration.html#testregex-string)ことが可能です。
154154

155155
Jestは、テスト対象のコードのすぐ隣に`__tests__`ディレクトリを作成することを推奨していますが、適切にテストを構造化することは自由です。 Jestがスナップショットテストを実行するテストファイルの隣に`__snapshots__`ディレクトリを作成することに注意してください。
156156

157-
### Spec の例
157+
## カバレッジ
158+
159+
Jest は複数のフォーマットでカバレッジを取ることができます。 以下はそれをするための簡単な例です。
160+
161+
`jest` の設定 (普通は `package.json` か `jest.config.js`) に [collectCoverage](https://facebook.github.io/jest/docs/en/configuration.html#collectcoverage-boolean) オプションを加えます。それから、カバレッジを収集する対象のファイルを [collectCoverageFrom](https://facebook.github.io/jest/docs/en/configuration.html#collectcoveragefrom-array) に配列で定義します。 正確なカバレッジデータのために [mapCoverage](https://facebook.github.io/jest/docs/en/configuration.html#mapcoverage-boolean)`true` にします。
162+
163+
```json
164+
{
165+
"jest": {
166+
// ...
167+
"collectCoverage": true,
168+
"collectCoverageFrom": [
169+
"**/*.{js,vue}",
170+
"!**/node_modules/**"
171+
],
172+
"mapCoverage": true
173+
}
174+
}
175+
```
176+
177+
[デフォルトのカバレッジレポーター](https://facebook.github.io/jest/docs/en/configuration.html#coveragereporters-array-string)のカバレッジレポートは有効になります。 `coverageReporters` オプションでそれらをカスタマイズすることができます。
178+
179+
```json
180+
{
181+
"jest": {
182+
// ...
183+
"coverageReporters": ["html", "text-summary"]
184+
}
185+
}
186+
```
187+
188+
より詳しい情報は [Jest configuration documentation](https://facebook.github.io/jest/docs/en/configuration.html#collectcoverage-boolean) にあります。 カバレッジの閾値やターゲットを出力するディレクトリなどのオプションが記載されています。
189+
190+
191+
## Spec の例
158192

159193
あなたが Jasmine をよく知っているなら、Jest の [assertion API](https://facebook.github.io/jest/docs/en/expect.html#content)は自宅のように感じるはずです。
160194

@@ -170,7 +204,7 @@ describe('Component', () => {
170204
})
171205
```
172206

173-
### リソース
207+
## リソース
174208

175209
- [このセットアップのプロジェクト例](https://github.com/vuejs/vue-test-utils-jest-example)
176210
- [Vue Conf 2017のスライド](https://github.com/codebryo/vue-testing-with-jest-conf17)

docs/ja/guides/testing-SFCs-with-mocha-webpack.md

+4
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ npm run unit
170170

171171
やったー!テストを実行している!
172172

173+
### カバレッジ
174+
175+
mocha-webpack にコードカバレッジをセットしたい場合は、 [the mocha-webpack code coverage guide](https://github.com/zinserjan/mocha-webpack/blob/master/docs/guides/code-coverage.md) に従ってください。
176+
173177
### リソース
174178

175179
- [この設定のプロジェクトの例](https://github.com/vuejs/vue-test-utils-mocha-webpack-example)
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# 非同期動作のテスト
2+
3+
テストをシンプルにするために、 `vue-test-utils` はDOMの更新を同期的に適用します。しかし、コールバックや Promise のようなコンポーネントの非同期動作をテストする場合、いくつかのテクニックを知っておく必要があります。
4+
5+
よくある非同期動作の1つとして API 呼び出しと Vuex の action があります。以下の例は API 呼び出しをするメソッドをテストする方法を示しています。この例は HTTP のライブラリである `axios` をモックしてテストを実行するために Jest を使っています。Jestのモックの詳細は[ここ](https://facebook.github.io/jest/docs/en/manual-mocks.html#content)にあります。
6+
7+
`axios` のモックの実装はこのようにします。
8+
9+
``` js
10+
export default {
11+
get: () => new Promise(resolve => {
12+
resolve({ data: 'value' })
13+
})
14+
}
15+
```
16+
17+
ボタンをクリックすると以下のコンポーネントは API 呼び出しをします。そして、レスポンスを `value` に代入します。
18+
19+
``` html
20+
<template>
21+
<button @click="fetchResults" />
22+
</template>
23+
24+
<script>
25+
import axios from 'axios'
26+
27+
export default {
28+
data () {
29+
return {
30+
value: null
31+
}
32+
},
33+
34+
methods: {
35+
async fetchResults () {
36+
const response = await axios.get('mock/service')
37+
this.value = response.data
38+
}
39+
}
40+
}
41+
</script>
42+
```
43+
44+
テストはこのように書くことができます。
45+
46+
``` js
47+
import { shallow } from '@vue/test-utils'
48+
import Foo from './Foo'
49+
jest.mock('axios')
50+
51+
test('Foo', () => {
52+
it('fetches async when a button is clicked', () => {
53+
const wrapper = shallow(Foo)
54+
wrapper.find('button').trigger('click')
55+
expect(wrapper.vm.value).toEqual('value')
56+
})
57+
})
58+
```
59+
60+
`fetchResults` 内の Promise が resolve する前にアサーションが呼ばれるので、このテストは現時点では失敗します。ほとんどのユニットテストライブラリはテストが完了したことをテストランナーに知らせるためのコールバック関数を提供します。Jest と Mocha は両方とも `done` を使います。アサーションが行われる前に確実に各 Promise が resolve するために `done``$nextTick` や `setTimeout` と組み合わせて使うことができます。
61+
62+
``` js
63+
test('Foo', () => {
64+
it('fetches async when a button is clicked', (done) => {
65+
const wrapper = shallow(Foo)
66+
wrapper.find('button').trigger('click')
67+
wrapper.vm.$nextTick(() => {
68+
expect(wrapper.vm.value).toEqual('value')
69+
done()
70+
})
71+
})
72+
})
73+
```
74+
75+
`$nextTick` と `setTimeout` がテストをパスする理由は `$nextTick` と `setTimeout` を処理するタスクキュー前に Promise のコールバック関数を処理するマイクロタスクキューが実行されるからです。つまり、`$nextTick` と `setTimeout` が実行される前に、マイクロタスクキュー上にあるすべての Promise のコールバック関数が実行されます。より詳しい説明は[ここ](https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/)を見てください。
76+
77+
もう1つの解決策は `async` function と npm パッケージの `flush-promises` を使用することです。`flush-promises` は堰き止められている resolve された Promise ハンドラを流します。堰き止められている Promise を流すこととテストの可読性を改善するために `await``flushPromises` の呼び出しの前に置きます。
78+
79+
反映されたテストはこのようになります。
80+
81+
``` js
82+
import { shallow } from '@vue/test-utils'
83+
import flushPromises from 'flush-promises'
84+
import Foo from './Foo'
85+
jest.mock('axios')
86+
87+
test('Foo', () => {
88+
it('fetches async when a button is clicked', async () => {
89+
const wrapper = shallow(Foo)
90+
wrapper.find('button').trigger('click')
91+
await flushPromises()
92+
expect(wrapper.vm.value).toEqual('value')
93+
})
94+
})
95+
```
96+
97+
同じテクニックをデフォルトで Promise を返す Vuex の action に適用することができます。

docs/ja/guides/using-with-vue-router.md

+11-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
これを回避するために、localeVue を作成し、その上に Vue Router をインストールすることができます。
88

99
```js
10+
import { shallow, createLocalVue } from '@vue/test-utils'
1011
import VueRouter from 'vue-router'
11-
const localVue = createLocalVue()
1212

13+
const localVue = createLocalVue()
1314
localVue.use(VueRouter)
1415

1516
shallow(Component, {
@@ -26,6 +27,8 @@ Vue Router をインストールする時、`router-link` と `router-view` コ
2627
### スタブを使用する
2728

2829
```js
30+
import { shallow } from '@vue/test-utils'
31+
2932
shallow(Component, {
3033
stubs: ['router-link', 'router-view']
3134
})
@@ -34,9 +37,10 @@ shallow(Component, {
3437
### localVue による Vue Router のインストール
3538

3639
```js
40+
import { shallow, createLocalVue } from '@vue/test-utils'
3741
import VueRouter from 'vue-router'
38-
const localVue = createLocalVue()
3942

43+
const localVue = createLocalVue()
4044
localVue.use(VueRouter)
4145

4246
shallow(Component, {
@@ -49,6 +53,8 @@ shallow(Component, {
4953
時々、コンポーネントが `$route``$router` オブジェクトから引数によって何かをするテストをしたいときがあります。これをするためには、Vue インスタンスにカスタムモックを渡すことができます。
5054

5155
```js
56+
import { shallow } from '@vue/test-utils'
57+
5258
const $route = {
5359
path: '/some/path'
5460
}
@@ -59,7 +65,7 @@ const wrapper = shallow(Component, {
5965
}
6066
})
6167

62-
wrapper.vm.$router // /some/path
68+
wrapper.vm.$route.path // /some/path
6369
```
6470

6571
## よくある落とし穴
@@ -68,4 +74,5 @@ Vue Router をインストールすると Vue のプロトタイプに読み取
6874

6975
これは、`$route` または `$router` をモックを試みるテストが将来失敗することを意味します。
7076

71-
これを回避するために、テストを実行するときに、Vue Router をインストールしないでください。
77+
これを回避するために、テストを実行するときに、Vue Router をグローバルにインストールしないでください。
78+
上記のように localVue を使用してください。

0 commit comments

Comments
 (0)