-
Notifications
You must be signed in to change notification settings - Fork 665
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 all commits
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
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
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 | ||
``` | ||
|
||
以下のようにプロジェクトを作成します。 | ||
|
||
```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 での単一ファイルコンポーネントの処理 | ||
|
||
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.
@eddyerburgh @kazupon
This line was changed.