From a9622654d5db05f5d1f2a0d6d8222cc1dbe7070f Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Fri, 25 Jan 2019 17:48:38 +0000 Subject: [PATCH 01/10] Move getType to named file with ES6 import/export --- .../{index.test.js => getType.test.js} | 4 +- packages/jest-get-type/src/getType.js | 63 +++++++++++++++++++ packages/jest-get-type/src/index.js | 55 +--------------- 3 files changed, 66 insertions(+), 56 deletions(-) rename packages/jest-get-type/src/__tests__/{index.test.js => getType.test.js} (95%) create mode 100644 packages/jest-get-type/src/getType.js diff --git a/packages/jest-get-type/src/__tests__/index.test.js b/packages/jest-get-type/src/__tests__/getType.test.js similarity index 95% rename from packages/jest-get-type/src/__tests__/index.test.js rename to packages/jest-get-type/src/__tests__/getType.test.js index 8390b9a4bfd6..19c7bfacb7d0 100644 --- a/packages/jest-get-type/src/__tests__/index.test.js +++ b/packages/jest-get-type/src/__tests__/getType.test.js @@ -6,9 +6,7 @@ * */ -'use strict'; - -const getType = require('..'); +import getType from '../getType'; describe('.getType()', () => { test('null', () => expect(getType(null)).toBe('null')); diff --git a/packages/jest-get-type/src/getType.js b/packages/jest-get-type/src/getType.js new file mode 100644 index 000000000000..e1cfecaaa634 --- /dev/null +++ b/packages/jest-get-type/src/getType.js @@ -0,0 +1,63 @@ +/** + * 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. + * + * @flow + */ + +'use strict'; + +export type ValueType = + | 'array' + | 'boolean' + | 'function' + | 'null' + | 'number' + | 'object' + | 'regexp' + | 'map' + | 'set' + | 'date' + | 'string' + | 'symbol' + | 'undefined'; + +// get the type of a value with handling the edge cases like `typeof []` +// and `typeof null` +const getType = (value: any): ValueType => { + if (value === undefined) { + return 'undefined'; + } else if (value === null) { + return 'null'; + } else if (Array.isArray(value)) { + return 'array'; + } else if (typeof value === 'boolean') { + return 'boolean'; + } else if (typeof value === 'function') { + return 'function'; + } else if (typeof value === 'number') { + return 'number'; + } else if (typeof value === 'string') { + return 'string'; + } else if (typeof value === 'object') { + if (value.constructor === RegExp) { + return 'regexp'; + } else if (value.constructor === Map) { + return 'map'; + } else if (value.constructor === Set) { + return 'set'; + } else if (value.constructor === Date) { + return 'date'; + } + return 'object'; + // $FlowFixMe https://github.com/facebook/flow/issues/1015 + } else if (typeof value === 'symbol') { + return 'symbol'; + } + + throw new Error(`value of unknown type: ${value}`); +}; + +export default getType; diff --git a/packages/jest-get-type/src/index.js b/packages/jest-get-type/src/index.js index 3a444dd6f84d..dee90d8fe811 100644 --- a/packages/jest-get-type/src/index.js +++ b/packages/jest-get-type/src/index.js @@ -7,57 +7,6 @@ * @flow */ -'use strict'; +import getType from './getType'; -export type ValueType = - | 'array' - | 'boolean' - | 'function' - | 'null' - | 'number' - | 'object' - | 'regexp' - | 'map' - | 'set' - | 'date' - | 'string' - | 'symbol' - | 'undefined'; - -// get the type of a value with handling the edge cases like `typeof []` -// and `typeof null` -const getType = (value: any): ValueType => { - if (value === undefined) { - return 'undefined'; - } else if (value === null) { - return 'null'; - } else if (Array.isArray(value)) { - return 'array'; - } else if (typeof value === 'boolean') { - return 'boolean'; - } else if (typeof value === 'function') { - return 'function'; - } else if (typeof value === 'number') { - return 'number'; - } else if (typeof value === 'string') { - return 'string'; - } else if (typeof value === 'object') { - if (value.constructor === RegExp) { - return 'regexp'; - } else if (value.constructor === Map) { - return 'map'; - } else if (value.constructor === Set) { - return 'set'; - } else if (value.constructor === Date) { - return 'date'; - } - return 'object'; - // $FlowFixMe https://github.com/facebook/flow/issues/1015 - } else if (typeof value === 'symbol') { - return 'symbol'; - } - - throw new Error(`value of unknown type: ${value}`); -}; - -module.exports = getType; +export default getType; From 32ef665d2d277e76bd2cebbad9b73fd1fed0a8fd Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Fri, 25 Jan 2019 17:49:18 +0000 Subject: [PATCH 02/10] Add isPrimitive function --- .../src/__tests__/isPrimitive.test.js | 25 +++++++++++++++++++ packages/jest-get-type/src/index.js | 1 + packages/jest-get-type/src/isPrimitive.js | 21 ++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 packages/jest-get-type/src/__tests__/isPrimitive.test.js create mode 100644 packages/jest-get-type/src/isPrimitive.js diff --git a/packages/jest-get-type/src/__tests__/isPrimitive.test.js b/packages/jest-get-type/src/__tests__/isPrimitive.test.js new file mode 100644 index 000000000000..0088cb896f82 --- /dev/null +++ b/packages/jest-get-type/src/__tests__/isPrimitive.test.js @@ -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 '../isPrimitive'; + +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.js b/packages/jest-get-type/src/index.js index dee90d8fe811..351c7bc62071 100644 --- a/packages/jest-get-type/src/index.js +++ b/packages/jest-get-type/src/index.js @@ -10,3 +10,4 @@ import getType from './getType'; export default getType; +export {default as isPrimitive} from './isPrimitive'; diff --git a/packages/jest-get-type/src/isPrimitive.js b/packages/jest-get-type/src/isPrimitive.js new file mode 100644 index 000000000000..e882249a4c39 --- /dev/null +++ b/packages/jest-get-type/src/isPrimitive.js @@ -0,0 +1,21 @@ +/** + * 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. + * + * @flow + */ + +import getType from './getType'; + +const PRIMITIVES = new Set([ + 'string', + 'number', + 'boolean', + 'null', + 'undefined', + 'symbol', +]); + +export default (value: any): boolean => PRIMITIVES.has(getType(value)); From e971c8c729d03ce22862f4a60b43018baf3aa260 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Fri, 25 Jan 2019 17:50:00 +0000 Subject: [PATCH 03/10] Update jest-each to use jest-get-type isPrimitive function --- packages/jest-each/src/bind.js | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/packages/jest-each/src/bind.js b/packages/jest-each/src/bind.js index b09a0dff20f3..f715aa477f6f 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,9 +196,8 @@ 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)) { + if (typeof value !== 'symbol' && isPrimitive(value)) { return title.replace(match, value); } return title.replace(match, pretty(value, {maxDepth: 1, min: true})); From 4bf9e36508c5268b2a6fbe0979f6521c75c2a6f3 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Fri, 25 Jan 2019 18:05:02 +0000 Subject: [PATCH 04/10] Fix lint/typechecking --- packages/jest-each/src/bind.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/jest-each/src/bind.js b/packages/jest-each/src/bind.js index f715aa477f6f..22c8e5d41e1a 100644 --- a/packages/jest-each/src/bind.js +++ b/packages/jest-each/src/bind.js @@ -197,6 +197,7 @@ const replaceKeyPathWithValue = data => (title, match) => { const keyPath = match.replace('$', '').split('.'); const value = getPath(data, keyPath); + // $FlowFixMe https://github.com/facebook/flow/issues/1015 if (typeof value !== 'symbol' && isPrimitive(value)) { return title.replace(match, value); } From 6fd52fcb63445ac931d053464dadff3ecd4e7577 Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Fri, 25 Jan 2019 18:06:28 +0000 Subject: [PATCH 05/10] Replace symbol check with string casting --- packages/jest-each/src/bind.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/jest-each/src/bind.js b/packages/jest-each/src/bind.js index 22c8e5d41e1a..8b8d95aa4f54 100644 --- a/packages/jest-each/src/bind.js +++ b/packages/jest-each/src/bind.js @@ -197,9 +197,8 @@ const replaceKeyPathWithValue = data => (title, match) => { const keyPath = match.replace('$', '').split('.'); const value = getPath(data, keyPath); - // $FlowFixMe https://github.com/facebook/flow/issues/1015 - if (typeof value !== 'symbol' && isPrimitive(value)) { - return title.replace(match, value); + if (isPrimitive(value)) { + return title.replace(match, String(value)); } return title.replace(match, pretty(value, {maxDepth: 1, min: true})); }; From 23fa298bb6de7b48c42e002258ac7917567e154f Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 28 Feb 2019 14:16:55 +0100 Subject: [PATCH 06/10] avoid breaking change --- packages/jest-get-type/src/index.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/jest-get-type/src/index.ts b/packages/jest-get-type/src/index.ts index b61a5305629e..6c18aeeb1793 100644 --- a/packages/jest-get-type/src/index.ts +++ b/packages/jest-get-type/src/index.ts @@ -5,7 +5,13 @@ * LICENSE file in the root directory of this source tree. */ -import getType from './getType'; +import getTypeFunc from './getType'; +import isPrimitive from './isPrimitive'; -export default getType; -export {default as isPrimitive} from './isPrimitive'; +function getType(value: unknown) { + return getTypeFunc(value); +} + +getType.isPrimitive = isPrimitive; + +export = getType; From 2c9e8163c46b16241287e5545829f7326e486b27 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 28 Feb 2019 14:23:12 +0100 Subject: [PATCH 07/10] changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 From eb297e788f254b4788162580a5b038f8920086f4 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 28 Feb 2019 14:24:39 +0100 Subject: [PATCH 08/10] moar type --- packages/jest-get-type/src/getType.ts | 2 +- packages/jest-get-type/src/isPrimitive.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/jest-get-type/src/getType.ts b/packages/jest-get-type/src/getType.ts index 3c1580a9b093..c35475ec6e07 100644 --- a/packages/jest-get-type/src/getType.ts +++ b/packages/jest-get-type/src/getType.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -type ValueType = +export type ValueType = | 'array' | 'boolean' | 'function' diff --git a/packages/jest-get-type/src/isPrimitive.ts b/packages/jest-get-type/src/isPrimitive.ts index 5449db1448bd..dd4ea519f7cb 100644 --- a/packages/jest-get-type/src/isPrimitive.ts +++ b/packages/jest-get-type/src/isPrimitive.ts @@ -5,9 +5,9 @@ * LICENSE file in the root directory of this source tree. */ -import getType from './getType'; +import getType, {ValueType} from './getType'; -const PRIMITIVES = new Set([ +const PRIMITIVES = new Set([ 'string', 'number', 'boolean', From c1437dfdab46893d212c918f2723bd127350462d Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 28 Feb 2019 14:30:58 +0100 Subject: [PATCH 09/10] reduce diff against master --- .../src/__tests__/getType.test.ts | 2 +- .../src/__tests__/isPrimitive.test.ts | 2 +- packages/jest-get-type/src/getType.ts | 60 ----------------- packages/jest-get-type/src/index.ts | 65 +++++++++++++++++-- packages/jest-get-type/src/isPrimitive.ts | 19 ------ 5 files changed, 62 insertions(+), 86 deletions(-) delete mode 100644 packages/jest-get-type/src/getType.ts delete mode 100644 packages/jest-get-type/src/isPrimitive.ts diff --git a/packages/jest-get-type/src/__tests__/getType.test.ts b/packages/jest-get-type/src/__tests__/getType.test.ts index 19c7bfacb7d0..de4bec59d8db 100644 --- a/packages/jest-get-type/src/__tests__/getType.test.ts +++ b/packages/jest-get-type/src/__tests__/getType.test.ts @@ -6,7 +6,7 @@ * */ -import getType from '../getType'; +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 index 0088cb896f82..d3e9a7175af5 100644 --- a/packages/jest-get-type/src/__tests__/isPrimitive.test.ts +++ b/packages/jest-get-type/src/__tests__/isPrimitive.test.ts @@ -6,7 +6,7 @@ * */ -import isPrimitive from '../isPrimitive'; +import {isPrimitive} from '..'; describe('.isPrimitive()', () => { test.each([null, undefined, 100, 'hello world', true, Symbol.for('a')])( diff --git a/packages/jest-get-type/src/getType.ts b/packages/jest-get-type/src/getType.ts deleted file mode 100644 index c35475ec6e07..000000000000 --- a/packages/jest-get-type/src/getType.ts +++ /dev/null @@ -1,60 +0,0 @@ -/** - * 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. - */ - -export type ValueType = - | 'array' - | 'boolean' - | 'function' - | 'null' - | 'number' - | 'object' - | 'regexp' - | 'map' - | 'set' - | 'date' - | 'string' - | 'symbol' - | 'undefined'; - -// get the type of a value with handling the edge cases like `typeof []` -// and `typeof null` -const getType = (value: unknown): ValueType => { - if (value === undefined) { - return 'undefined'; - } else if (value === null) { - return 'null'; - } else if (Array.isArray(value)) { - return 'array'; - } else if (typeof value === 'boolean') { - return 'boolean'; - } else if (typeof value === 'function') { - return 'function'; - } else if (typeof value === 'number') { - return 'number'; - } else if (typeof value === 'string') { - return 'string'; - } else if (typeof value === 'object') { - if (value != null) { - if (value.constructor === RegExp) { - return 'regexp'; - } else if (value.constructor === Map) { - return 'map'; - } else if (value.constructor === Set) { - return 'set'; - } else if (value.constructor === Date) { - return 'date'; - } - } - return 'object'; - } else if (typeof value === 'symbol') { - return 'symbol'; - } - - throw new Error(`value of unknown type: ${value}`); -}; - -export default getType; diff --git a/packages/jest-get-type/src/index.ts b/packages/jest-get-type/src/index.ts index 6c18aeeb1793..73edf8b58bad 100644 --- a/packages/jest-get-type/src/index.ts +++ b/packages/jest-get-type/src/index.ts @@ -5,13 +5,68 @@ * LICENSE file in the root directory of this source tree. */ -import getTypeFunc from './getType'; -import isPrimitive from './isPrimitive'; +type ValueType = + | 'array' + | 'boolean' + | 'function' + | 'null' + | 'number' + | 'object' + | 'regexp' + | 'map' + | 'set' + | 'date' + | 'string' + | 'symbol' + | 'undefined'; -function getType(value: unknown) { - return getTypeFunc(value); +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` +function getType(value: unknown): ValueType { + if (value === undefined) { + return 'undefined'; + } else if (value === null) { + return 'null'; + } else if (Array.isArray(value)) { + return 'array'; + } else if (typeof value === 'boolean') { + return 'boolean'; + } else if (typeof value === 'function') { + return 'function'; + } else if (typeof value === 'number') { + return 'number'; + } else if (typeof value === 'string') { + return 'string'; + } else if (typeof value === 'object') { + if (value != null) { + if (value.constructor === RegExp) { + return 'regexp'; + } else if (value.constructor === Map) { + return 'map'; + } else if (value.constructor === Set) { + return 'set'; + } else if (value.constructor === Date) { + return 'date'; + } + } + return 'object'; + } else if (typeof value === 'symbol') { + return 'symbol'; + } + + throw new Error(`value of unknown type: ${value}`); } -getType.isPrimitive = isPrimitive; +getType.isPrimitive = (value: unknown): boolean => + PRIMITIVES.has(getType(value)); export = getType; diff --git a/packages/jest-get-type/src/isPrimitive.ts b/packages/jest-get-type/src/isPrimitive.ts deleted file mode 100644 index dd4ea519f7cb..000000000000 --- a/packages/jest-get-type/src/isPrimitive.ts +++ /dev/null @@ -1,19 +0,0 @@ -/** - * 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 getType, {ValueType} from './getType'; - -const PRIMITIVES = new Set([ - 'string', - 'number', - 'boolean', - 'null', - 'undefined', - 'symbol', -]); - -export default (value: unknown): boolean => PRIMITIVES.has(getType(value)); From e5384cfe7c112196a121db524cffc4d65da001c7 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Thu, 28 Feb 2019 14:32:04 +0100 Subject: [PATCH 10/10] remove extra explicit typing --- packages/jest-get-type/src/index.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/jest-get-type/src/index.ts b/packages/jest-get-type/src/index.ts index 73edf8b58bad..cfea04eaf48a 100644 --- a/packages/jest-get-type/src/index.ts +++ b/packages/jest-get-type/src/index.ts @@ -66,7 +66,6 @@ function getType(value: unknown): ValueType { throw new Error(`value of unknown type: ${value}`); } -getType.isPrimitive = (value: unknown): boolean => - PRIMITIVES.has(getType(value)); +getType.isPrimitive = (value: unknown) => PRIMITIVES.has(getType(value)); export = getType;