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(expect): remove some dependencies on jest internals #8782

Merged
merged 1 commit into from
Sep 14, 2021
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
133 changes: 4 additions & 129 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
"expect": "^26.4.2",
"extract-zip": "^2.0.1",
"https-proxy-agent": "^5.0.0",
"jest-matcher-utils": "^26.4.2",
"jpeg-js": "^0.4.2",
"mime": "^2.4.6",
"minimatch": "^3.0.3",
Expand Down
8 changes: 2 additions & 6 deletions src/test/matchers/toBeTruthy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
* limitations under the License.
*/

import {
matcherHint,
MatcherHintOptions
} from 'jest-matcher-utils';
import { currentTestInfo } from '../globals';
import type { Expect } from '../types';
import { expectType, pollUntilDeadline } from '../util';
Expand All @@ -35,7 +31,7 @@ export async function toBeTruthy<T>(
throw new Error(`${matcherName} must be called during the test`);
expectType(receiver, receiverType, matcherName);

const matcherOptions: MatcherHintOptions = {
const matcherOptions = {
isNot: this.isNot,
promise: this.promise,
};
Expand All @@ -50,7 +46,7 @@ export async function toBeTruthy<T>(
}, options.timeout, testInfo._testFinished);

const message = () => {
return matcherHint(matcherName, undefined, '', matcherOptions);
return this.utils.matcherHint(matcherName, undefined, '', matcherOptions);
};

return { message, pass };
Expand Down
24 changes: 8 additions & 16 deletions src/test/matchers/toEqual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,9 @@
* limitations under the License.
*/

import { equals } from 'expect/build/jasmineUtils';
import {
iterableEquality
} from 'expect/build/utils';
import {
matcherHint, MatcherHintOptions,
printDiffOrStringify,
printExpected,
printReceived,
stringify
} from 'jest-matcher-utils';
import { currentTestInfo } from '../globals';
import type { Expect } from '../types';
import { expectType, pollUntilDeadline } from '../util';
Expand Down Expand Up @@ -57,7 +49,7 @@ export async function toEqual<T>(
throw new Error(`${matcherName} must be called during the test`);
expectType(receiver, receiverType, matcherName);

const matcherOptions: MatcherHintOptions = {
const matcherOptions = {
comment: 'deep equality',
isNot: this.isNot,
promise: this.promise,
Expand All @@ -68,22 +60,22 @@ export async function toEqual<T>(

await pollUntilDeadline(testInfo, async remainingTime => {
received = await query(remainingTime);
pass = equals(received, expected, [iterableEquality, regExpTester]);
pass = this.equals(received, expected, [iterableEquality, regExpTester]);
return pass === !matcherOptions.isNot;
}, options.timeout, testInfo._testFinished);

const message = pass
? () =>
matcherHint(matcherName, undefined, undefined, matcherOptions) +
this.utils.matcherHint(matcherName, undefined, undefined, matcherOptions) +
'\n\n' +
`Expected: not ${printExpected(expected)}\n` +
(stringify(expected) !== stringify(received)
? `Received: ${printReceived(received)}`
`Expected: not ${this.utils.printExpected(expected)}\n` +
(this.utils.stringify(expected) !== this.utils.stringify(received)
? `Received: ${this.utils.printReceived(received)}`
: '')
: () =>
matcherHint(matcherName, undefined, undefined, matcherOptions) +
this.utils.matcherHint(matcherName, undefined, undefined, matcherOptions) +
'\n\n' +
printDiffOrStringify(
this.utils.printDiffOrStringify(
expected,
received,
EXPECTED_LABEL,
Expand Down
7 changes: 6 additions & 1 deletion src/test/matchers/toMatchSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
import type { Expect } from '../types';
import { currentTestInfo } from '../globals';
import { compare } from './golden';
import { SyncExpectationResult } from 'expect/build/types';

// from expect/build/types
type SyncExpectationResult = {
pass: boolean;
message: () => string;
};

export function toMatchSnapshot(this: ReturnType<Expect['getState']>, received: Buffer | string, nameOrOptions: string | { name: string, threshold?: number }, optOptions: { threshold?: number } = {}): SyncExpectationResult {
let options: { name: string, threshold?: number };
Expand Down
30 changes: 11 additions & 19 deletions src/test/matchers/toMatchText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ import {
printReceivedStringContainExpectedSubstring
} from 'expect/build/print';

import {
EXPECTED_COLOR,
matcherErrorMessage,
matcherHint, MatcherHintOptions,
printExpected,
printWithType,
printDiffOrStringify,
} from 'jest-matcher-utils';
import { currentTestInfo } from '../globals';
import type { Expect } from '../types';
import { expectType, pollUntilDeadline } from '../util';
Expand All @@ -45,7 +37,7 @@ export async function toMatchText(
throw new Error(`${matcherName} must be called during the test`);
expectType(receiver, receiverType, matcherName);

const matcherOptions: MatcherHintOptions = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we copy this type definition for our own safety?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It gets checked down below when we pass it in as an argument.

const matcherOptions = {
isNot: this.isNot,
promise: this.promise,
};
Expand All @@ -55,12 +47,12 @@ export async function toMatchText(
!(expected && typeof expected.test === 'function')
) {
throw new Error(
matcherErrorMessage(
matcherHint(matcherName, undefined, undefined, matcherOptions),
`${EXPECTED_COLOR(
this.utils.matcherErrorMessage(
this.utils.matcherHint(matcherName, undefined, undefined, matcherOptions),
`${this.utils.EXPECTED_COLOR(
'expected',
)} value must be a string or regular expression`,
printWithType('Expected', expected, printExpected),
this.utils.printWithType('Expected', expected, this.utils.printExpected),
),
);
}
Expand All @@ -84,17 +76,17 @@ export async function toMatchText(
const message = pass
? () =>
typeof expected === 'string'
? matcherHint(matcherName, undefined, undefined, matcherOptions) +
? this.utils.matcherHint(matcherName, undefined, undefined, matcherOptions) +
'\n\n' +
`Expected ${stringSubstring}: not ${printExpected(expected)}\n` +
`Expected ${stringSubstring}: not ${this.utils.printExpected(expected)}\n` +
`Received string: ${printReceivedStringContainExpectedSubstring(
received,
received.indexOf(expected),
expected.length,
)}`
: matcherHint(matcherName, undefined, undefined, matcherOptions) +
: this.utils.matcherHint(matcherName, undefined, undefined, matcherOptions) +
'\n\n' +
`Expected pattern: not ${printExpected(expected)}\n` +
`Expected pattern: not ${this.utils.printExpected(expected)}\n` +
`Received string: ${printReceivedStringContainExpectedResult(
received,
typeof expected.exec === 'function'
Expand All @@ -107,9 +99,9 @@ export async function toMatchText(
const labelReceived = 'Received string';

return (
matcherHint(matcherName, undefined, undefined, matcherOptions) +
this.utils.matcherHint(matcherName, undefined, undefined, matcherOptions) +
'\n\n' +
printDiffOrStringify(
this.utils.printDiffOrStringify(
expected,
received,
labelExpected,
Expand Down
3 changes: 1 addition & 2 deletions types/testExpect.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import type * as expect from 'expect';
import type { ExpectedAssertionsErrors } from 'expect/build/types';

export declare type AsymmetricMatcher = Record<string, any>;

Expand All @@ -17,7 +16,7 @@ export declare type Expect = {
// Sourced from node_modules/expect/build/types.d.ts
assertions(arg0: number): void;
extend(arg0: any): void;
extractExpectedAssertionsErrors: () => ExpectedAssertionsErrors;
extractExpectedAssertionsErrors: typeof expect['extractExpectedAssertionsErrors'];
getState(): expect.MatcherState;
hasAssertions(): void;
setState(state: Partial<expect.MatcherState>): void;
Expand Down