"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も効く