LoginSignup
3
0

More than 5 years have passed since last update.

cypressでintegrationフォルダ以外を対象にしたい時

Posted at

Angularのテストランナーをkarma→cypressに乗り換えたときに
デフォルトのspec探索パスが'/cypress/integration/**.spec.ts'になっているのを変更したい

TL;DR

  • package.jsonのscriptsを変更する
  • cypress.jsonの設定を追加する

対象読者

  • cypressを使う人です

デフォルト設定

cypressをインストールしてすぐだと、cypressフォルダが生成され、
cypress/integration/配下にあるspecファイルだけがテストの実行対象になっているはずです。

cypress
  ├── fixtures
  │   └── example.json
  ├── integration
  │   └── test.spec.ts
  ├── plugins
  │   ├── cy-ts-preprocessor.js
  │   └── index.js
  ├── support
  │   ├── commands.js
  │   ├── index.js
  │   └── po.ts
  └── tsconfig.json

しかし、angularではsrc/app配下にspecファイルが生成されるので、そのspecファイルを対象にしたい・・・

1. package.jsonのscriptsを変更する

cypress runだけだと、cypress/integration配下specファイルだけですが、--specで対象を変更できます。

package.json

{
  "scripts": {
    "test": "cypress run --spec 'src/app/**/*.spec.ts'",
  },
  (以下省略)
}

2. cypress.jsonの設定を追加する

cypressフォルダがデフォルトになっているので、
あとはcypress.jsonで'./'相対パスになるよう追加すれば完了です。

cypress.json

{
  "integrationFolder": "./"
}

すると

src/app
├── app.component.pug
├── app.component.sass
├── app.component.spec.ts
├── app.component.ts
├── app.module.ts
├── favorite-lists
│   ├── favorite-lists.component.pug
│   ├── favorite-lists.component.sass
│   ├── favorite-lists.component.spec.ts
│   ├── favorite-lists.component.ts
│   └── favorite-mock-lists.ts
└── top-toolbar
    ├── top-toolbar.component.pug
    ├── top-toolbar.component.sass
    ├── top-toolbar.component.spec.ts
    └── top-toolbar.component.ts

src/app配下のspecファイルが対象にできました:santa:

参考

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0