Skip to content

Commit

Permalink
Move getType from jest-matcher-utils to separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
wyze committed May 12, 2017
1 parent e4db404 commit c3f3466
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 79 deletions.
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[ignore]
.*/node_modules/conventional-changelog-core/.*
.*/node_modules/fbjs/.*
.*/node_modules/jest-validate/.*
.*/node_modules/react-native/.*
.*/vendor/jsonlint/.*
.*/examples/.*
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/reporterValidationErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-diff/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion packages/jest-diff/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
4 changes: 4 additions & 0 deletions packages/jest-get-type/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**/__mocks__/**
**/__tests__/**
src
yarn.lock
12 changes: 12 additions & 0 deletions packages/jest-get-type/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
28 changes: 28 additions & 0 deletions packages/jest-get-type/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
@@ -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'));
});
61 changes: 61 additions & 0 deletions packages/jest-get-type/src/index.js
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions packages/jest-matcher-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
23 changes: 1 addition & 22 deletions packages/jest-matcher-utils/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@

'use strict';

const {
stringify,
getType,
ensureNumbers,
pluralize,
ensureNoExpected,
} = require('../');
const {stringify, ensureNumbers, pluralize, ensureNoExpected} = require('../');

describe('.stringify()', () => {
[
Expand Down Expand Up @@ -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(() => {
Expand Down
50 changes: 1 addition & 49 deletions packages/jest-matcher-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'use strict';

const chalk = require('chalk');
const getType = require('jest-get-type');
const prettyFormat = require('pretty-format');
const {
AsymmetricMatcher,
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -213,7 +166,6 @@ module.exports = {
ensureExpectedIsNumber,
ensureNoExpected,
ensureNumbers,
getType,
highlightTrailingWhitespace,
matcherHint,
pluralize,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-matchers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-matchers/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-matchers/src/toThrowMatchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import type {MatchersObject} from 'types/Matchers';

const getType = require('jest-get-type');
const {escapeStrForRegex} = require('jest-regex-util');
const {
formatStackTrace,
Expand All @@ -20,7 +21,6 @@ const {
const {
RECEIVED_BG,
RECEIVED_COLOR,
getType,
highlightTrailingWhitespace,
matcherHint,
printExpected,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-validate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-validate/src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down

0 comments on commit c3f3466

Please sign in to comment.