diff --git a/.flowconfig b/.flowconfig index fb8bb253300b..50c6167168cc 100644 --- a/.flowconfig +++ b/.flowconfig @@ -1,6 +1,7 @@ [ignore] .*/node_modules/conventional-changelog-core/.* .*/node_modules/fbjs/.* +.*/node_modules/jest-validate/.* .*/node_modules/react-native/.* .*/vendor/jsonlint/.* .*/examples/.* diff --git a/packages/jest-config/package.json b/packages/jest-config/package.json index 80bb3dc0895c..f25d5cb06639 100644 --- a/packages/jest-config/package.json +++ b/packages/jest-config/package.json @@ -12,8 +12,8 @@ "glob": "^7.1.1", "jest-environment-jsdom": "^20.0.1", "jest-environment-node": "^20.0.1", + "jest-get-type": "^20.0.1", "jest-jasmine2": "^20.0.1", - "jest-matcher-utils": "^20.0.1", "jest-regex-util": "^20.0.1", "jest-resolve": "^20.0.1", "jest-validate": "^20.0.1", diff --git a/packages/jest-config/src/reporterValidationErrors.js b/packages/jest-config/src/reporterValidationErrors.js index e084555bc8fe..24aa32b42c8d 100644 --- a/packages/jest-config/src/reporterValidationErrors.js +++ b/packages/jest-config/src/reporterValidationErrors.js @@ -14,7 +14,7 @@ import type {ReporterConfig} from 'types/Config'; const {ValidationError} = require('jest-validate'); const chalk = require('chalk'); -const {getType} = require('jest-matcher-utils'); +const getType = require('jest-get-type'); const {DOCUMENTATION_NOTE, BULLET} = require('./utils'); const validReporterTypes = ['array', 'string']; diff --git a/packages/jest-diff/package.json b/packages/jest-diff/package.json index 28279183ab78..e58ee08426a2 100644 --- a/packages/jest-diff/package.json +++ b/packages/jest-diff/package.json @@ -11,7 +11,7 @@ "dependencies": { "chalk": "^1.1.3", "diff": "^3.2.0", - "jest-matcher-utils": "^20.0.1", + "jest-get-type": "^20.0.1", "pretty-format": "^20.0.1" } } diff --git a/packages/jest-diff/src/index.js b/packages/jest-diff/src/index.js index fc499ccea100..7fccb7610877 100644 --- a/packages/jest-diff/src/index.js +++ b/packages/jest-diff/src/index.js @@ -21,7 +21,7 @@ const { } = require('pretty-format').plugins; const chalk = require('chalk'); -const {getType} = require('jest-matcher-utils'); +const getType = require('jest-get-type'); const prettyFormat = require('pretty-format'); const diffStrings = require('./diffStrings'); diff --git a/packages/jest-get-type/.npmignore b/packages/jest-get-type/.npmignore new file mode 100644 index 000000000000..c8ab4c8d108a --- /dev/null +++ b/packages/jest-get-type/.npmignore @@ -0,0 +1,4 @@ +**/__mocks__/** +**/__tests__/** +src +yarn.lock diff --git a/packages/jest-get-type/package.json b/packages/jest-get-type/package.json new file mode 100644 index 000000000000..000476a78e28 --- /dev/null +++ b/packages/jest-get-type/package.json @@ -0,0 +1,12 @@ +{ + "name": "jest-get-type", + "description": "A utility function to get the type of a value", + "version": "20.0.1", + "repository": { + "type": "git", + "url": "https://github.com/facebook/jest.git" + }, + "license": "BSD-3-Clause", + "main": "build/index.js", + "browser": "build-es5/index.js" +} diff --git a/packages/jest-get-type/src/__tests__/index-test.js b/packages/jest-get-type/src/__tests__/index-test.js new file mode 100644 index 000000000000..d8e938d2b4c2 --- /dev/null +++ b/packages/jest-get-type/src/__tests__/index-test.js @@ -0,0 +1,28 @@ +/** + * Copyright (c) 2014, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @emails oncall+jsinfra + */ + +'use strict'; + +const getType = require('..'); + +describe('.getType()', () => { + test('null', () => expect(getType(null)).toBe('null')); + test('undefined', () => expect(getType(undefined)).toBe('undefined')); + test('object', () => expect(getType({})).toBe('object')); + test('array', () => expect(getType([])).toBe('array')); + test('number', () => expect(getType(1)).toBe('number')); + test('string', () => expect(getType('oi')).toBe('string')); + test('function', () => expect(getType(() => {})).toBe('function')); + test('boolean', () => expect(getType(true)).toBe('boolean')); + test('symbol', () => expect(getType(Symbol.for('a'))).toBe('symbol')); + test('regexp', () => expect(getType(/abc/)).toBe('regexp')); + test('map', () => expect(getType(new Map())).toBe('map')); + test('set', () => expect(getType(new Set())).toBe('set')); +}); diff --git a/packages/jest-get-type/src/index.js b/packages/jest-get-type/src/index.js new file mode 100644 index 000000000000..571527364a92 --- /dev/null +++ b/packages/jest-get-type/src/index.js @@ -0,0 +1,61 @@ +/** + * Copyright (c) 2014, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @flow + */ + +'use strict'; + +export type ValueType = + | 'array' + | 'boolean' + | 'function' + | 'null' + | 'number' + | 'object' + | 'regexp' + | 'map' + | 'set' + | '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 (typeof 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'; + } + 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; diff --git a/packages/jest-matcher-utils/package.json b/packages/jest-matcher-utils/package.json index 5088c6f5a4a7..c8f1f2e6a128 100644 --- a/packages/jest-matcher-utils/package.json +++ b/packages/jest-matcher-utils/package.json @@ -11,6 +11,7 @@ "browser": "build-es5/index.js", "dependencies": { "chalk": "^1.1.3", + "jest-get-type": "^20.0.1", "pretty-format": "^20.0.1" } } diff --git a/packages/jest-matcher-utils/src/__tests__/index-test.js b/packages/jest-matcher-utils/src/__tests__/index-test.js index b09eaa33a4a6..8c4450f3571c 100644 --- a/packages/jest-matcher-utils/src/__tests__/index-test.js +++ b/packages/jest-matcher-utils/src/__tests__/index-test.js @@ -10,13 +10,7 @@ 'use strict'; -const { - stringify, - getType, - ensureNumbers, - pluralize, - ensureNoExpected, -} = require('../'); +const {stringify, ensureNumbers, pluralize, ensureNoExpected} = require('../'); describe('.stringify()', () => { [ @@ -93,21 +87,6 @@ describe('.stringify()', () => { }); }); -describe('.getType()', () => { - test('null', () => expect(getType(null)).toBe('null')); - test('undefined', () => expect(getType(undefined)).toBe('undefined')); - test('object', () => expect(getType({})).toBe('object')); - test('array', () => expect(getType([])).toBe('array')); - test('number', () => expect(getType(1)).toBe('number')); - test('string', () => expect(getType('oi')).toBe('string')); - test('function', () => expect(getType(() => {})).toBe('function')); - test('boolean', () => expect(getType(true)).toBe('boolean')); - test('symbol', () => expect(getType(Symbol.for('a'))).toBe('symbol')); - test('regexp', () => expect(getType(/abc/)).toBe('regexp')); - test('map', () => expect(getType(new Map())).toBe('map')); - test('set', () => expect(getType(new Set())).toBe('set')); -}); - describe('.ensureNumbers()', () => { test('dont throw error when variables are numbers', () => { expect(() => { diff --git a/packages/jest-matcher-utils/src/index.js b/packages/jest-matcher-utils/src/index.js index 7ceca1f9964b..cf9f2f1de62b 100644 --- a/packages/jest-matcher-utils/src/index.js +++ b/packages/jest-matcher-utils/src/index.js @@ -11,6 +11,7 @@ 'use strict'; const chalk = require('chalk'); +const getType = require('jest-get-type'); const prettyFormat = require('pretty-format'); const { AsymmetricMatcher, @@ -23,20 +24,6 @@ const PLUGINS = [AsymmetricMatcher, ReactElement, HTMLElement].concat( Immutable, ); -export type ValueType = - | 'array' - | 'boolean' - | 'function' - | 'null' - | 'number' - | 'object' - | 'regexp' - | 'map' - | 'set' - | 'string' - | 'symbol' - | 'undefined'; - const EXPECTED_COLOR = chalk.green; const EXPECTED_BG = chalk.bgGreen; const RECEIVED_COLOR = chalk.red; @@ -59,40 +46,6 @@ const NUMBERS = [ 'thirteen', ]; -// get the type of a value with handling the edge cases like `typeof []` -// and `typeof null` -const getType = (value: any): ValueType => { - if (typeof 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'; - } - 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}`); -}; - const stringify = (object: any, maxDepth?: number = 10): string => { const MAX_LENGTH = 10000; let result; @@ -213,7 +166,6 @@ module.exports = { ensureExpectedIsNumber, ensureNoExpected, ensureNumbers, - getType, highlightTrailingWhitespace, matcherHint, pluralize, diff --git a/packages/jest-matchers/package.json b/packages/jest-matchers/package.json index eb3c63df558e..ced56505ee68 100644 --- a/packages/jest-matchers/package.json +++ b/packages/jest-matchers/package.json @@ -10,6 +10,7 @@ "browser": "build-es5/index.js", "dependencies": { "jest-diff": "^20.0.1", + "jest-get-type": "^20.0.1", "jest-matcher-utils": "^20.0.1", "jest-message-util": "^20.0.1", "jest-regex-util": "^20.0.1" diff --git a/packages/jest-matchers/src/matchers.js b/packages/jest-matchers/src/matchers.js index 8ac81245a237..ec7f2458ed4c 100644 --- a/packages/jest-matchers/src/matchers.js +++ b/packages/jest-matchers/src/matchers.js @@ -13,13 +13,13 @@ import type {MatchersObject} from 'types/Matchers'; const diff = require('jest-diff'); +const getType = require('jest-get-type'); const {escapeStrForRegex} = require('jest-regex-util'); const { EXPECTED_COLOR, RECEIVED_COLOR, ensureNoExpected, ensureNumbers, - getType, matcherHint, printReceived, printExpected, diff --git a/packages/jest-matchers/src/toThrowMatchers.js b/packages/jest-matchers/src/toThrowMatchers.js index 5bdd1bfdbc4e..d7596fac8b1b 100644 --- a/packages/jest-matchers/src/toThrowMatchers.js +++ b/packages/jest-matchers/src/toThrowMatchers.js @@ -12,6 +12,7 @@ import type {MatchersObject} from 'types/Matchers'; +const getType = require('jest-get-type'); const {escapeStrForRegex} = require('jest-regex-util'); const { formatStackTrace, @@ -20,7 +21,6 @@ const { const { RECEIVED_BG, RECEIVED_COLOR, - getType, highlightTrailingWhitespace, matcherHint, printExpected, diff --git a/packages/jest-validate/package.json b/packages/jest-validate/package.json index a951126422cf..edad05d68e63 100644 --- a/packages/jest-validate/package.json +++ b/packages/jest-validate/package.json @@ -10,7 +10,7 @@ "browser": "build-es5/index.js", "dependencies": { "chalk": "^1.1.3", - "jest-matcher-utils": "^20.0.1", + "jest-get-type": "^20.0.1", "leven": "^2.1.0", "pretty-format": "^20.0.1" } diff --git a/packages/jest-validate/src/errors.js b/packages/jest-validate/src/errors.js index c9797d43ae8e..96a610fd6425 100644 --- a/packages/jest-validate/src/errors.js +++ b/packages/jest-validate/src/errors.js @@ -13,7 +13,7 @@ import type {ValidationOptions} from './types'; const chalk = require('chalk'); -const {getType} = require('jest-matcher-utils'); +const getType = require('jest-get-type'); const {format, ValidationError, ERROR} = require('./utils'); const errorMessage = (