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

Move getType from jest-matcher-utils to separate package #3559

Merged
merged 1 commit into from
May 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions packages/jest-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"glob": "^7.1.1",
"jest-environment-jsdom": "^20.0.3",
"jest-environment-node": "^20.0.3",
"jest-get-type": "^20.0.3",
"jest-jasmine2": "^20.0.4",
"jest-matcher-utils": "^20.0.3",
"jest-regex-util": "^20.0.3",
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 @@ -12,7 +12,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
1 change: 1 addition & 0 deletions packages/jest-diff/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"chalk": "^1.1.3",
"diff": "^3.2.0",
"jest-matcher-utils": "^20.0.3",
"jest-get-type": "^20.0.3",
"pretty-format": "^20.0.3"
}
}
2 changes: 1 addition & 1 deletion packages/jest-diff/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,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.3",
"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.3",
"pretty-format": "^20.0.3"
}
}
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 @@ -9,6 +9,7 @@
*/

const chalk = require('chalk');
const getType = require('jest-get-type');
const prettyFormat = require('pretty-format');
const {
AsymmetricMatcher,
Expand All @@ -21,20 +22,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 @@ -57,40 +44,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 @@ -211,7 +164,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.3",
"jest-get-type": "^20.0.3",
"jest-matcher-utils": "^20.0.3",
"jest-message-util": "^20.0.3",
"jest-regex-util": "^20.0.3"
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 @@ -11,13 +11,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 @@ -10,6 +10,7 @@

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

const getType = require('jest-get-type');
const {escapeStrForRegex} = require('jest-regex-util');
const {
formatStackTrace,
Expand All @@ -18,7 +19,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.3",
"jest-get-type": "^20.0.3",
"leven": "^2.1.0",
"pretty-format": "^20.0.3"
}
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 @@ -11,7 +11,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