Jest matcher that allows you to test the expected type of value.
npm i jest-to-be-typed
yarn add jest-to-be-typed
Add jest-to-be-typed
to your Jest setupFilesAfterEnv
configuration. See for help
"jest": {
"setupFilesAfterEnv": ["jest-to-be-typed"]
}
"jest": {
"setupTestFrameworkScriptFile": "jest-to-be-typed"
}
OR
Simply import toBeTyped
// *.test.js
import toBeTyped from 'jest-to-be-typed'
expect.extend(toBeTyped);
There are two kinds of modes.
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 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);
Inspired by jest-tobetype