/jest-to-be-typed

Jest matcher that allows you to test the expected type of value.

Primary LanguageJavaScriptMIT LicenseMIT

🎉 Jest To Be Typed

version downloads MIT License PRs Welcome

Jest matcher that allows you to test the expected type of value.

🔧 Installation

With npm

npm i jest-to-be-typed

With yarn

yarn add jest-to-be-typed

📦 Setup

Jest >v24

Add jest-to-be-typed to your Jest setupFilesAfterEnv configuration. See for help

"jest": {
  "setupFilesAfterEnv": ["jest-to-be-typed"]
}

Jest <v23

"jest": {
  "setupTestFrameworkScriptFile": "jest-to-be-typed"
}

OR

Simply import toBeTyped

// *.test.js
import toBeTyped from 'jest-to-be-typed'

expect.extend(toBeTyped);

✏️ Usage

There are two kinds of modes.

Default mode:

Default mode checks typeof expected value.

  expect('').toBeTyped('string');
  expect({}).toBeTyped('object');
  expect(1).toBeTyped('number');
  expect(false).toBeTyped('boolean');
  expect(Symbol('foobar')).toBeTyped('symbol');
  expect(() => {}).toBeTyped('function');
  expect([]).toBeTyped('array');
  expect(/foobar/).toBeTyped('regexp');
  expect(new RegExp('foobar')).toBeTyped('regexp');
  expect(null).toBeTyped('null');
  expect(undefined).toBeTyped('undefined');
  expect(new Map()).toBeTyped('map');
  expect(new Set()).toBeTyped('set');
  expect(new Date()).toBeTyped('date');
  expect(Promise.resolve([])).resolves.toBeTyped('array');

Advanced mode:

Advanced mode is somelike typescript interface.

  const data = {
    name: 'eddie',
    age: 13,
    address: [],
    isMarried: false
  };
  const types = {
    name: 'string',
    age: 'number',
    address: 'array',
    isMarried: 'boolean'
  };
  expect(data).toBeTyped(types);

📄 License

MIT

💡 Reference

Inspired by jest-tobetype