address comments

This commit is contained in:
Yury Semikhatsky 2024-05-23 09:46:08 -07:00
parent 2d5c7c6707
commit 62054a3b36
6 changed files with 32 additions and 18 deletions

View file

@ -15,9 +15,7 @@
*/
export const expect: typeof import('../../bundles/expect/node_modules/expect/build').expect = require('./expectBundleImpl').expect;
export type ExpectMatcherContext = import('../../bundles/expect/node_modules/expect/build').MatcherContext;
export const EXPECTED_COLOR: typeof import('../../bundles/expect/node_modules/jest-matcher-utils/build').EXPECTED_COLOR = require('./expectBundleImpl').EXPECTED_COLOR;
export const INVERTED_COLOR: typeof import('../../bundles/expect/node_modules/jest-matcher-utils/build').INVERTED_COLOR = require('./expectBundleImpl').INVERTED_COLOR;
export const RECEIVED_COLOR: typeof import('../../bundles/expect/node_modules/jest-matcher-utils/build').RECEIVED_COLOR = require('./expectBundleImpl').RECEIVED_COLOR;
export const matcherErrorMessage: typeof import('../../bundles/expect/node_modules/jest-matcher-utils/build').matcherErrorMessage = require('./expectBundleImpl').matcherErrorMessage;
export const printReceived: typeof import('../../bundles/expect/node_modules/jest-matcher-utils/build').printReceived = require('./expectBundleImpl').printReceived;

View file

@ -58,7 +58,6 @@ import {
RECEIVED_COLOR,
printReceived,
} from '../common/expectBundle';
export type { ExpectMatcherContext } from '../common/expectBundle';
import { zones } from 'playwright-core/lib/utils';
import { TestInfoImpl } from '../worker/testInfo';
import { ExpectError } from './matcherHint';
@ -132,12 +131,11 @@ function createExpect(info: ExpectMetaInfo) {
const wrappedMatchers: any = {};
Object.entries(matchers).forEach(([name, matcher]) => {
wrappedMatchers[name] = function(...args: any[]) {
const { isNot, promise, utils, expand } = this;
const { isNot, promise, utils } = this;
const newThis: ExpectMatcherState = {
isNot,
promise,
utils,
expand,
timeout: currentExpectTimeout({})
};
return (matcher as any).call(newThis, ...args);

View file

@ -22,11 +22,12 @@ import {
printReceivedStringContainExpectedResult,
printReceivedStringContainExpectedSubstring
} from './expect';
import { EXPECTED_COLOR, matcherErrorMessage } from '../common/expectBundle';
import { EXPECTED_COLOR } from '../common/expectBundle';
import type { ExpectMatcherState } from '../../types/test';
import { kNoElementsFoundError, matcherHint } from './matcherHint';
import type { MatcherResult } from './matcherHint';
import type { Locator } from 'playwright-core';
import { colors } from 'playwright-core/lib/utilsBundle';
export async function toMatchText(
this: ExpectMatcherState,
@ -48,15 +49,12 @@ export async function toMatchText(
!(typeof expected === 'string') &&
!(expected && typeof expected.test === 'function')
) {
throw new Error(
matcherErrorMessage(
matcherHint(this, receiver, matcherName, receiver, expected, matcherOptions),
`${EXPECTED_COLOR(
'expected',
)} value must be a string or regular expression`,
this.utils.printWithType('Expected', expected, this.utils.printExpected),
),
);
// Same format as jest's matcherErrorMessage
throw new Error([
matcherHint(this, receiver, matcherName, receiver, expected, matcherOptions),
`${colors.bold('Matcher error')}: ${EXPECTED_COLOR('expected',)} value must be a string or regular expression`,
this.utils.printWithType('Expected', expected, this.utils.printExpected)
].join('\n\n'));
}
const timeout = options.timeout ?? this.timeout;

View file

@ -6510,10 +6510,20 @@ export interface ExpectMatcherUtils {
}
export type ExpectMatcherState = {
/**
* Wehther this matcher was called with the negated .not modifier.
*/
isNot: boolean;
/**
* - 'rejects' if matcher was called with the promise .rejects modifier
* - 'resolves' if matcher was called with the promise .resolves modifier
* - '' if matcher was not called with a promise modifier
*/
promise: 'rejects' | 'resolves' | '';
utils: ExpectMatcherUtils;
expand: boolean;
/**
* Timeout in milliseconds for the assertion to be fulfilled.
*/
timeout: number;
};

View file

@ -1012,7 +1012,7 @@ test('should expose timeout to custom matchers', async ({ runInlineTest, runTSC
import type { ExpectMatcherState, MatcherReturnType } from '@playwright/test';
import { test, expect as base } from '@playwright/test';
const expect = base.extend<{assertTimeout: (this: ExpectMatcherState, page: any, value: number) => MatcherReturnType}>({
const expect = base.extend({
assertTimeout(page: any, value: number) {
const pass = this.timeout === value;
return {

View file

@ -358,10 +358,20 @@ export interface ExpectMatcherUtils {
}
export type ExpectMatcherState = {
/**
* Wehther this matcher was called with the negated .not modifier.
*/
isNot: boolean;
/**
* - 'rejects' if matcher was called with the promise .rejects modifier
* - 'resolves' if matcher was called with the promise .resolves modifier
* - '' if matcher was not called with a promise modifier
*/
promise: 'rejects' | 'resolves' | '';
utils: ExpectMatcherUtils;
expand: boolean;
/**
* Timeout in milliseconds for the assertion to be fulfilled.
*/
timeout: number;
};