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?

EclipseでJavascriptでJestを実行する方法

0
Posted at

今回は、Eclipse2025のIDEを用いて、Spring Boot(Maven)プロジェクトにおいてJavascriptのJestを実行してみます。
JavaScript / Jest は Maven 管理ではなく Node.js 管理になります。

ディレクトリ構成

frontendフォルダを作成しておきましょう。
ポイント
1.Spring Boot Jestは ディレクトリ分離がベスト
2.Maven npmは役割を分ける

spring-boot-project
├─ pom.xml
├─ src/main/java      ← Spring Boot
├─ src/main/resources
│  └─ static
│     └─ js           ← JavaScript(テスト対象)
├─ frontend           ← ★ Node.js & Jest 用ディレクトリ
│  ├─ package.json
│  ├─ node_modules
│  ├─ src
│  │  └─ sum.js
│  └─ tests
│     └─ sum.test.js

image.png

frontend(Node.js用)ディレクトリを作成

1.プロジェクト右クリック
2.NewFolder
3.名前:frontend

frontendでnpm初期化 & Jestインストール

Eclipseのターミナルで

cd frontend
npm init -y
npm install --save-dev jest

image.png

image.png

package.jsonの編集

npm init -y を実行した直後のpackage.jsonには、デフォルトで次のような設定が入っています:

package.json(一部抜粋)
"scripts": {
  "test": "echo \"Error: no test specified\" && exit 1"
}

つまり、「テストは未設定です」と表示して わざと失敗するようになっています。
なので、コマンドnpm testを入力すると下記のエラーがターミナルに表示されます。

> frontend@1.0.0 test
> echo "Error: no test specified" && exit 1

"Error: no test specified"

解決方法
frontend/package.json を開く
scripts.test jestに変更する

package.json(修正前)
"scripts": {
  "test": "echo \"Error: no test specified\" && exit 1"
}
package.json(修正後)
"scripts": {
  "test": "jest"
}

もう一度、npm testコマンドを入力すると下記が表示されます。
image.png

さいごに、変更後と変更前のコードの差異を記載します。
変更前

package.json
{
  "name": "frontend",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "jest": "^30.2.0"
  }
}

変更後

package.json
{
  "name": "frontend",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "jest": "^30.2.0"
  }
}

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?