1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Jest】スキップするファイルをファイル名で指定したい

1
Last updated at Posted at 2026-01-07

はじめに

Jestを勉強している間、ファイル名でスキップしたいファイルを制御できないのか気になり調べました。
例:ファイル名の先頭にアンダーバーがついたファイル(_sample.spec.jsなど)はテスト対象外にする

問題

jestの初期設定では、ファイル名でスキップすることはできません。

解決方法

jest.config.mjsにテストスキップしたいファイル名(今回の場合は、アンダーバーが先頭につくファイル名)を設定します。

jest.config.mjs
export default {
  testEnvironment: "jsdom",
  moduleNameMapper: {
    "\\.(css|less)$": "identity-obj-proxy",
  },
  setupFilesAfterEnv: ["./jest.setup.js"],
+  testPathIgnorePatterns: ["/node_modules/", "/_.*\\.spec\\.js$"],
};

testPathIgnorePatternsはデフォルトで["/node_modules/"]が設定されています。
今回testPathIgnorePatternsを明示的に指定したのでデフォルト設定が上書きされます。そのため、["/node_modules/"]も忘れず明記してください。

おわりに

今回"スキップ"という表現をしていますが、厳密にはテスト対象外にするファイルを指定しています。そのため、Jestの実行結果には、アンダーバーが先頭につくファイルをスキップしたことが表現されません。(テスト対象の分母として含まれていないからです)

なので、実際のテストの際はスキップしたことがわかるように、test.skipを使ってください。

参考

JISOUのメンバー募集中!

プログラミングコーチングJISOUでは、新たなメンバーを募集しています。
日本一のアウトプットコミュニティでキャリアアップしませんか?
興味のある方は、ぜひホームページをのぞいてみてください!
▼▼▼

1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?