Jest
Jest · 🃏快適なJavaScriptのテスト
anything
null と undefined 以外のものにマッチする
expect(...).toEqual(expect.anything())
any
そのコンストラクタで生成されたものにマッチする
expect(...).toEqual(expect.any(Number))
jest.mock
よく使い方がわからない代表
jest/index.ts at 170eee11d03b0ed5c60077982fdbc3bafd403638 · facebook/jest
jest/index.ts at master · facebook/jest
たぶん globals.moduleName を再帰的にたどって mock に置き換える
facebook/jest@master - packages/jest-mock/src/index.ts#L366
Mock Functions · Jest
jest.mock('module') でそのモジュールを mock に置き換える、他の module で import しているのもモックできる
code:mock.ts
import {google} from 'googleapis';
jest.mock('googleapis');
const mocked = google as jest.Mocked<typeof google>;
const datastore = {
projects: {
export: jest.fn().mockReturnValue({data:'mocked'})
},
};
mocked.datastore.mockReturnValue(datastore as any);
hoge() // 中で datastore('v1').projects.export(...) を呼んでいる関数
expect(datastore.projects.export).toBeCalledWith(...)
ts-jest の mocked() と組み合わせて使うと楽
Test helpers | ts-jest
diagnostic
Diagnostics option | ts-jest
jest.config.js の globals.ts-jest.diagnostics にオプションを渡せば無効にできる
ignoreCodes ならこういう感じで
code:jest.config.js
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
rootDir: 'src',
globals: {
'ts-jest': {
diagnostics: {
ignoreCodes: 151001,
},
},
},
};
jest が遅いやつ
Bind mount performance really slow using JEST testing framework · Issue #1358 · docker/for-mac
with @testing-library/
testing-library/jest-dom: Custom jest matchers to test the state of the DOM
#npm #JavaScript #Node #dev