Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jest-get-type] Add isPrimitive function #7708

Merged
merged 11 commits into from
Feb 28, 2019
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',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've already merged this, but I'm not sure if I agree Symbol is a primitive type?

/cc @mattphillips

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://leanpub.com/understandinges6/read#leanpub-auto-symbols-and-symbol-properties

Symbols are a primitive type introduced in ECMAScript 6, joining the existing primitive types: strings, numbers, booleans, null, and undefined.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the link 🙏

]);

// 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;