0
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でもRSpecのように行番号を指定してテストを実行したい

Last updated at Posted at 2025-09-17

やりたいこと

RSpecでは

$ rspec hoge_spec.rb:50

というように、行番号を指定して、その行を含むテストのみ実行できる。これと同じことをJestでもしたい。

実現方法

Jestにはそのような機能は無い。
2017年にissueが上がって「interesting」と言われているが、今に至るまで実装されていない。

VSCodeのJest Runnerという拡張を使うと、CodeLensでエディタ内に「Run」「Debug]というリンクが表示され、クリックするとそのテストを実行できる。

そこで、Jest Runnerのソースを流用させてもらい、テストのファイル名だけ抽出して表示するスクリプトを作った:jestname.js

あとはそのテスト名を-tで指定してjestを実行するスクリプトを作ればよい。

例:(環境や好みに応じて書き換えていただきたい)

#!/bin/sh

filename="$1"
linenum="$2"
name=$(node jestname.js "$filename:$linenum")
if [ "$name" != "" ]; then
  npx jest "$filename" -t "$name"
fi
0
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
0
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?