"cypress": "^3.4.1"現在
Cypressのテストを.tsで書くのは簡単
Transpiling TypeScript test filesにあるように
- tsconfig.jsonを書く
- テストファイルを
.tsにする - テストをTypescriptで書く
これだけで良い
CypressのCustom CommandsもTypescriptで書きたい
上の状態でsupport/commands.jsをJavascriptのまま書くなら何も問題が無いが、これすらもsupport/commands.tsとTypescriptで書こうとすると色々問題が出てくる
直接的な解決方法はこの例にあるrepoを見れば分かるが、流れとしては
- Cypressは素の状態では
support/index.jsを読み込む -
support/index.jsはsupport/commands.jsを読み込む - これによりCustom Commandsが使えるようになる
なので
-
support/commands.jsをsupport/commands.tsにするには -
support/index.jsをsupport/index.tsにしないといけない -
support/index.tsにしたのなら、Cypressにsupport/index.tsを読み込むように指示しないといけない - この為、configのsupportFileを変更しないといけない
- 更にはこれらのtsをトランスパイル出来るように色々インストールしておかないといけない
解決方法
plugins/index.jsに何か追加していたなら、事前にplugins/index.jsはバックアップしておいた方が良い
@bahmutov/add-typescript-to-cypressがざっくり上書きする!
-
yarn add -D @babel/core @babel/preset-env @bahmutov/add-typescript-to-cypress babel-loader typescript webpackを追加でインストール -
plugins/index.jsの内容が変わっているので、追加内容があったのなら編集 - cypress.jsonに
"supportFile": "cypress/support/index.ts"を追加(パスはcypress.jsonの位置から見て相対で) -
support/index.jsをsupport/index.tsに -
support/commands.jsをsupport/commands.tsに -
support/commands.ts内にdeclare globalを記載する -
support/commands.ts内にexport {}を記載する
後は通常通りcypressを走らせられるし、IntelliSenseも効く