2
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?

npm testについて npm run testとの違い

Posted at

npm testとは

npm testとはnode.js環境でよく使用するコマンドである。

  • どのようなコマンド動作にするかは開発者側で決められる
  • testには任意のコマンドを指定できる(任意のコマンドをnpm testに置き換えられる)
  • 指定せずにこのコマンドを使っても何も返ってこない

npm testを動作させるために何をする?

  1. package.jsonをアクティブ化し
  2. 必要なパッケージをインストールし
  3. package.jsonの内容を一部(test)を定義する部分を書き換える

実際の流れ
(今回は"cowsay"というかわいい馬を表示するコマンドを用いるけど、任意のコマンドで大丈夫だよ)

  1. "npm init"をコマンドラインに入力、実行
    英文がたくさん表示されるが無視してEnterキーを押しまくる
  2. "npm install cowsay --save-dev"を実行
  3. package.jsonを
before
"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
after
"scripts": {
  "test": "cowsay"
},

このように変更する
なお、"test"以外にも任意のスクリプトを指定することができる。
が、任意のスクリプトを使用する際は注意が必要である(後述)。

コマンドラインに"npm test hello"と入力すると...?

_______
< hello >
-------
      \   ^__^
       \  (oo)\_______
          (__)\       )\/\
              ||----w |
              ||     ||

このようにかわいい馬が表示される!

npm testとnpm run testの違い

基本的にはこの2つのコマンドに大きな差異はない。
しかし、以降のことはしっかり頭に入れておく必要がある。

"test"と"start"は特別なスクリプトである。
"npm test"や"npm start"とコマンド入力すると動く。

しかし、"tests"や"star"と設定すると
"npm tests", "npm star"は動かない。

これらの通常のスクリプトを使う場合は、
"npm run tests", "npm run star"
と記述する必要がある。

つまり、基本的には"npm run (スクリプト名)"
と記述する必要があるのだ。

なお、"npm test"と"npm run test"は同じ出力結果を示す。

2
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
2
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?