Skip to content

Commit 3d47913

Browse files
38elementseddyerburgh
authored andcommitted
docs: update docs/ja (#900)
1 parent d433ea5 commit 3d47913

6 files changed

+229
-41
lines changed

Diff for: docs/guides/using-with-typescript.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ In this guide, we'll walk through how to setup a testing setup for a TypeScript
1111
First you need to create a project. If you don't have Vue CLI installed, install it globally:
1212

1313
```shell
14-
$ npm install -g @vue/cli-service-global
14+
$ npm install -g @vue/cli
1515
```
1616

1717
And create a project by running:

Diff for: docs/ja/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* [Mocha + webpack による単一ファイルコンポーネントのテスト](guides/testing-single-file-components-with-mocha-webpack.md)
1212
* [Karma による単一ファイルコンポーネントのテスト](guides/testing-single-file-components-with-karma.md)
1313
* [非同期動作のテスト](guides/testing-async-components.md)
14+
* [TypeScript と一緒に使う](guides/using-with-typescript.md)
1415
* [Vue Router と一緒に使う](guides/using-with-vue-router.md)
1516
* [Vuex と一緒に使う](guides/using-with-vuex.md)
1617
* [API](api/)

Diff for: docs/ja/api/options.md

+35-13
Original file line numberDiff line numberDiff line change
@@ -65,30 +65,52 @@ expect(wrapper.find('div')).toBe(true)
6565

6666
## scopedSlots
6767

68-
- 型: `{ [name: string]: string }`
68+
- 型: `{ [name: string]: string|Function }`
6969

70-
コンポーネントにスコープ付きスロットのコンテンツのオブジェクトを渡します。key はスロット名に対応します。値はテンプレート文字列を指定します
70+
コンポーネントにスコープ付きスロットのオブジェクトを提供します。そのオブジェクトのキーはスロット名になります
7171

72-
3つ制限事項があります
72+
slot-scope 属性を使って props を指定することができます
7373

74-
* [email protected]+ のみをサポートします。
74+
```js
75+
shallowMount(Component, {
76+
scopedSlots: {
77+
foo: '<p slot-scope="foo">{{foo.index}},{{foo.text}}</p>'
78+
}
79+
})
80+
```
7581

76-
* `<template>` タグを `scopedSlots` オプションのルート要素として使用することはできません
82+
slot-scope 属性を使って props を指定しない場合、スロットが展開されると `props` という変数名で props を使用することができます
7783

78-
* PhantomJS をサポートしません。
79-
代わりに [Puppeteer](https://github.com/karma-runner/karma-chrome-launcher#headless-chromium-with-puppeteer) を使用してください。
84+
```js
85+
shallowMount(Component, {
86+
scopedSlots: {
87+
default: '<p>{{props.index}},{{props.text}}</p>'
88+
}
89+
})
90+
```
8091

81-
例:
92+
props を引数に取る関数を渡すことができます。
8293

8394
```js
84-
const wrapper = shallowMount(Component, {
95+
shallowMount(Component, {
96+
scopedSlots: {
97+
foo: function (props) {
98+
return this.$createElement('div', props.index)
99+
}
100+
}
101+
})
102+
```
103+
104+
または、 JSX を使用することができます。メソッド内に JSX を書いた場合、`this.$createElement` は babel-plugin-transform-vue-jsx によって自動的に注入されます。
105+
106+
```js
107+
shallowMount(Component, {
85108
scopedSlots: {
86-
foo: '<p slot-scope="props">{{props.index}},{{props.text}}</p>'
109+
foo (props) {
110+
return <div>{ props.text }</div>
111+
}
87112
}
88113
})
89-
expect(wrapper.find('#fooWrapper').html()).toBe(
90-
`<div id="fooWrapper"><p>0,text1</p><p>1,text2</p><p>2,text3</p></div>`
91-
)
92114
```
93115

94116
## stubs

Diff for: docs/ja/guides/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
!!!include(docs/ja/guides/testing-single-file-components-with-mocha-webpack.md)!!!
99
!!!include(docs/ja/guides/testing-single-file-components-with-karma.md)!!!
1010
!!!include(docs/ja/guides/testing-async-components.md)!!!
11+
!!!include(docs/ja/guides/using-with-typescript.md)!!!
1112
!!!include(docs/ja/guides/using-with-vue-router.md)!!!
12-
!!!include(docs/ja/guides/using-with-vuex.md)!!!
13+
!!!include(docs/ja/guides/using-with-vuex.md)!!!

Diff for: docs/ja/guides/testing-single-file-components-with-jest.md

+33-26
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ npm install --save-dev vue-jest
4747
],
4848
"transform": {
4949
// vue-jest で *.vue ファイルを処理する
50-
".*\\.(vue)$": "<rootDir>/node_modules/vue-jest"
50+
".*\\.(vue)$": "vue-jest"
5151
}
5252
}
5353
}
@@ -122,31 +122,6 @@ webpack で `babel-preset-env` を使用するとした場合、webpack は ES M
122122
}
123123
```
124124

125-
### スナップショットテスト
126-
127-
[`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) のスナップショットとして表示されます。
128-
129-
`vue-server-renderer` の描画結果には、いくつかの SSR (Server-Side Rendering: サーバサイドレンダリング) 固有の属性が含まれており、空白を無視するため、diff をスキャンするのが難しくなります。カスタムシリアライザを使用して、保存されたスナップショットを改善することができます。
130-
131-
``` bash
132-
npm install --save-dev jest-serializer-vue
133-
```
134-
135-
次に `package.json` で設定します:
136-
137-
``` json
138-
{
139-
// ...
140-
"jest": {
141-
// ...
142-
// スナップショットのシリアライザ
143-
"snapshotSerializers": [
144-
"<rootDir>/node_modules/jest-serializer-vue"
145-
]
146-
}
147-
}
148-
```
149-
150125
### テストファイルの配置
151126

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

180+
### スナップショットテスト
181+
182+
Vue Test Utils でコンポーネントをマウントすると、コンポーネントのルート HTML 要素にアクセスすることができます。 [Jest のスナップショットテスト](https://facebook.github.io/jest/docs/en/snapshot-testing.html)で使用するためにこれを保存することができます。
183+
184+
```js
185+
test('renders correctly', () => {
186+
const wrapper = mount(Component)
187+
expect(wrapper.element).toMatchSnapshot()
188+
})
189+
```
190+
191+
カスタムシリアライザを使用することによって保存されたスナップショットを改善することができます。
192+
193+
``` bash
194+
npm install --save-dev jest-serializer-vue
195+
```
196+
197+
`package.json` でその設定をします。
198+
199+
``` json
200+
{
201+
// ...
202+
"jest": {
203+
// ...
204+
// スナップショットに対するシリアライズ
205+
"snapshotSerializers": [
206+
"jest-serializer-vue"
207+
]
208+
}
209+
}
210+
```
211+
205212
### リソース
206213

207214
- [このセットアップのプロジェクト例](https://github.com/vuejs/vue-test-utils-jest-example)

Diff for: docs/ja/guides/using-with-typescript.md

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
## TypeScript と一緒に使う
2+
3+
> この記事のサンプルプロジェクトは、 [GitHub](https://github.com/vuejs/vue-test-utils-typescript-example) にあります。
4+
5+
TypeScript は JavaScript に型とクラスを加えた人気のある JavaScript のスーパーセットです。 Vue Test Utils の型定義は、配布されているVue Test Utils のパッケージに含まれています。だから、Vue Test Utils と TypeScript はうまく動作します。
6+
7+
ここでは、基本的な Vue CLI を使った TypeScript のセットアップから Jest と Vue Test Utils を使用した TypeScript のテストの作成までを解説します。
8+
9+
### TypeScript の追加
10+
11+
最初にプロジェクトを作成します。もし、Vue CLI をインストールしていないなら、 Vue CLI をグローバルにインストールしてください。
12+
13+
```shell
14+
$ npm install -g @vue/cli
15+
```
16+
17+
以下のようにプロジェクトを作成します。
18+
19+
```shell
20+
$ vue create hello-world
21+
```
22+
23+
CLI プロンプトで `Manually select features` を選択します。そして、 TypeScript を選択して Enter キーを押します。これで TypeScript の設定がされているプロジェクトが生成されます。
24+
25+
::: tip 注意
26+
Vue と TypeScript を一緒に使うためのセットアップの詳細は、 [TypeScript Vue starter guide](https://github.com/Microsoft/TypeScript-Vue-Starter) を確認してください。
27+
:::
28+
29+
次にプロジェクトに Jest を加えます。
30+
31+
### Jest のセットアップ
32+
33+
Jest はバッテリー付属のユニットテストソリューションを提供するために Facebook が開発したテストランナです。 Jest の詳細については[公式ドキュメント](https://facebook.github.io/jest/) を参照してください。
34+
35+
Jest と Vue Test Utils をインストールします。
36+
37+
```bash
38+
$ npm install --save-dev jest @vue/test-utils
39+
```
40+
41+
次に `test:unit` スクリプトを `package.json` に定義します。
42+
43+
```json
44+
// package.json
45+
{
46+
// ..
47+
"scripts": {
48+
// ..
49+
"test:unit": "jest"
50+
}
51+
// ..
52+
}
53+
```
54+
55+
### Jest での単一ファイルコンポーネントの処理
56+
57+
Jest が `*.vue` ファイルを処理するために `vue-jest` プリプロセッサをインストールして設定します。
58+
59+
``` bash
60+
npm install --save-dev vue-jest
61+
```
62+
63+
次に `jest` ブロックを `package.json` に追加します。
64+
65+
``` json
66+
{
67+
// ...
68+
"jest": {
69+
"moduleFileExtensions": [
70+
"js",
71+
"ts",
72+
"json",
73+
// `*.vue` ファイルを Jest で取り扱います。
74+
"vue"
75+
],
76+
"transform": {
77+
// `vue-jest` で `*.vue` ファイルを処理します。
78+
".*\\.(vue)$": "vue-jest",
79+
},
80+
"testURL": "http://localhost/"
81+
}
82+
}
83+
```
84+
85+
### Jest に対応するための TypeScript の設定
86+
87+
テストで TypeScript ファイルを使うために Jest が TypeScript をコンパイルするようにセットアップする必要があります。そのために `ts-jest` をインストールします。
88+
89+
``` bash
90+
$ npm install --save-dev ts-jest
91+
```
92+
93+
次に Jest が TypeScript のテストファイルを `ts-jest` で処理するために `package.json``jest.transform` に設定を追加します。
94+
95+
``` json
96+
{
97+
// ...
98+
"jest": {
99+
// ...
100+
"transform": {
101+
// ...
102+
// `ts-jest` で `*.ts` ファイルを処理します。
103+
"^.+\\.tsx?$": "ts-jest"
104+
},
105+
// ...
106+
}
107+
}
108+
```
109+
110+
### テストファイルの配置
111+
112+
デフォルトでは、 Jest はプロジェクトにある拡張子が `.spec.js` もしくは `.test.js` のすべてのファイルを対象にします。
113+
114+
拡張子が `.ts` のテストファイルを実行するために、`package.json` ファイルの `testRegex` を変更する必要があります。
115+
116+
以下を `package.json``jest` フィールドに追加します。
117+
118+
``` json
119+
{
120+
// ...
121+
"jest": {
122+
// ...
123+
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$"
124+
}
125+
}
126+
```
127+
128+
Jest はテストされるコードと同じディレクトリに `__tests__` ディレクトリを作成することを推奨していますが、あなたにとってテストに適したディレクトリ構造にして構いません。ただ、Jest は `__snapshots__` ディレクトリをスナップショットテストを実施するテストファイルと同じディレクトリに作成することに注意してください。
129+
130+
### ユニットテストを書く
131+
132+
これでプロジェクトのセットアップが完了しました。今度はユニットテストを作成します。
133+
134+
`src/components/__tests__/HelloWorld.spec.ts` ファイルを作成して、以下のコードを加えます。
135+
136+
```js
137+
// src/components/__tests__/HelloWorld.spec.ts
138+
import { shallowMount } from '@vue/test-utils'
139+
import HelloWorld from '../HelloWorld.vue'
140+
141+
describe('HelloWorld.vue', () => {
142+
test('renders props.msg when passed', () => {
143+
const msg = 'new message'
144+
const wrapper = shallowMount(HelloWorld, {
145+
propsData: { msg }
146+
})
147+
expect(wrapper.text()).toMatch(msg)
148+
})
149+
})
150+
```
151+
152+
これが TypeScript と Vue Test Utils を協業させるために必要なことすべてです!
153+
154+
### リソース
155+
156+
- [この記事のサンプルプロジェクト](https://github.com/vuejs/vue-test-utils-typescript-example)
157+
- [Jest](https://facebook.github.io/jest/)

0 commit comments

Comments
 (0)