Skip to content

eddie0329/jest-to-be-typed

Repository files navigation

πŸŽ‰ 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