【node.js】jestを使って単体テストできるまでの手順 | 瀬戸内の雲のように

【node.js】jestを使って単体テストできるまでの手順

Posted: 2020-05-17


スポンサーリンク

目次

背景

私は元々サーバエンジニアですが、一応railsやpythonの開発経験はあり、ちゃんと動くコードを書くことは一応できます。
そんな私の自己評価としてプロのプログラマーと一番大きな差は テストをしっかり行うこと(そのための経験や考え方) だと考えています。(要するに正常系しかテストしてない)

ということで、やや話は飛びますが今回はnode.jsで単体テストを実行できるようにするまでの手順をメモします。

 

今回使うモジュール

jest というモジュールを使います。 nodejs 単体テスト などでググると色々な記事が出てきましたが、このjestが一番良さそうだと感じました。

  • reactのテストに向いている(どちらもfacebookが開発)
  • モックも使える
  • カバレッジを出せる

※他のでもできるんだとは思いますが、使い勝手が良さそうだったのと記事が多そうだったので。

 

インストール手順

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にテストを追記

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.

以上となります。


スポンサーリンク




コメント一覧

2022/06/19 06:46:16

azithromycin tablets さん

buy priligy online buy generic priligy uk la pastilla priligy es segura how nuch time between dapoxetine and sildenafil


2022/06/19 07:29:37

Cwiwqbさん

oral cenforce 50mg - domperidone brand order motilium pill


2022/06/20 10:33:28

Gisexdさん

generic viagra - sildenafil for women order tadalafil 5mg online cheap


2022/06/20 16:55:45

Wnbtdkさん

sildenafil order - sildenafil 50mg tablets cialis 5mg oral


2022/06/21 22:09:16

Ufygsiさん

generic provigil 200mg - brand budesonide budesonide oral


2022/06/22 03:01:35

how to get cialis さん

chemist warehouse cialis viagra canada easiest way to get viagra where to buy viagra in sydney


2022/06/22 04:35:32

Wblztwさん

modafinil buy online - prednisone 5mg uk rhinocort drug


2022/06/22 07:10:13

kamagra polo 100mg さん

low dose cialis [url=http://australiaviagra.today/#]how long does viagra last? [/url] cialis 5 mg chemist warehouse how to order viagra from canada


2022/06/22 13:35:08

bxjkfcuiさん

hydroxychloroquine over the counter canada plaquenil medication plaquenil 200mg friends program


2022/06/23 15:17:23

Oyaibeさん

buy accutane 10mg for sale - buy zithromax 500mg generic tetracycline order online


2022/06/23 23:20:54

Xgkrwaさん

isotretinoin online buy - azithromycin 250mg for sale tetracycline 500mg cost


2022/06/25 16:05:47

Onebrzさん

buy cyclobenzaprine for sale - inderal for sale order inderal 10mg generic


2022/06/25 08:16:44

Juxwjjさん

cyclobenzaprine 15mg tablet - gloperba cheap buy propranolol online cheap


2022/06/26 20:04:05

viagra cenforce 25 さん

kamagra porno faut il une ordonnance pour le kamagra comparaison kamagra cialis levitra spedra ou commander du kamagra en france


2022/06/26 22:00:36

cialis 5mg australia さん

furosemide tablet furosemide tablets furosemide pka of carboxyl group what to watch for in a patient taking lasix


2022/06/27 01:52:55

Psnenoさん

purchase clopidogrel sale - plavix price buy reglan 10mg generic


2022/06/27 09:35:46

Ryzutpさん

buy clopidogrel 150mg sale - warfarin 2mg uk order reglan 10mg generic


2022/06/28 18:58:58

Pqkqgyさん

order losartan generic - buy promethazine 25mg phenergan over the counter


2022/04/08 04:38:25

Wtyuazさん

order topiramate 100mg online - buy topamax 100mg sale sumatriptan 25mg cost


2022/04/09 13:31:28

Luzmzeさん

order avodart 0.5mg without prescription - generic avodart 0.5mg female cialis pill


2022/04/11 19:48:56

Mwbauhさん

where to buy otc ed pills - erection pills that work prednisone 20mg brand


2022/04/12 20:43:19

Ulanygさん

cheap accutane 20mg - cheap amoxicillin 1000mg amoxicillin 250mg cost


2022/04/14 01:33:31

Lflhxqさん

lasix oral - lasix 40mg us azithromycin price


2022/04/15 02:15:42

Lwkqdwさん

buy doxycycline 200mg pill - buy doxycycline 100mg pill generic chloroquine


2022/05/20 04:58:33

qfbqrthoさん

modafinil oral


2022/04/22 01:20:17

Abcyemさん

order tadalafil - Generic cialis viagra 50mg price


2022/04/23 06:43:28

Ckjzpaさん

order provigil 100mg without prescription - viagra 150mg price viagra for men over 50


2022/04/24 05:00:44

Ymacvtさん

erection pills that work - erectal disfunction buy deltasone 40mg pill


2022/04/26 11:46:35

Qjiccjさん

flomax 0.4mg price - buy flomax without prescription buy aldactone 100mg pills


2022/04/27 18:24:41

Ktfvfxさん

buy zocor for sale - buy zocor without prescription online assignment help


2022/04/28 21:30:29

Efqlaoさん

online casino with free signup bonus real money usa - casino slots do my term paper


2022/04/30 05:45:23

Yryeztさん

online casinos - ampicillin 500mg sale ampicillin online buy


2022/05/01 05:54:33

Sqjskgさん

ciprofloxacin pills - cialis 40mg cheap buy tadalafil 40mg sale


2022/05/02 05:42:44

Cnwtghさん

viagra 150mg generic - buy vardenafil with no prescription generic vardenafil online


2022/05/02 17:34:27

Tqhmsdさん

viagra 50mg price - cheap sildenafil 100mg buy sildenafil pill


2022/05/07 09:25:30

Exljmsさん

prednisolone 40mg pills - tadalafil 40mg usa generic tadalafil


2022/05/07 20:36:57

Tdsfoqさん

prednisolone 10mg without prescription - purchase tadalafil cialis 5 mg


2022/05/09 19:42:01

Vvoanaさん

purchase augmentin pills - generic cialis 5mg cialis 20mg over the counter


2022/05/10 08:19:39

Bbrasyさん

order augmentin 625mg generic - order tadalafil online cheap cheap cialis 40mg


2022/05/12 09:19:39

Jtfeklさん

purchase bactrim generic - order sildenafil 50mg for sale purchase sildenafil online cheap


2022/05/12 19:49:43

Udorspさん

sulfamethoxazole price - buy viagra 150mg sildenafil pills 25mg


2022/05/14 15:40:45

Snlaxcさん

cheap cephalexin 125mg - erythromycin sale order generic erythromycin 250mg


2022/05/15 03:13:25

Dnoupqさん

cephalexin 125mg cheap - cheap cleocin purchase erythromycin sale


2022/05/18 00:18:08

hverwvxnさん

buy provigil 200mg online cheap provigil 200mg drug


2022/05/18 00:56:23

xqpsspxoさん

buy provigil 200mg online order provigil pills


2022/05/18 06:29:19

gzwjmykwさん

order provigil 200mg for sale


2022/05/18 10:43:06

Dfkqywさん

rhinocort sale - budesonide for sale antabuse 250mg without prescription


2022/05/18 11:25:17

ydiawpkbさん

provigil 100mg ca purchase provigil order modafinil sale


2022/05/18 17:36:03

Izducdさん

buy rhinocort generic - buy disulfiram 250mg antabuse without prescription


2022/05/19 22:19:59

Jlffmxさん

buy cefuroxime pills - cefuroxime 500mg price tadalafil 5mg without prescription


2022/05/20 05:02:53

Mdfvfhさん

buy cefuroxime 250mg pills - cialis 10mg ca cialis 5mg pill


2022/05/20 12:32:18

jrjrtwrkさん

uses for erythromycin ophthalmic ointment http://erythromycinn.com/#


2022/05/20 20:42:01

Nishaknappさん

The quality and quantity of work produced here are absolutely informative. Thanks for sharing. My site:: 안전한토토사이트


2022/05/20 20:06:49

Nishaknappさん

The quality and quantity of work produced here are absolutely informative. Thanks for sharing. 토토 커뮤니티


2022/05/20 20:13:14

Nishaknappさん

The quality and quantity of work produced here are absolutely informative. Thanks for sharing. My site:: 먹튀검증사이트


2022/05/21 09:21:43

Hojfytさん

ampicillin over the counter - buy ampicillin pill cialis 10mg ca


2022/05/21 15:08:17

afailfnyさん

modafinil 100mg over the counter


2022/05/21 15:54:32

Vaxcbjさん

order ampicillin 500mg online - order cipro cialis 20mg cost


2022/05/22 03:00:22

mvnjigzmさん

provigil 100mg pills order modafinil 100mg for sale


2022/05/22 03:35:37

uwxmmvdbさん

buy provigil pills modafinil 100mg cost buy modafinil online cheap


2022/05/22 10:42:46

vzcdznuxさん

buy modafinil 200mg online modafinil 100mg usa buy modafinil 200mg generic


2022/05/22 15:18:19

Nishaknappさん

The quality and quantity of work produced here are absolutely informative. Thanks for sharing. My site:: 검증사이트


2022/05/22 15:21:49

Nishaknappさん

The quality and quantity of work produced here are absolutely informative. Thanks for sharing. My site:: 토토안전놀이터


2022/05/22 15:19:35

Nishaknappさん

The quality and quantity of work produced here are absolutely informative. Thanks for sharing. My site:: 사설토토사이트추천


2022/05/22 16:39:27

pxgbdcteさん

order modafinil online cheap modafinil cost


2022/05/23 08:50:53

Joogzvさん

buy amoxil 500mg pill - cost levitra 20mg order levitra online


2022/05/23 16:35:20

Rkckjcさん

amoxicillin 500mg uk - buy amoxil 1000mg generic buy vardenafil generic


2022/05/24 21:46:00

Ocrkjgさん

ivermectin 5ml - ivermectin nz purchase levitra pill


2022/05/24 21:53:54

xiktyrynさん

erythromycin ointment eye https://erythromycin1m.com/#


2022/05/26 08:34:41

Kbwuvgさん

buy doxycycline 200mg online cheap - clomid uk tadalafil liquid


2022/05/26 15:30:58

Gkansqさん

doxycycline 200mg tablet - buy cialis 5mg online cheap tadalafil 40mg oral


2022/05/28 05:48:18

Lfbnwpさん

oral tadalafil - online drugs provigil 100mg us


2022/05/28 09:48:44

apnkhbtrさん

erythromycin topical https://erythromycin1m.com/#


2022/05/28 12:09:30

Rcaddqさん

tadalafil for sale - cialis india provigil 100mg brand


2022/05/29 14:59:12

Ydddybさん

buy prednisone 40mg sale - herbal ed pills isotretinoin 40mg without prescription


2022/05/29 21:11:25

Eyppuqさん

buy deltasone 10mg generic - buy accutane isotretinoin medication


2022/05/31 00:01:47

Yhxyczさん

purchase amoxil for sale - azithromycin 250mg cheap viagra sale


2022/05/31 06:12:39

Lkxmbxさん

order amoxil sale - amoxil 1000mg over the counter pfizer viagra


2022/05/31 18:07:50

ggejjccrさん

http://erythromycinn.com/# erythromycin for dogs


2022/06/01 10:55:47

Dmvfpzさん

buy prednisolone 10mg pill - order prednisolone 40mg pills viagra next day delivery


2022/06/01 19:04:09

Bnlnurさん

prednisolone 20mg tablet - prednisolone 20mg without prescription over the counter viagra


2022/06/03 03:23:50

Dthibcさん

buy lasix 100mg pills - buy lasix ivermectin medicine


2022/06/03 04:23:52

Slonnogさん

0nr26

9sjg9

1wq9


2022/06/03 10:43:16

Ugbsejさん

order furosemide generic - buy lasix 100mg generic generic stromectol online


2022/06/04 06:30:33

udmvuzujさん

http://erythromycinn.com/# erythromycin generic name


2022/06/04 18:49:52

Mlfwibさん

plaquenil 400mg cost - order baricitinib online order baricitinib 2mg


2022/06/05 02:09:47

Lrmvicさん

buy generic plaquenil - hydroxychloroquine 200mg canada cost baricitinib 2mg


2022/06/06 08:33:52

Ejyswbさん

buy metformin 500mg pill - buy lipitor 40mg for sale order norvasc


2022/06/06 15:37:15

Hjhbxxさん

order glucophage 1000mg pill - order norvasc 5mg generic amlodipine price


2022/06/07 20:17:00

uduxwhegさん

erythromycin for dogs erythromycin ophthalmic solution


2022/06/07 22:13:06

Rivvdtさん

lisinopril 5mg oral - purchase lisinopril online cheap buy atenolol pills


2022/06/08 04:56:40

Emxjjeさん

brand lisinopril 2.5mg - buy omeprazole 20mg pill buy tenormin 50mg online


2022/06/08 22:10:04

Slonnogさん

8c6n2

r4yjp

un9k


2022/06/09 09:18:33

Ojbqapさん

vardenafil 10mg price - order clomiphene generic order clomid for sale


2022/06/09 16:26:00

Zqojudさん

buy levitra generic - order pregabalin 75mg order clomiphene 50mg without prescription


2022/06/11 01:23:26

Tetrgaさん

order ventolin 2mg online cheap - buy a research paper online dapoxetine price


2022/06/11 07:45:03

Qjtfozさん

buy albuterol pill - dapoxetine over the counter buy dapoxetine sale


2022/06/11 07:51:32

amxevqepさん

erythromycin ointment for newborns erythromycin overdose


2022/06/12 12:50:17

Zuwbkaさん

order synthroid online cheap - buy clarinex generic hydroxychloroquine 400mg uk


2022/06/12 19:44:55

Nzdkcrさん

cheap levothyroxine for sale - buy hydroxychloroquine 200mg generic plaquenil 400mg price


2022/06/14 00:38:27

Uvabloさん

buy cialis 10mg pill - buy cialis 10mg generic sildenafil 25 mg


2022/06/14 07:06:17

Pbgrjgさん

online cialis - order cialis 10mg pills brand viagra


2022/06/15 10:13:22

duqrbxoyさん

buy plaquenil online cheap alberta order plaquenil online


2022/06/15 21:02:57

Xylzhvさん

prednisone 5mg uk - xenical 60mg tablet order generic amoxil 1000mg


2022/06/16 02:52:29

Lvxpojさん

order prednisone pill - amoxil 500mg pills amoxicillin 1000mg oral


2022/06/16 05:24:43

buy priligy 30mg さん

cialis review how to get cialis prescription what is cialis for daily use what are the effects of cialis


2022/06/16 08:14:09

rx tizanidine さん

levitra generika 10mg preisvergleich levitra 20mg sildenafil viagra vardenafil levitra and tadalafil cialis levitra nebenwirkungen


2022/06/18 23:40:39

swisvyzpさん

buy plaquenil over the counter where can one get hydroxychloroquine plaquenil cheap


2022/06/19 00:52:40

Njbxtzさん

order generic cenforce - buy stromectol 3mg motilium online order



コメントを投稿する


お名前


コメント内容





TOP back