Skip to content

Commit

Permalink
[jest-get-type] Add isPrimitive function (#7708)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattphillips authored and SimenB committed Feb 28, 2019
1 parent df3eb5e commit 5d2d46f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
- `[expect]`: Improve report when matcher fails, part 10 ([#7960](https://github.com/facebook/jest/pull/7960))
- `[pretty-format]` Support `React.memo` ([#7891](https://github.com/facebook/jest/pull/7891))
- `[jest-config]` Print error information on preset normalization error ([#7935](https://github.com/facebook/jest/pull/7935))
- `[jest-haste-map]` Add "skipPackageJson" option
- `[jest-haste-map]` Add `skipPackageJson` option ([#7778](https://github.com/facebook/jest/pull/7778))
- `[jest-get-type]` Add `isPrimitive` function ([#7708](https://github.com/facebook/jest/pull/7708))

### Fixes

Expand Down
14 changes: 3 additions & 11 deletions packages/jest-each/src/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import util from 'util';
import chalk from 'chalk';
import pretty from 'pretty-format';
import getType from 'jest-get-type';
import {isPrimitive} from 'jest-get-type';
import {ErrorWithStack} from 'jest-util';

type Table = Array<Array<any>>;
Expand All @@ -24,13 +24,6 @@ const RECEIVED_COLOR = chalk.red;
const SUPPORTED_PLACEHOLDERS = /%[sdifjoOp%]/g;
const PRETTY_PLACEHOLDER = '%p';
const INDEX_PLACEHOLDER = '%#';
const PRIMITIVES = new Set([
'string',
'number',
'boolean',
'null',
'undefined',
]);

export default (cb: Function, supportsDone: boolean = true) => (...args: any) =>
function eachBind(title: string, test: Function, timeout: number): void {
Expand Down Expand Up @@ -203,10 +196,9 @@ const getMatchingKeyPaths = title => (matches, key) =>
const replaceKeyPathWithValue = data => (title, match) => {
const keyPath = match.replace('$', '').split('.');
const value = getPath(data, keyPath);
const valueType = getType(value);

if (PRIMITIVES.has(valueType)) {
return title.replace(match, value);
if (isPrimitive(value)) {
return title.replace(match, String(value));
}
return title.replace(match, pretty(value, {maxDepth: 1, min: true}));
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
*/

import getType from '..';
import getType from '../';

describe('.getType()', () => {
test('null', () => expect(getType(null)).toBe('null'));
Expand Down
25 changes: 25 additions & 0 deletions packages/jest-get-type/src/__tests__/isPrimitive.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/

import {isPrimitive} from '..';

describe('.isPrimitive()', () => {
test.each([null, undefined, 100, 'hello world', true, Symbol.for('a')])(
'returns true when given primitive value of: %s',
primitive => {
expect(isPrimitive(primitive)).toBe(true);
},
);

test.each([{}, [], () => {}, /abc/, new Map(), new Set(), new Date()])(
'returns false when given non primitive value of: %s',
value => {
expect(isPrimitive(value)).toBe(false);
},
);
});
15 changes: 13 additions & 2 deletions packages/jest-get-type/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,18 @@ type ValueType =
| 'symbol'
| 'undefined';

const PRIMITIVES = new Set<ValueType>([
'string',
'number',
'boolean',
'null',
'undefined',
'symbol',
]);

// get the type of a value with handling the edge cases like `typeof []`
// and `typeof null`
const getType = (value: unknown): ValueType => {
function getType(value: unknown): ValueType {
if (value === undefined) {
return 'undefined';
} else if (value === null) {
Expand Down Expand Up @@ -55,6 +64,8 @@ const getType = (value: unknown): ValueType => {
}

throw new Error(`value of unknown type: ${value}`);
};
}

getType.isPrimitive = (value: unknown) => PRIMITIVES.has(getType(value));

export = getType;

0 comments on commit 5d2d46f

Please sign in to comment.