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 2 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
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: any, maxDepth: number = 10): string => {
SimenB marked this conversation as resolved.
Show resolved Hide resolved
const MAX_LENGTH = 10000;
let result;

Expand Down Expand Up @@ -184,7 +188,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"}
]
}