jest-asserts
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

jest-asserts

Assertion functions of jest for type guard in TypeScript.

TypeScript の型ガードのための、JEST のアサーション関数群です。

expect(o).toBe~(...)assertToBe~(o, ...)の型で呼ぶようにしただけですが、TypeScript3.7 から導入されたAssertion Functionsを利用して型ガードが利用できるようになっています。

今まで必要になったらその場その場で作っていたんですが、型に合わせて作り直すのが面倒になってきたので Generics で引数の型に合わせてうまくやってくれるようにしてみました。

import * as jestAsserts from 'jest-asserts';

importだけは前もってしておいてください。

    const o = ((): { a: 1 } | { b: 1 } => ({ a: 1 }))();
    // @ts-expect-error ここではエラー
    o.a;
    jestAsserts.assertToHaveProperty(o, 'a');
    // ここではエラーにならない
    o.a;

あわせてexpect(o).not.toBe~(...)を呼ぶだけのassertNotToBe~(o, ...)も用意しています。

    const o = ((): { a: 1 } | { b: 1 } => ({ a: 1 }))();
    // @ts-expect-error ここではエラー
    o.a;
    jestAsserts.assertNotToHaveProperty(o, 'b');
    // ここではエラーにならない
    o.a;

Line up

  • jestAsserts.assert
  • jestAsserts.assertToHaveProperty
  • jestAsserts.assertNotToHaveProperty
  • jestAsserts.assertToBeInstanceOf
  • jestAsserts.assertNotToBeInstanceOf
  • jestAsserts.assertToBeUndefined
  • jestAsserts.assertNotToBeUndefined
  • jestAsserts.assertToBeDefined
  • jestAsserts.assertNotToBeDefined
  • jestAsserts.assertToBeNull
  • jestAsserts.assertNotToBeNull
  • jestAsserts.assertToBeFalsy
  • jestAsserts.assertNotToBeFalsy
  • jestAsserts.assertToBeTruthy
  • jestAsserts.assertNotToBeTruthy
  • jestAsserts.assertToBe
  • jestAsserts.assertNotToBe

Readme

Keywords

none

Package Sidebar

Install

npm i jest-asserts

Weekly Downloads

3

Version

0.2.1

License

none

Unpacked Size

23.2 kB

Total Files

5

Last publish

Collaborators

  • sugoroku-y