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

chore: migrate jest-matcher-util to TypeScript #7835

Merged
merged 3 commits into from Feb 8, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,7 @@
- `[jest-diff]`: Migrate to TypeScript ([#7824](https://github.com/facebook/jest/pull/7824))
- `[jest-leak-detector]`: Migrate to TypeScript ([#7825](https://github.com/facebook/jest/pull/7825))
- `[jest-changed-files]`: Migrate to TypeScript ([#7827](https://github.com/facebook/jest/pull/7827))
- `[jest-matcher-utils]`: Migrate to TypeScript ([#7835](https://github.com/facebook/jest/pull/7835))

### Performance

Expand Down
1 change: 1 addition & 0 deletions packages/jest-circus/src/formatNodeAssertErrors.js
Expand Up @@ -11,6 +11,7 @@
import type {DiffOptions} from 'jest-diff';
import type {Event, State} from 'types/Circus';

// $FlowFixMe: Converted to TS
import {diff, printExpected, printReceived} from 'jest-matcher-utils';
import chalk from 'chalk';
// $FlowFixMe: Converted to TS
Expand Down
Expand Up @@ -37,7 +37,7 @@ describe('.stringify()', () => {
});

test('circular references', () => {
const a = {};
const a: any = {};
a.a = a;
expect(stringify(a)).toBe('{"a": [Circular]}');
});
Expand Down Expand Up @@ -75,8 +75,8 @@ describe('.stringify()', () => {
});

test('reduces maxDepth if stringifying very large objects', () => {
const big = {a: 1, b: {}};
const small = {a: 1, b: {}};
const big: any = {a: 1, b: {}};
const small: any = {a: 1, b: {}};
for (let i = 0; i < 10000; i += 1) {
big.b[i] = 'test';
}
Expand All @@ -93,18 +93,21 @@ describe('.stringify()', () => {
describe('.ensureNumbers()', () => {
test('dont throw error when variables are numbers', () => {
expect(() => {
// @ts-ignore
ensureNumbers(1, 2);
}).not.toThrow();
});

test('throws error when expected is not a number', () => {
expect(() => {
// @ts-ignore
ensureNumbers(1, 'not_a_number');
}).toThrowErrorMatchingSnapshot();
});

test('throws error when received is not a number', () => {
expect(() => {
// @ts-ignore
ensureNumbers('not_a_number', 3);
}).toThrowErrorMatchingSnapshot();
});
Expand All @@ -113,6 +116,7 @@ describe('.ensureNumbers()', () => {
describe('.ensureNoExpected()', () => {
test('dont throw error when undefined', () => {
expect(() => {
// @ts-ignore
ensureNoExpected(undefined);
}).not.toThrow();
});
Expand Down
Expand Up @@ -3,12 +3,8 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

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

import chalk from 'chalk';
import jestDiff from 'jest-diff';
import getType from 'jest-get-type';
Expand All @@ -31,6 +27,14 @@ const PLUGINS = [
AsymmetricMatcher,
];

export type MatcherHintOptions = {
comment?: string;
isDirectExpectCall?: boolean;
isNot?: boolean;
promise?: string;
secondArgument?: string;
};

export const EXPECTED_COLOR = chalk.green;
export const RECEIVED_COLOR = chalk.red;
const DIM_COLOR = chalk.dim;
Expand Down Expand Up @@ -60,7 +64,7 @@ export const SUGGEST_TO_CONTAIN_EQUAL = chalk.dim(
'Looks like you wanted to test for object/array equality with the stricter `toContain` matcher. You probably need to use `toContainEqual` instead.',
);

export const stringify = (object: any, maxDepth?: number = 10): string => {
export const stringify = (object: unknown, maxDepth: number = 10): string => {
const MAX_LENGTH = 10000;
let result;

Expand All @@ -87,15 +91,15 @@ export const stringify = (object: any, maxDepth?: number = 10): string => {
export const highlightTrailingWhitespace = (text: string): string =>
text.replace(/\s+$/gm, chalk.inverse('$&'));

export const printReceived = (object: any) =>
export const printReceived = (object: unknown) =>
RECEIVED_COLOR(highlightTrailingWhitespace(stringify(object)));
export const printExpected = (value: any) =>
export const printExpected = (value: unknown) =>
EXPECTED_COLOR(highlightTrailingWhitespace(stringify(value)));

export const printWithType = (
name: string, // 'Expected' or 'Received'
value: any,
print: (value: any) => string, // printExpected or printReceived
value: unknown,
print: (value: unknown) => string, // printExpected or printReceived
) => {
const type = getType(value);
const hasType =
Expand All @@ -107,7 +111,7 @@ export const printWithType = (
};

export const ensureNoExpected = (
expected: any,
expected: unknown,
matcherName: string,
options?: MatcherHintOptions,
) => {
Expand All @@ -126,7 +130,7 @@ export const ensureNoExpected = (
}
};

export const ensureActualIsNumber = (actual: any, matcherName: string) => {
export const ensureActualIsNumber = (actual: unknown, matcherName: string) => {
matcherName || (matcherName = 'This matcher');
if (typeof actual !== 'number') {
throw new Error(
Expand All @@ -139,7 +143,10 @@ export const ensureActualIsNumber = (actual: any, matcherName: string) => {
}
};

export const ensureExpectedIsNumber = (expected: any, matcherName: string) => {
export const ensureExpectedIsNumber = (
expected: unknown,
matcherName: string,
) => {
matcherName || (matcherName = 'This matcher');
if (typeof expected !== 'number') {
throw new Error(
Expand All @@ -153,8 +160,8 @@ export const ensureExpectedIsNumber = (expected: any, matcherName: string) => {
};

export const ensureNumbers = (
actual: any,
expected: any,
actual: unknown,
expected: unknown,
matcherName: string,
) => {
ensureActualIsNumber(actual, matcherName);
Expand All @@ -164,7 +171,7 @@ export const ensureNumbers = (
// Sometimes, e.g. when comparing two numbers, the output from jest-diff
// does not contain more information than the `Expected:` / `Received:` already gives.
// In those cases, we do not print a diff to make the output shorter and not redundant.
const shouldPrintDiff = (actual: any, expected: any) => {
const shouldPrintDiff = (actual: unknown, expected: unknown) => {
if (typeof actual === 'number' && typeof expected === 'number') {
return false;
}
Expand All @@ -184,7 +191,7 @@ export const pluralize = (word: string, count: number) =>
// return function which given each string, returns the label:
// string, colon, space, and enough padding spaces to align the value.

type PrintLabel = string => string;
type PrintLabel = (string: string) => string;

export const getLabelPrinter = (...strings: Array<string>): PrintLabel => {
const maxLength = strings.reduce(
Expand Down
12 changes: 12 additions & 0 deletions packages/jest-matcher-utils/tsconfig.json
@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build"
},
"references": [
{"path": "../jest-diff"},
{"path": "../jest-get-type"},
{"path": "../pretty-format"}
]
}