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?

VisualStudioCodeでJestのデバッグ構成を設定する

Posted at

■はじめに

Jestでテストを実行するにあたって、テストコードとアプリケーション側のコードをデバッグしたかったため、VSCodeでデバッグする構成を作りました。

■Jestのデバッグ構成設定

スクリーンショット 2025-03-16 18.38.10.png

VSCodeで「command + ,」を押すと、設定画面が表示されるので、「settings」で検索します。

そうすると、「settings.jsonで編集」というリンクが表示されるのでクリックします。

スクリーンショット 2025-03-16 18.39.19.png

// デバッグ構成
"launch": {
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "[Jest] 全てのテストを実行",
            "program": "${workspaceFolder}/node_modules/.bin/jest",
            "args": ["--runInBand"],
            "console": "externalTerminal",
            "internalConsoleOptions": "neverOpen",
            "windows": {
                "program": "${workspaceFolder}/node_modules/jest/bin/jest"
            }
        },
        {
            "type": "node",
            "request": "launch",
            "name": "[Jest] 現在のファイルを実行",
            "program": "${workspaceFolder}/node_modules/.bin/jest",
            "args": ["--runTestsByPath", "${relativeFile}", "--config", "jest.config.ts"],
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen",
            "windows": {
                "program": "${workspaceFolder}/node_modules/jest/bin/jest"
            }
        }
    ]
},

settings.jsonにデバッグ構成のコードを貼り付けます。

■VSCodeでテストコードをデバッグしてみる

スクリーンショット 2025-03-16 18.46.06.png

先ほど設定したデバッグ構成は、VSCodeの虫マーク内に表示されます。

今回は「現在のファイルを実行」というデバッグ構成で実行してみます。

「全てのテストを実行」の場合は、文字通り全てのテストが実行されます

スクリーンショット 2025-03-16 18.49.45.jpg

テストファイルを開いて、デバッグしたいコードの左側をクリックすると、赤丸が表示されます。

この赤丸をつけた部分がブレークポイントで処理が止まる部分になります。

赤丸をつけた状態で、左上の緑三角マークをクリックすると、現在開いているファイル(OtherUtils.test.ts)が実行されて、赤丸をつけたコードで処理が止まります。

サイドバーや変数名にhoverすることで、ブレークポイント時点での変数の中身や引数を確認することができます。

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?