diff --git a/CHANGELOG.md b/CHANGELOG.md index d26f7237dbde..6d0474da0917 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/packages/jest-each/src/bind.js b/packages/jest-each/src/bind.js index b09a0dff20f3..8b8d95aa4f54 100644 --- a/packages/jest-each/src/bind.js +++ b/packages/jest-each/src/bind.js @@ -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>; @@ -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 { @@ -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})); }; diff --git a/packages/jest-get-type/src/__tests__/index.test.ts b/packages/jest-get-type/src/__tests__/getType.test.ts similarity index 97% rename from packages/jest-get-type/src/__tests__/index.test.ts rename to packages/jest-get-type/src/__tests__/getType.test.ts index 8a34a0cb947d..de4bec59d8db 100644 --- a/packages/jest-get-type/src/__tests__/index.test.ts +++ b/packages/jest-get-type/src/__tests__/getType.test.ts @@ -6,7 +6,7 @@ * */ -import getType from '..'; +import getType from '../'; describe('.getType()', () => { test('null', () => expect(getType(null)).toBe('null')); diff --git a/packages/jest-get-type/src/__tests__/isPrimitive.test.ts b/packages/jest-get-type/src/__tests__/isPrimitive.test.ts new file mode 100644 index 000000000000..d3e9a7175af5 --- /dev/null +++ b/packages/jest-get-type/src/__tests__/isPrimitive.test.ts @@ -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); + }, + ); +}); diff --git a/packages/jest-get-type/src/index.ts b/packages/jest-get-type/src/index.ts index dd15b71be771..cfea04eaf48a 100644 --- a/packages/jest-get-type/src/index.ts +++ b/packages/jest-get-type/src/index.ts @@ -20,9 +20,18 @@ type ValueType = | 'symbol' | 'undefined'; +const PRIMITIVES = new Set([ + '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) { @@ -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;