LoginSignup
0
0

More than 1 year has passed since last update.

メモ:node.jsのjestでユニットテスト

Posted at

pythonのユニットテストpytestは軽く見たので、node.jsも見ておく。
現時点だとjestというのが一番良さそう。
[公式日本語版}(https://jestjs.io/ja/docs/getting-started)ぽいのや
ここここ
を確認しながら。

インストール

単体だとnpm install jestだが
npm install typescript jest @types/jest ts-jestで入れた。
実際の開発現場だと--save-devしてるのかも
27.4.5で入った。

jest -initコマンドを流すのだが

C:\nodejs\jest>.\node_modules\.bin\jest --init
Could not find a "package.json" file in C:\nodejs\jest

とpackage.jsonがないとエラーになるようなので

package.json
{
  "scripts": {
    "test": "jest"
  }
}

を作成してから実行、y/nの選択肢が出てくるがエンター連打で良さそう。

実行テスト

適当なtarget.jsを作成してテストしてみる。
exportで出力する形の.js(もしくは.ts)をテストするのが基本のようなので公式に倣う。

中身は公式のスクリプトとほぼ同じもの。

target.js
function sum(a, b) {
    return a + b;
  }
module.exports = sum;

テストは__tests__フォルダを作成して、target.test.jsを作成
特に設定をしない場合、jestは自動的にtests以下からxxxx.test.jsを見つける。

target.test.js
const sum = require('../target');

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);
});

これでnpm testを実行する事でTests: 1 passed, 1 totalが出たのでOK

実際の使い方

比較式

expect(実行).toBe(答え)
で基本的な関数のテストは行う事が可能。
この時===で比較するので注意。値比較で良いなら.toEqualも使える。
○○以外ではない事を確認する事は少なそうだが、expect(x).not.toBe(y)を使う。
その他toBeNull,toBeTruthy,toBeFalsyなどが使える。
文字列はtoMatchで正規表現比較が可能。配列にはtoContain。
javascriptの正規表現はここ等で。

小数の比較の場合は丸め誤差(0.1+0.2と0.3の比較など)が影響するので
toBeCloseToを使うと無視できる。

例外が出るかどうかについては
expect(() => compileAndroidCode()).toThrow();
のようにラムダ式+toThrowで判定

プロパティを見るにはtoHaveProperty

この辺りが主な判定方法で、全てを確認する場合はドキュメントを見る。

非同期

非同期系のコードをテストする場合の情報はここを参照。
Async/Awaitを確認。
jsonはhttp://jsonplaceholder.typicode.com/postsにアクセスして取得する。
node.jsのrequestが非推奨になっているので代わりにnpm install axios
プロキシはオプションで設定する。

target.js
const { assertJSXAttribute } = require("@babel/types");
const axios = require("axios");

const proxyConfig = { proxy: { host: 'hostname', port: '8080' } }

function sum(a, b) {
    return a + b;
  }

async function  getjson(n){
    return await axios.get("http://jsonplaceholder.typicode.com/posts/"+n ,proxyConfig );
}

 module.exports = getjson;

テスト側でも普通にasync,awaitを使う。

target.test.js
const getjson = require('../target');


test('json', async () => {
  const res = await getjson(3)
  expect(res.data.id).toBe(3);
});

事前処理

beforeとafterを利用する

beforeEach(() => {
  initializeDatabase();
});

afterEach(() => {
  clearDatabase();
});

モック

公式の他
実装についてはここここ考え方含めてはこことかが参考になるかも?

関数の場合はjest.fnを利用する。

const mockCallback = jest.fn(x => 42 + x);
const mockFunc = jest.fn().mockImplementation(() => "mock func");

のようにする。

ライブラリの場合は

jest.mock("random"); // jest.mockでモックを使う宣言をして

test('should fetch users', () => {
  const users = [{name: 'Bob'}];
  const resp = {data: users};
  axios.get.mockResolvedValue(resp); // getをCallした時にmockが返す値をセット

  // もしくはmockImplementation
  // or you could use the following depending on your use case:
  // axios.get.mockImplementation(() => Promise.resolve(resp))

  // Usersクラスのstaticなallをチェック
  return Users.all().then(data => expect(data).toEqual(users));
});

基本はなんとなくわかった気になった。

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