スポンサーリンク
私は元々サーバエンジニアですが、一応railsやpythonの開発経験はあり、ちゃんと動くコードを書くことは一応できます。
そんな私の自己評価としてプロのプログラマーと一番大きな差は テストをしっかり行うこと(そのための経験や考え方) だと考えています。(要するに正常系しかテストしてない)
ということで、やや話は飛びますが今回はnode.jsで単体テストを実行できるようにするまでの手順をメモします。
jest
というモジュールを使います。 nodejs 単体テスト
などでググると色々な記事が出てきましたが、このjestが一番良さそうだと感じました。
※他のでもできるんだとは思いますが、使い勝手が良さそうだったのと記事が多そうだったので。
npmでインストールします。
$ npm install --save-dev jest
以下のような結果が帰ってくればOK。
npm WARN blog@1.0.0 No description
+ jest@26.0.1
added 300 packages from 184 contributors and audited 991 packages in 27.705s
今回テストされるコードはこちら
// hello.js
module.exports = function() {
return "hello world";
};
テストするコードはこちら
// hello.test.js
const hello = require("./hello.js");
test("check hello", () => {
expect(hello()).toBe("hello world");
});
package.jsonにテスト用のスクリプトの記述を追加します。
"scripts": {
"test": "jest" //ここを追記
},
これで準備はOK。
では実際にテストを行ってみます。
$ npm run test
以下のような結果が帰ってくればOKです。
> blog@1.0.0 test
> jest
PASS ./hello.test.js
✓ check hello (4 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 2.546 s
Ran all test suites.
以上となります。
スポンサーリンク
2021/03/19 17:36:13
https://vskamagrav.com/ - order kamagra online
2021/04/08 01:47:46
Each article ought to be quite a brief article of about 500 words. You have to create in sentences and utilize appropriate punctuation, grammar, and punctuation. 2. Your composition needs to answer the problem that is assigned, drawing the reading mission that is appropriate. https://jorstore.com/writing-thesis-statements/