-
Notifications
You must be signed in to change notification settings - Fork 668
docs: update docs/ja #900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
docs: update docs/ja #900
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,30 +65,52 @@ expect(wrapper.find('div')).toBe(true) | |
|
||
## scopedSlots | ||
|
||
- 型: `{ [name: string]: string }` | ||
- 型: `{ [name: string]: string|Function }` | ||
|
||
コンポーネントにスコープ付きスロットのコンテンツのオブジェクトを渡します。key はスロット名に対応します。値はテンプレート文字列を指定します。 | ||
コンポーネントにスコープ付きスロットのオブジェクトを提供します。そのオブジェクトのキーはスロット名になります。 | ||
|
||
3つ制限事項があります。 | ||
slot-scope 属性を使って props を指定することができます。 | ||
|
||
* [email protected]+ のみをサポートします。 | ||
```js | ||
shallowMount(Component, { | ||
scopedSlots: { | ||
foo: '<p slot-scope="foo">{{foo.index}},{{foo.text}}</p>' | ||
} | ||
}) | ||
``` | ||
|
||
* `<template>` タグを `scopedSlots` オプションのルート要素として使用することはできません。 | ||
slot-scope 属性を使って props を指定しない場合、スロットが展開されると `props` という変数名で props を使用することができます。 | ||
|
||
* PhantomJS をサポートしません。 | ||
代わりに [Puppeteer](https://github.com/karma-runner/karma-chrome-launcher#headless-chromium-with-puppeteer) を使用してください。 | ||
```js | ||
shallowMount(Component, { | ||
scopedSlots: { | ||
default: '<p>{{props.index}},{{props.text}}</p>' | ||
} | ||
}) | ||
``` | ||
|
||
例: | ||
props を引数に取る関数を渡すことができます。 | ||
|
||
```js | ||
const wrapper = shallowMount(Component, { | ||
shallowMount(Component, { | ||
scopedSlots: { | ||
foo: function (props) { | ||
return this.$createElement('div', props.index) | ||
} | ||
} | ||
}) | ||
``` | ||
|
||
または、 JSX を使用することができます。メソッド内に JSX を書いた場合、`this.$createElement` は babel-plugin-transform-vue-jsx によって自動的に注入されます。 | ||
|
||
```js | ||
shallowMount(Component, { | ||
scopedSlots: { | ||
foo: '<p slot-scope="props">{{props.index}},{{props.text}}</p>' | ||
foo (props) { | ||
return <div>{ props.text }</div> | ||
} | ||
} | ||
}) | ||
expect(wrapper.find('#fooWrapper').html()).toBe( | ||
`<div id="fooWrapper"><p>0,text1</p><p>1,text2</p><p>2,text3</p></div>` | ||
) | ||
``` | ||
|
||
## stubs | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
## TypeScript と一緒に使う | ||
|
||
> この記事のサンプルプロジェクトは、 [GitHub](https://github.com/vuejs/vue-test-utils-typescript-example) にあります。 | ||
|
||
TypeScript は JavaScript に型とクラスを加えた人気のある JavaScript のスーパーセットです。 Vue Test Utils の型定義は、配布されているVue Test Utils のパッケージに含まれています。だから、Vue Test Utils と TypeScript はうまく動作します。 | ||
|
||
ここでは、基本的な Vue CLI を使った TypeScript のセットアップから Jest と Vue Test Utils を使用した TypeScript のテストの作成までを解説します。 | ||
|
||
### TypeScript の追加 | ||
|
||
最初にプロジェクトを作成します。もし、Vue CLI をインストールしていないなら、 Vue CLI をグローバルにインストールしてください。 | ||
|
||
```shell | ||
$ npm install -g @vue/cli-service-global | ||
``` | ||
|
||
以下のようにプロジェクトを作成します。 | ||
|
||
```shell | ||
$ vue create hello-world | ||
``` | ||
|
||
CLI プロンプトで `Manually select features` を選択します。そして、 TypeScript を選択して Enter キーを押します。これで TypeScript の設定がされているプロジェクトが生成されます。 | ||
|
||
::: tip 注意 | ||
Vue と TypeScript を一緒に使うためのセットアップの詳細は、 [TypeScript Vue starter guide](https://github.com/Microsoft/TypeScript-Vue-Starter) を確認してください。 | ||
::: | ||
|
||
次にプロジェクトに Jest を加えます。 | ||
|
||
### Jest のセットアップ | ||
|
||
Jest はバッテリー付属のユニットテストソリューションを提供するために Facebook が開発したテストランナです。 Jest の詳細については[公式ドキュメント](https://facebook.github.io/jest/) を参照してください。 | ||
|
||
Jest と Vue Test Utils をインストールします。 | ||
|
||
```bash | ||
$ npm install --save-dev jest @vue/test-utils | ||
``` | ||
|
||
次に `test:unit` スクリプトを `package.json` に定義します。 | ||
|
||
```json | ||
// package.json | ||
{ | ||
// .. | ||
"scripts": { | ||
// .. | ||
"test:unit": "jest" | ||
} | ||
// .. | ||
} | ||
``` | ||
|
||
### Jest での SFCs の処理 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Vue で単体テスト出来る人は、初心者じゃないので、 |
||
|
||
Jest が `*.vue` ファイルを処理するために `vue-jest` プリプロセッサをインストールして設定します。 | ||
|
||
``` bash | ||
npm install --save-dev vue-jest | ||
``` | ||
|
||
次に `jest` ブロックを `package.json` に追加します。 | ||
|
||
``` json | ||
{ | ||
// ... | ||
"jest": { | ||
"moduleFileExtensions": [ | ||
"js", | ||
"ts", | ||
"json", | ||
// `*.vue` ファイルを Jest で取り扱います。 | ||
"vue" | ||
], | ||
"transform": { | ||
// `vue-jest` で `*.vue` ファイルを処理します。 | ||
".*\\.(vue)$": "vue-jest", | ||
}, | ||
"testURL": "http://localhost/" | ||
} | ||
} | ||
``` | ||
|
||
### Jest に対応するための TypeScript の設定 | ||
|
||
テストで TypeScript ファイルを使うために Jest が TypeScript をコンパイルするようにセットアップする必要があります。そのために `ts-jest` をインストールします。 | ||
|
||
``` bash | ||
$ npm install --save-dev ts-jest | ||
``` | ||
|
||
次に Jest が TypeScript のテストファイルを `ts-jest` で処理するために `package.json` の `jest.transform` に設定を追加します。 | ||
|
||
``` json | ||
{ | ||
// ... | ||
"jest": { | ||
// ... | ||
"transform": { | ||
// ... | ||
// `ts-jest` で `*.ts` ファイルを処理します。 | ||
"^.+\\.tsx?$": "ts-jest" | ||
}, | ||
// ... | ||
} | ||
} | ||
``` | ||
|
||
### テストファイルの配置 | ||
|
||
デフォルトでは、 Jest はプロジェクトにある拡張子が `.spec.js` もしくは `.test.js` のすべてのファイルを対象にします。 | ||
|
||
拡張子が `.ts` のテストファイルを実行するために、`package.json` ファイルの `testRegex` を変更する必要があります。 | ||
|
||
以下を `package.json` の `jest` フィールドに追加します。 | ||
|
||
``` json | ||
{ | ||
// ... | ||
"jest": { | ||
// ... | ||
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$" | ||
} | ||
} | ||
``` | ||
|
||
Jest はテストされるコードと同じディレクトリに `__tests__` ディレクトリを作成することを推奨していますが、あなたにとってテストに適したディレクトリ構造にして構いません。ただ、Jest は `__snapshots__` ディレクトリをスナップショットテストを実施するテストファイルと同じディレクトリに作成することに注意してください。 | ||
|
||
### ユニットテストを書く | ||
|
||
これでプロジェクトのセットアップが完了しました。今度はユニットテストを作成します。 | ||
|
||
`src/components/__tests__/HelloWorld.spec.ts` ファイルを作成して、以下のコードを加えます。 | ||
|
||
```js | ||
// src/components/__tests__/HelloWorld.spec.ts | ||
import { shallowMount } from '@vue/test-utils' | ||
import HelloWorld from '../HelloWorld.vue' | ||
|
||
describe('HelloWorld.vue', () => { | ||
test('renders props.msg when passed', () => { | ||
const msg = 'new message' | ||
const wrapper = shallowMount(HelloWorld, { | ||
propsData: { msg } | ||
}) | ||
expect(wrapper.text()).toMatch(msg) | ||
}) | ||
}) | ||
``` | ||
|
||
これが TypeScript と Vue Test Utils を協業させるために必要なことすべてです! | ||
|
||
### リソース | ||
|
||
- [この記事のサンプルプロジェクト](https://github.com/vuejs/vue-test-utils-typescript-example) | ||
- [Jest](https://facebook.github.io/jest/) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
npm install -g @vue/cli
では?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
原文もそうなんですね。https://github.com/vuejs/vue-test-utils/blob/dev/docs/guides/using-with-typescript.md#adding-typescript