Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mochaをnode:testに変更する #1737

Open
wants to merge 4 commits into
base: v6-node-updates
Choose a base branch
from

Conversation

windchime-yk
Copy link

@windchime-yk windchime-yk commented Apr 1, 2024

ref: #1717
これはテキスト修正のみの対応になります。
実装側の修正については、#1698 が終わり次第着手する予定です。

修正内容としては、表題の通りMochaからnode:testへの変更になります。
変更に際し、以下を考慮しました。

  • Mocha互換のdescribe/itも実装されていましたが、Node.js公式ドキュメントではtestが主流とされていたため、そちらに寄せました
  • Mochaではテストフォルダまでコマンド実行時に指定していますが、node --test test/だとmacOS上でエラーとなるため、デフォルトでtestフォルダが指定されていることも加味してnode --testとしました
  • コマンドライン上で表示されるパスは、本ユースケース内を参照して、Users/laco/nodecli/に統一しています

Preview

@bot-user
Copy link

bot-user commented Apr 1, 2024

Deploy Preview for js-primer ready!

Name Link
🔨 Latest commit 0530871
🔍 Latest deploy log https://app.netlify.com/sites/js-primer/deploys/661d4f10117a930008d9b78a
😎 Deploy Preview https://deploy-preview-1737--js-primer.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@azu
Copy link
Collaborator

azu commented Apr 2, 2024

関連しそうなので一応 cc @yossydev @jp-knj @SotaYamaguchi

@azu azu requested a review from lacolaco April 2, 2024 00:08
この記述により、`npm test`コマンドを実行すると、`mocha`コマンドで`test/`ディレクトリにあるテストファイルを実行します。
試しに`npm test`コマンドを実行し、Mochaによるテストが行われることを確認しましょう。
まだテストファイルを作っていないので、`Error: No test files found`というエラーが表示されます。
この記述により、`npm test`コマンドを実行すると、`node --test`で`test/`ディレクトリにあるテストファイルを実行します。
Copy link
Collaborator

@azu azu Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://nodejs.org/api/test.html#running-tests-from-the-command-line
node --test もデフォルトで test/** 見るんですね。
コマンドからtest/ を省く場合は、デフォルトで test/ を探索する的な説明があった方がいい気はしますね(自明ではないので、mochaの場合は意図的にtest/を入れていたんだと思います)

まだテストファイルを作っていないので、`Error: No test files found`というエラーが表示されます
この記述により、`npm test`コマンドを実行すると、`node --test`で`test/`ディレクトリにあるテストファイルを実行します。
試しに`npm test`コマンドを実行し、テストが行われることを確認しましょう
まだテストファイルを作っていないので、テストが何も走らず正常終了します
Copy link
Collaborator

@azu azu Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

テストが何も走らず正常終了します。

Mochaと違って正常終了なんですね。

"テストが何も走らず"というのが、node --test 自体が何も動いてないように見えるので、
0個のテストが実行されて正常終了 という感じの言い方の方もありかな。

  • 実行するテストがないため正常終了します
  • 0個のテストが実行されて正常終了します
  • テストを実行した結果は0件となり正常終了します
$ node --test
ℹ tests 0
ℹ suites 0
ℹ pass 0
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 27.760042

$ echo $?
0

```

## ユニットテストを記述する {#write-unit-test}

テストの実行環境ができたので、実際にユニットテストを記述します。
Mochaのユニットテストは`test`ディレクトリの中にJavaScriptファイルを配置して記述します。
Node.jsのユニットテストは`test`ディレクトリの中にJavaScriptファイルを配置して記述します。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Node.jsのユニットテスト"というと広いので、 node:test でのユニットテスト な気はするので、

[testモジュール][]でのユニットテストはtestディレクトリの中にJavaScriptファイルを配置して記述します。

とかなのかなとは思います。

testディレクトリを探索するのはTest Runnerなので、ちょっと言い方が難しいですが…

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かに言い方が難しいですね……。
こちらについては言い方を考えてみます! ご指摘ありがとうございます!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@azu
こちらについて書き方を考えていたのですが、句末に4回記述します。が続いているのが気になったため、以下のように修正するのはいかがでしょうか?

記述する際には、testディレクトリの中にJavaScriptファイルを配置します。

全体的にはこういった修正を考えております。

テストの実行環境ができたので、実際にユニットテストを記述します。
- Node.jsのユニットテストは`test`ディレクトリの中にJavaScriptファイルを配置して記述します。
+ 記述する際には、`test`ディレクトリの中にJavaScriptファイルを配置します。
`test/md2html-test.js`ファイルを作成し、`md2html.js`に対するユニットテストを次のように記述します。

- `test`関数は第一引数にテストのタイトルを入れ、第二引数にテストの内容を記述します。
+ `test`関数は第一引数にテストのタイトルを入れ、第二引数にテストの内容を入れます。

Copy link
Collaborator

@azu azu Apr 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

良いと思います

`test`関数は第一引数にテストのタイトルを入れ、第二引数にテストの内容を書きます。

入れるだと枠っぽいので、実際には自由記述なので、書くや記述が良いかなと思います

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど、ありがとうございます! 書くで修正させていただきます!


ユニットテストを実行するには、Mochaが提供する`mocha`コマンドを使います。
Mochaをインストールした後、`package.json`の`scripts`プロパティを次のように記述します。
ユニットテストを実行するには、Node.jsが提供する`node`コマンドに`--test`オプションを付与して使います。
Copy link
Collaborator

@lacolaco lacolaco Apr 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ユニットテストを実行するには、Node.jsが提供する`node`コマンドに`--test`オプションを付与して使います
ユニットテストを実行するには、Node.jsが提供する`node`コマンドを`--test`オプションを付与して実行します

「使います」の対象がわかりにくかったので「nodeコマンドを実行する」を軸にしてみましたが、「を」お連続がこれはこれで気持ち悪いですね

Suggested change
ユニットテストを実行するには、Node.jsが提供する`node`コマンドに`--test`オプションを付与して使います
ユニットテストを実行するには、Node.jsが提供する`node`コマンドの`--test`オプションを使います

このほうがいいかも?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

レビューありがとうございます!
私としても下のほうが読みやすいと感じるので、そちらで修正させていただきます!

@windchime-yk
Copy link
Author

@azu @lacolaco
レビューありがとうございました!
ご指摘をいただいた部分を修正しましたので、再レビューをお願いします! 🙇

Copy link
Collaborator

@azu azu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

他はLGTMです

残り

  • コードの変更
  • 依存の変更

Comment on lines 87 to 89

$ echo $?
0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここは省略していいかなと思います。

Suggested change
$ echo $?
0

Windowsでは動かないため。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windowsを失念しておりました! ご指摘ありがとうございます!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@azu
ご指摘の点を修正しました!

Copy link
Collaborator

@lacolaco lacolaco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTMです

@windchime-yk windchime-yk requested a review from azu April 15, 2024 16:03
@azu
Copy link
Collaborator

azu commented Apr 16, 2024

先にコードと依存の変更までやってマージしちゃっていい気がしてきました

@windchime-yk
Copy link
Author

@azu
すみません、通知に埋もれていて気づくのが遅れてしまいました!
今見た限りですと前段のutils.parseArgsが未着手なようなので、先にこちらの変更を進めようと思います!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants