Jest
anything
null と undefined 以外のものにマッチする
expect(...).toEqual(expect.anything())
any
そのコンストラクタで生成されたものにマッチする
expect(...).toEqual(expect.any(Number))
jest.mock
よく使い方がわからない代表
たぶん globals.moduleName を再帰的にたどって mock に置き換える
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() と組み合わせて使うと楽
diagnostic
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: {
},
},
},
};
jest が遅いやつ
with @testing-library/