Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore(test-runner): vendor the expect library (#40301)

Dmitry Gozman396a39b1471c
diff --git a/package-lock.json b/package-lock.json
index b1ce8d7e90..2ca6b75f7b 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -3963,22 +3963,6 @@
"url": "https://github.com/sponsors/colinhacks"
}
},
- "node_modules/ci-info": {
- "version": "4.4.0",
- "resolved": "https://registry.npmjs.org/ci-info/-/ci-info-4.4.0.tgz",
- "integrity": "sha512-77PSwercCZU2Fc4sX94eF8k8Pxte6JAwL4/ICZLFjJLqegs7kCuAsqqj/70NQF6TvDpgFjkubQB2FW2ZZddvQg==",
- "dev": true,
- "funding": [
- {
- "type": "github",
- "url": "https://github.com/sponsors/sibiraj-s"
- }
- ],
- "license": "MIT",
- "engines": {
- "node": ">=8"
- }
- },
"node_modules/cliui": {
"version": "7.0.4",
"resolved": "https://registry.npmjs.org/cliui/-/cliui-7.0.4.tgz",
@@ -5161,24 +5145,6 @@
"node": ">=18.0.0"
}
},
- "node_modules/expect": {
- "version": "30.2.0",
- "resolved": "https://registry.npmjs.org/expect/-/expect-30.2.0.tgz",
- "integrity": "sha512-u/feCi0GPsI+988gU2FLcsHyAHTU0MX1Wg68NhAnN7z/+C5wqG+CY8J53N9ioe8RXgaoz0nBR/TYMf3AycUuPw==",
- "dev": true,
- "license": "MIT",
- "dependencies": {
- "@jest/expect-utils": "30.2.0",
- "@jest/get-type": "30.1.0",
- "jest-matcher-utils": "30.2.0",
- "jest-message-util": "30.2.0",
- "jest-mock": "30.2.0",
- "jest-util": "30.2.0"
- },
- "engines": {
- "node": "^18.14.0 || ^20.0.0 || ^22.0.0 || >=24.0.0"
- }
- },
"node_modules/express": {
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/express/-/express-5.2.1.tgz",
@@ -6591,21 +6557,6 @@
"dev": true,
"license": "ISC"
},
- "node_modules/jest-mock": {
- "version": "30.2.0",
- "resolved": "https://registry.npmjs.org/jest-mock/-/jest-mock-30.2.0.tgz",
- "integrity": "sha512-JNNNl2rj4b5ICpmAcq+WbLH83XswjPbjH4T7yvGzfAGCPh1rw+xVNbtk+FnRslvt9lkCcdn9i1oAoKUuFsOxRw==",
- "dev": true,
- "license": "MIT",
- "dependencies": {
- "@jest/types": "30.2.0",
- "@types/node": "*",
- "jest-util": "30.2.0"
- },
- "engines": {
- "node": "^18.14.0 || ^20.0.0 || ^22.0.0 || >=24.0.0"
- }
- },
"node_modules/jest-regex-util": {
"version": "30.0.1",
"resolved": "https://registry.npmjs.org/jest-regex-util/-/jest-regex-util-30.0.1.tgz",
@@ -6616,31 +6567,6 @@
"node": "^18.14.0 || ^20.0.0 || ^22.0.0 || >=24.0.0"
}
},
- "node_modules/jest-util": {
- "version": "30.2.0",
- "resolved": "https://registry.npmjs.org/jest-util/-/jest-util-30.2.0.tgz",
- "integrity": "sha512-QKNsM0o3Xe6ISQU869e+DhG+4CK/48aHYdJZGlFQVTjnbvgpcKyxpzk29fGiO7i/J8VENZ+d2iGnSsvmuHywlA==",
- "dev": true,
- "license": "MIT",
- "dependencies": {
- "@jest/types": "30.2.0",
- "@types/node": "*",
- "chalk": "^4.1.2",
- "ci-info": "^4.2.0",
- "graceful-fs": "^4.2.11",
- "picomatch": "^4.0.2"
- },
- "engines": {
- "node": "^18.14.0 || ^20.0.0 || ^22.0.0 || >=24.0.0"
- }
- },
- "node_modules/jest-util/node_modules/graceful-fs": {
- "version": "4.2.11",
- "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz",
- "integrity": "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==",
- "dev": true,
- "license": "ISC"
- },
"node_modules/jose": {
"version": "6.1.3",
"resolved": "https://registry.npmjs.org/jose/-/jose-6.1.3.tgz",
@@ -9726,12 +9652,13 @@
"@babel/plugin-transform-private-property-in-object": "^7.27.1",
"@babel/plugin-transform-react-jsx": "^7.27.1",
"@babel/preset-typescript": "^7.27.1",
+ "@jest/expect-utils": "30.2.0",
"@types/babel__code-frame": "^7.0.6",
"@types/babel__core": "^7.20.5",
"@types/babel__helper-plugin-utils": "^7.10.3",
"@types/babel__traverse": "^7.20.6",
- "expect": "30.2.0",
- "jest-matcher-utils": "30.2.0"
+ "jest-matcher-utils": "30.2.0",
+ "jest-message-util": "30.2.0"
},
"engines": {
"node": ">=18"
diff --git a/packages/playwright/package.json b/packages/playwright/package.json
index 64c80e7ca4..1c4d762fe9 100644
--- a/packages/playwright/package.json
+++ b/packages/playwright/package.json
@@ -83,8 +83,9 @@
"@types/babel__core": "^7.20.5",
"@types/babel__helper-plugin-utils": "^7.10.3",
"@types/babel__traverse": "^7.20.6",
- "expect": "30.2.0",
- "jest-matcher-utils": "30.2.0"
+ "@jest/expect-utils": "30.2.0",
+ "jest-matcher-utils": "30.2.0",
+ "jest-message-util": "30.2.0"
},
"optionalDependencies": {
"fsevents": "2.3.2"
diff --git a/packages/playwright/src/matchers/DEPS.list b/packages/playwright/src/matchers/DEPS.list
index 8a96baf06c..823dbf002f 100644
--- a/packages/playwright/src/matchers/DEPS.list
+++ b/packages/playwright/src/matchers/DEPS.list
@@ -2,4 +2,8 @@
@isomorphic/**
@utils/**
node_modules/colors/safe
-node_modules/expect
+
+[expectLibrary.ts]
+node_modules/@jest/expect-utils
+node_modules/jest-matcher-utils
+node_modules/jest-message-util
diff --git a/packages/playwright/src/matchers/expect.ts b/packages/playwright/src/matchers/expect.ts
index 32e27cdc56..2c754b94b2 100644
--- a/packages/playwright/src/matchers/expect.ts
+++ b/packages/playwright/src/matchers/expect.ts
@@ -16,15 +16,38 @@
import path from 'path';
-import {
- expect as expectLibrary,
-} from 'expect';
import { parseStackFrame, captureRawStack } from '@isomorphic/stackTrace';
import { escapeWithQuotes, isString } from '@isomorphic/stringUtils';
import { pollAgainstDeadline } from '@isomorphic/timeoutRunner';
import { currentZone } from '@utils/zones';
-import { ExpectError, isJestError } from './matcherHint';
+import {
+ any,
+ anything,
+ arrayContaining,
+ arrayNotContaining,
+ arrayOf,
+ buildCustomAsymmetricMatcher,
+ closeTo,
+ createThrowMatcher,
+ getMessage,
+ validateMatcherResult,
+ isPromise,
+ matchers as expectMatchers,
+ notArrayOf,
+ notCloseTo,
+ objectContaining,
+ objectNotContaining,
+ stringContaining,
+ stringMatching,
+ stringNotContaining,
+ stringNotMatching,
+ utils,
+ createExpectedPromiseMessage,
+ createExpectedToResolveMessage,
+ createExpectedToRejectMessage,
+} from './expectLibrary';
+import { ExpectError } from './matcherHint';
import {
computeMatcherTitleSuffix,
deadlineForMatcher,
@@ -61,17 +84,20 @@ import {
import { toMatchAriaSnapshot } from './toMatchAriaSnapshot';
import { toHaveScreenshot, toMatchSnapshot } from './toMatchSnapshot';
+import type { MatcherContext, MatchersObject, RawMatcherFn } from './expectLibrary';
+import type { MatcherAttachment, MatcherResult } from './matcherHint';
import type { ExpectMatcherStateInternal } from './matchers';
import type { Expect } from '../../types/test';
import type { StackFrame } from '@protocol/channels';
-export interface ExpectStepInfo {
- _attachToStep(attachment: { name: string; contentType: string; path?: string; body?: string | Buffer }): void;
-}
-
-export interface ExpectStep {
- complete(result: { error?: Error | unknown, suggestedRebaseline?: string }): void;
- info: ExpectStepInfo;
+interface ExpectStep {
+ complete(result: {
+ error?: Error | unknown,
+ softError?: Error | unknown,
+ shouldNotRetryTest?: boolean,
+ suggestedRebaseline?: string,
+ attachments?: MatcherAttachment[],
+ }): void;
}
export interface ExpectTestInfo {
@@ -81,10 +107,8 @@ export interface ExpectTestInfo {
title: string;
shortTitle: string;
params?: Record<string, any>;
- infectParentStepsWithError?: boolean;
}): ExpectStep;
_deadline(): { deadline: number; timeout: number };
- _failWithError(error: Error | unknown, shouldNotRetry?: 'shouldNotRetry'): void;
_resolveSnapshotPaths(kind: 'snapshot' | 'screenshot' | 'aria', name: string | string[] | undefined, updateSnapshotIndex: 'updateSnapshotIndex' | 'dontUpdateSnapshotIndex', anonymousExtension?: string): { absoluteSnapshotPath: string; relativeOutputPath: string };
_getOutputPath(...pathSegments: string[]): string;
}
@@ -134,141 +158,26 @@ export function expectConfig(): ExpectConfig {
type ExpectMessage = string | { message?: string };
-function createMatchers(actual: unknown, info: ExpectMetaInfo, prefix: string[]): any {
- return new Proxy(expectLibrary(actual), new ExpectMetaInfoProxyHandler(actual, info, prefix));
-}
-
-const userMatchersSymbol = Symbol('userMatchers');
-
-function qualifiedMatcherName(qualifier: string[], matcherName: string) {
- return qualifier.join(':') + '$' + matcherName;
-}
-
-let lastExtendId = 0;
-
-function createExpect(info: ExpectMetaInfo, prefix: string[], userMatchers: Record<string, Function>) {
- const expectInstance: Expect<{}> = new Proxy(expectLibrary, {
- apply: function(target: any, thisArg: any, argumentsList: [unknown, ExpectMessage?]) {
- const [actual, messageOrOptions] = argumentsList;
- const message = isString(messageOrOptions) ? messageOrOptions : messageOrOptions?.message || info.message;
- const newInfo = { ...info, message };
- if (newInfo.poll) {
- if (typeof actual !== 'function')
- throw new Error('`expect.poll()` accepts only function as a first argument');
- newInfo.poll.generator = actual as any;
- }
- return createMatchers(actual, newInfo, prefix);
- },
-
- get: function(target: any, property: string | typeof userMatchersSymbol) {
- if (property === 'configure')
- return configure;
-
- if (property === 'extend') {
- return (matchers: any) => {
- const qualifier = [...prefix, String(++lastExtendId)];
-
- const wrappedMatchers: any = {};
- for (const [name, matcher] of Object.entries(matchers)) {
- wrappedMatchers[name] = wrapPlaywrightMatcherToPassNiceThis(matcher);
- const key = qualifiedMatcherName(qualifier, name);
- wrappedMatchers[key] = wrappedMatchers[name];
- Object.defineProperty(wrappedMatchers[key], 'name', { value: name });
- }
- expectLibrary.extend(wrappedMatchers);
- return createExpect(info, qualifier, { ...userMatchers, ...matchers });
- };
- }
-
- if (property === 'soft') {
- return (actual: unknown, messageOrOptions?: ExpectMessage) => {
- return configure({ soft: true })(actual, messageOrOptions) as any;
- };
- }
-
- if (property === userMatchersSymbol)
- return userMatchers;
-
- if (property === 'poll') {
- return (actual: unknown, messageOrOptions?: ExpectMessage & { timeout?: number, intervals?: number[] }) => {
- const poll = isString(messageOrOptions) ? {} : messageOrOptions || {};
- return configure({ _poll: poll })(actual, messageOrOptions) as any;
- };
- }
- return (expectLibrary as any)[property];
- },
- });
-
- const configure = (configuration: { message?: string, timeout?: number, soft?: boolean, _poll?: boolean | { timeout?: number, intervals?: number[] } }) => {
- const newInfo = { ...info };
- if ('message' in configuration)
- newInfo.message = configuration.message;
- if ('timeout' in configuration)
- newInfo.timeout = configuration.timeout;
- if ('soft' in configuration)
- newInfo.isSoft = configuration.soft;
- if ('_poll' in configuration) {
- newInfo.poll = configuration._poll ? { ...info.poll, generator: () => {} } : undefined;
- if (typeof configuration._poll === 'object') {
- newInfo.poll!.timeout = configuration._poll.timeout ?? newInfo.poll!.timeout;
- newInfo.poll!.intervals = configuration._poll.intervals ?? newInfo.poll!.intervals;
- }
- }
- return createExpect(newInfo, prefix, userMatchers);
+type ExpectMetaInfo = {
+ message?: string;
+ isNot?: boolean;
+ isSoft?: boolean;
+ poll?: {
+ timeout?: number;
+ intervals?: number[];
};
-
- return expectInstance;
-}
-
-// Expect wraps matchers, so there is no way to pass this information to the raw Playwright matcher.
-// Rely on sync call sequence to seed each matcher call with the context.
-type MatcherCallContext = {
- expectInfo: ExpectMetaInfo;
- testInfo: ExpectTestInfo | null;
- step?: ExpectStepInfo;
+ timeout?: number;
+ userMatchers: MatchersObject;
};
-let matcherCallContext: MatcherCallContext | undefined;
-
-function setMatcherCallContext(context: MatcherCallContext) {
- matcherCallContext = context;
-}
-
-function takeMatcherCallContext(): MatcherCallContext | undefined {
- try {
- return matcherCallContext;
- } finally {
- // Any subsequent matcher following the first is assumed to be an unsupported legacy asymmetric matcher.
- // Lacking call context in these scenarios is not particularly important.
- matcherCallContext = undefined;
- }
-}
+const META_INFO = Symbol('expectMetaInfo');
const defaultExpectTimeout = 5000;
-function wrapPlaywrightMatcherToPassNiceThis(matcher: any) {
- return function(this: any, ...args: any[]) {
- const { isNot, promise, utils } = this;
- const context = takeMatcherCallContext();
- const timeout = context?.expectInfo.timeout ?? expectConfig().timeout ?? defaultExpectTimeout;
- const newThis: ExpectMatcherStateInternal = {
- isNot,
- promise,
- utils,
- timeout,
- _stepInfo: context?.step,
- };
- (newThis as any).equals = throwUnsupportedExpectMatcherError;
- return matcher.call(newThis, ...args);
- };
-}
-
function throwUnsupportedExpectMatcherError() {
throw new Error('It looks like you are using custom expect matchers that are not compatible with Playwright. See https://aka.ms/playwright/expect-compatibility');
}
-expectLibrary.setState({ expand: false });
-
const customAsyncMatchers = {
toBeAttached,
toBeChecked,
@@ -303,160 +212,238 @@ const customAsyncMatchers = {
toPass,
};
-const customMatchers = {
+const allBuiltinMatchers: MatchersObject = {
+ ...expectMatchers,
+ toThrow: createThrowMatcher('toThrow'),
+ toThrowError: createThrowMatcher('toThrowError'),
...customAsyncMatchers,
toMatchSnapshot,
+} as any;
+
+const promiseThrowMatchers: MatchersObject = {
+ toThrow: createThrowMatcher('toThrow', true),
+ toThrowError: createThrowMatcher('toThrowError', true),
};
-type Generator = () => any;
+function createExpect(info: ExpectMetaInfo): Expect<{}> {
+ const expectFn: any = (actual: unknown, messageOrOptions?: ExpectMessage) => createMatchers(actual, info, messageOrOptions);
+ Object.defineProperty(expectFn, META_INFO, { value: info });
+
+ expectFn.any = any;
+ expectFn.anything = anything;
+ expectFn.arrayContaining = arrayContaining;
+ expectFn.arrayOf = arrayOf;
+ expectFn.closeTo = closeTo;
+ expectFn.objectContaining = objectContaining;
+ expectFn.stringContaining = stringContaining;
+ expectFn.stringMatching = stringMatching;
+
+ const notAsymmetric: any = {
+ arrayContaining: arrayNotContaining,
+ arrayOf: notArrayOf,
+ closeTo: notCloseTo,
+ objectContaining: objectNotContaining,
+ stringContaining: stringNotContaining,
+ stringMatching: stringNotMatching,
+ };
+ expectFn.not = notAsymmetric;
-type ExpectMetaInfo = {
- message?: string;
- isNot?: boolean;
- isSoft?: boolean;
- poll?: {
- timeout?: number;
- intervals?: number[];
- generator: Generator;
+ for (const [name, matcher] of Object.entries(info.userMatchers)) {
+ const { positive, inverse } = buildCustomAsymmetricMatcher(name, matcher);
+ expectFn[name] = positive;
+ notAsymmetric[name] = inverse;
+ }
+
+ expectFn.getState = () => ({});
+
+ expectFn.configure = (configuration: { message?: string, timeout?: number, soft?: boolean }) => {
+ const newInfo: ExpectMetaInfo = { ...info };
+ if ('message' in configuration)
+ newInfo.message = configuration.message;
+ if ('timeout' in configuration)
+ newInfo.timeout = configuration.timeout;
+ if ('soft' in configuration)
+ newInfo.isSoft = configuration.soft;
+ return createExpect(newInfo);
};
- timeout?: number;
-};
-class ExpectMetaInfoProxyHandler implements ProxyHandler<any> {
- private _actual: any;
- private _info: ExpectMetaInfo;
- private _prefix: string[];
+ expectFn.soft = (actual: unknown, messageOrOptions?: ExpectMessage) => {
+ return createMatchers(actual, { ... info, isSoft: true }, messageOrOptions);
+ };
- constructor(actual: any, info: ExpectMetaInfo, prefix: string[]) {
- this._actual = actual;
- this._info = { ...info };
- this._prefix = prefix;
- }
+ expectFn.poll = (actual: unknown, messageOrOptions?: ExpectMessage & { timeout?: number, intervals?: number[] }) => {
+ const poll = isString(messageOrOptions) ? {} : messageOrOptions || {};
+ return createMatchers(actual, { ...info, poll: { timeout: poll.timeout, intervals: poll.intervals } }, messageOrOptions);
+ };
- get(target: Object, matcherName: string | symbol, receiver: any): any {
- if (matcherName === 'toThrowError')
- matcherName = 'toThrow';
- let matcher = Reflect.get(target, matcherName, receiver);
- if (typeof matcherName !== 'string')
- return matcher;
-
- let resolvedMatcherName = matcherName;
- for (let i = this._prefix.length; i > 0; i--) {
- const qualifiedName = qualifiedMatcherName(this._prefix.slice(0, i), matcherName);
- if (Reflect.has(target, qualifiedName)) {
- matcher = Reflect.get(target, qualifiedName, receiver);
- resolvedMatcherName = qualifiedName;
- break;
- }
+ expectFn.extend = (matchers: MatchersObject) => {
+ for (const [name, m] of Object.entries(matchers)) {
+ if (typeof m !== 'function')
+ throw new TypeError(`expect.extend: \`${name}\` is not a valid matcher. Must be a function, is "${typeof m}"`);
}
- if (matcher === undefined)
- throw new Error(`expect: Property '${matcherName}' not found.`);
- if (typeof matcher !== 'function') {
- if (matcherName === 'not')
- this._info.isNot = !this._info.isNot;
- return new Proxy(matcher, this);
+ // Legacy behavior: `expect.extend({...})` without capturing the return value
+ // must make the new matchers available on the same expect instance.
+ Object.assign(info.userMatchers, matchers);
+ for (const [name, matcher] of Object.entries(matchers)) {
+ const { positive, inverse } = buildCustomAsymmetricMatcher(name, matcher);
+ expectFn[name] = positive;
+ notAsymmetric[name] = inverse;
}
- if (this._info.poll) {
- if ((customAsyncMatchers as any)[matcherName] || matcherName === 'resolves' || matcherName === 'rejects')
- throw new Error(`\`expect.poll()\` does not support "${matcherName}" matcher.`);
- matcher = (...args: any[]) => pollMatcher(resolvedMatcherName, this._info, this._prefix, ...args);
+ // End of legacy behavior.
+
+ return createExpect({
+ ...info,
+ userMatchers: { ...info.userMatchers, ...matchers },
+ });
+ };
+
+ return expectFn as Expect<{}>;
+}
+
+function createMatchers(actual: unknown, originalInfo: ExpectMetaInfo, messageOrOptions?: ExpectMessage): any {
+ const message = isString(messageOrOptions) ? messageOrOptions : messageOrOptions?.message || originalInfo.message;
+ const info = { ...originalInfo, message };
+ const result: any = { not: {}, resolves: { not: {} }, rejects: { not: {} } };
+ const notInfo: ExpectMetaInfo = { ...info, isNot: !info.isNot };
+ for (const [name, matcher] of Object.entries({ ...allBuiltinMatchers, ...info.userMatchers })) {
+ result[name] = createMatcher(name, info, actual, matcher);
+ result.not[name] = createMatcher(name, notInfo, actual, matcher);
+ const promiseMatcher = promiseThrowMatchers[name] ?? matcher;
+ result.resolves[name] = createMatcher(name, info, actual, promiseMatcher, 'resolves');
+ result.resolves.not[name] = createMatcher(name, notInfo, actual, promiseMatcher, 'resolves');
+ result.rejects[name] = createMatcher(name, info, actual, promiseMatcher, 'rejects');
+ result.rejects.not[name] = createMatcher(name, notInfo, actual, promiseMatcher, 'rejects');
+ }
+ return result;
+}
+
+function createMatcher(matcherName: string, info: ExpectMetaInfo, actual: unknown, matcher: RawMatcherFn, promise?: 'resolves' | 'rejects') {
+ return (...args: any[]) => callMatcherAsStep(matcherName, info, actual, matcher, args, promise);
+}
+
+function callMatcherAsStep(matcherName: string, info: ExpectMetaInfo, actual: unknown, matcher: RawMatcherFn, args: any[], promise?: 'resolves' | 'rejects') {
+ const testInfo = expectConfig().testInfo;
+ const customMessage = info.message || '';
+ const suffixes = computeMatcherTitleSuffix(matcherName, actual, args);
+ const defaultTitle = `${info.poll ? 'poll ' : ''}${info.isSoft ? 'soft ' : ''}${info.isNot ? 'not ' : ''}${matcherName}${suffixes.short || ''}`;
+ const shortTitle = customMessage || `Expect ${escapeWithQuotes(defaultTitle, '"')}`;
+ const longTitle = shortTitle + (suffixes.long || '');
+ const apiName = `expect${info.poll ? '.poll ' : ''}${info.isSoft ? '.soft ' : ''}${info.isNot ? '.not' : ''}.${matcherName}${suffixes.short || ''}`;
+
+ // This looks like it is unnecessary, but it isn't - we need to filter
+ // out all the frames that belong to the test runner from caught runtime errors.
+ const stackFrames = expectConfig().filteredStackTrace(captureRawStack());
+ const stepData = {
+ category: 'expect' as const,
+ apiName,
+ title: longTitle,
+ shortTitle,
+ params: args[0] ? { expected: args[0] } : undefined,
+ };
+ const step = testInfo?._addStep(stepData);
+
+ const handleError = (error: Error, result?: MatcherResult) => {
+ if (info.isSoft && step) {
+ step.complete({ ...result, error, softError: error });
+ } else {
+ step?.complete({ ...result, error });
+ throw error;
}
- return (...args: any[]) => {
- const testInfo = expectConfig().testInfo;
- setMatcherCallContext({ expectInfo: this._info, testInfo });
- if (!testInfo)
- return matcher.call(target, ...args);
-
- const customMessage = this._info.message || '';
- const suffixes = computeMatcherTitleSuffix(matcherName, this._actual, args);
- const defaultTitle = `${this._info.poll ? 'poll ' : ''}${this._info.isSoft ? 'soft ' : ''}${this._info.isNot ? 'not ' : ''}${matcherName}${suffixes.short || ''}`;
- const shortTitle = customMessage || `Expect ${escapeWithQuotes(defaultTitle, '"')}`;
- const longTitle = shortTitle + (suffixes.long || '');
- const apiName = `expect${this._info.poll ? '.poll ' : ''}${this._info.isSoft ? '.soft ' : ''}${this._info.isNot ? '.not' : ''}.${matcherName}${suffixes.short || ''}`;
-
- // This looks like it is unnecessary, but it isn't - we need to filter
- // out all the frames that belong to the test runner from caught runtime errors.
- const stackFrames = expectConfig().filteredStackTrace(captureRawStack());
-
- // toPass and poll matchers can contain other steps, expects and API calls,
- // so they behave like a retriable step.
- const stepInfo = {
- category: 'expect' as const,
- apiName,
- title: longTitle,
- shortTitle,
- params: args[0] ? { expected: args[0] } : undefined,
- infectParentStepsWithError: this._info.isSoft,
- };
-
- const step = testInfo._addStep(stepInfo);
-
- const reportStepError = (e: Error | unknown) => {
- const jestError = isJestError(e) ? e : null;
- const expectError = jestError ? new ExpectError(jestError, customMessage, stackFrames) : undefined;
- if (jestError?.matcherResult.suggestedRebaseline) {
- // NOTE: this is a workaround for the fact that we can't pass the suggested rebaseline
- // for passing matchers. See toMatchAriaSnapshot for a counterpart.
- step.complete({ suggestedRebaseline: jestError?.matcherResult.suggestedRebaseline });
- return;
- }
-
- const error = expectError ?? e;
- step.complete({ error });
-
- if (this._info.isSoft)
- testInfo._failWithError(error);
- else
- throw error;
- };
-
- const finalizer = () => {
- step.complete({});
- };
-
- try {
- setMatcherCallContext({ expectInfo: this._info, testInfo, step: step.info });
- const callback = () => matcher.call(target, ...args);
- const result = currentZone().with('stepZone', step).run(callback);
- if (result instanceof Promise)
- return result.then(finalizer).catch(reportStepError);
- finalizer();
- return result;
- } catch (e) {
- void reportStepError(e);
- }
- };
+ };
+
+ const finalizer = (result: MatcherResult) => {
+ validateMatcherResult(result);
+ if (result.pass === !!info.isNot) {
+ const error = new ExpectError({ ...result, name: matcherName, message: getMessage(result.message) }, customMessage, stackFrames);
+ handleError(error, result);
+ } else {
+ step?.complete(result);
+ }
+ };
+
+ try {
+ const invoke = () => info.poll
+ ? invokePollMatcher(matcherName, info, matcher, actual, args, promise)
+ : invokeMatcher(matcherName, info, matcher, actual, args, promise);
+ const result = step ? currentZone().with('stepZone', step).run(invoke) : invoke();
+ if (result instanceof Promise)
+ return result.then(finalizer, handleError);
+ finalizer(result);
+ } catch (error) {
+ handleError(error);
+ }
+}
+
+function invokeMatcher(
+ matcherName: string,
+ info: ExpectMetaInfo,
+ matcher: RawMatcherFn,
+ actual: unknown,
+ args: any[],
+ promise: 'resolves' | 'rejects' | undefined,
+): MatcherResult | Promise<MatcherResult> {
+ const isNot = !!info.isNot;
+ const timeout = info.timeout ?? expectConfig().timeout ?? defaultExpectTimeout;
+ const matcherContext: MatcherContext & ExpectMatcherStateInternal = {
+ customTesters: [],
+ isNot,
+ promise: promise ?? '',
+ utils,
+ timeout,
+ equals: throwUnsupportedExpectMatcherError as any,
+ };
+
+ if (!promise)
+ return matcher.call(matcherContext, actual, ...args) as MatcherResult | Promise<MatcherResult>;
+
+ if (typeof actual === 'function')
+ actual = actual();
+ if (!isPromise(actual))
+ return { pass: false, message: createExpectedPromiseMessage(matcherName, isNot, promise, actual) };
+
+ if (promise === 'resolves') {
+ return actual.then(
+ result => matcher.call(matcherContext, result, ...args) as MatcherResult | Promise<MatcherResult>,
+ error => ({ pass: false, message: createExpectedToResolveMessage(matcherName, isNot, promise, error) }),
+ );
+ } else {
+ return actual.then(
+ result => ({ pass: false, message: createExpectedToRejectMessage(matcherName, isNot, promise, result) }),
+ error => matcher.call(matcherContext, error, ...args) as MatcherResult | Promise<MatcherResult>,
+ );
}
}
-async function pollMatcher(qualifiedMatcherName: string, info: ExpectMetaInfo, prefix: string[], ...args: any[]) {
+async function invokePollMatcher(
+ matcherName: string,
+ info: ExpectMetaInfo,
+ matcher: RawMatcherFn,
+ actual: unknown,
+ args: any[],
+ promise: 'resolves' | 'rejects' | undefined,
+): Promise<MatcherResult> {
+ if (typeof actual !== 'function')
+ throw new Error('`expect.poll()` accepts only function as a first argument');
+ if (promise || (customAsyncMatchers as any)[matcherName])
+ throw new Error(`\`expect.poll()\` does not support "${promise ?? matcherName}" matcher.`);
+
const testInfo = expectConfig().testInfo;
const poll = info.poll!;
const timeout = poll.timeout ?? info.timeout ?? expectConfig().timeout ?? defaultExpectTimeout;
const { deadline, timeoutMessage } = deadlineForMatcher(testInfo, timeout);
- const result = await pollAgainstDeadline<Error|undefined>(async () => {
+ const result = await pollAgainstDeadline<Error | undefined>(async () => {
if (testInfo && expectConfig().testInfo !== testInfo)
return { continuePolling: false, result: undefined };
-
- const innerInfo: ExpectMetaInfo = {
- ...info,
- isSoft: false, // soft is outside of poll, not inside
- poll: undefined,
- };
- const value = await poll.generator();
+ const value = await actual();
try {
- let matchers = createMatchers(value, innerInfo, prefix);
- if (info.isNot)
- matchers = matchers.not;
- matchers[qualifiedMatcherName](...args);
+ await callMatcherAsStep(matcherName, { ...info, poll: undefined, isSoft: false }, value, matcher, args);
return { continuePolling: false, result: undefined };
} catch (error) {
return { continuePolling: true, result: error };
}
}, deadline, poll.intervals ?? [100, 250, 500, 1000]);
-
if (result.timedOut) {
const message = result.result ? [
result.result.message,
@@ -464,20 +451,20 @@ async function pollMatcher(qualifiedMatcherName: string, info: ExpectMetaInfo, p
`Call Log:`,
`- ${timeoutMessage}`,
].join('\n') : timeoutMessage;
-
- throw new Error(message);
+ return { pass: !!info.isNot, message: () => message };
}
+ return { pass: !info.isNot, message: () => '' };
}
-export const expect: Expect<{}> = createExpect({}, [], {}).extend(customMatchers as any);
+export const expect: Expect<{}> = createExpect({ userMatchers: {} });
export function mergeExpects(...expects: any[]) {
let merged = expect;
for (const e of expects) {
- const internals = e[userMatchersSymbol];
- if (!internals) // non-playwright expects mutate the global expect, so we don't need to do anything special
+ const info: ExpectMetaInfo | undefined = e[META_INFO];
+ if (!info) // non-playwright expects mutate the global expect, so we don't need to do anything special
continue;
- merged = merged.extend(internals);
+ merged = merged.extend(info.userMatchers as any) as typeof merged;
}
return merged;
}
diff --git a/packages/playwright/src/matchers/expectLibrary.ts b/packages/playwright/src/matchers/expectLibrary.ts
new file mode 100644
index 0000000000..03af2f5975
--- /dev/null
+++ b/packages/playwright/src/matchers/expectLibrary.ts
@@ -0,0 +1,1745 @@
+/**
+ * Copyright (c) Meta Platforms, Inc. and affiliates. All Rights Reserved.
+ * Modifications copyright (c) Microsoft Corporation.
+ *
+ * This source code is licensed under the MIT license found here
+ * https://github.com/jestjs/jest/blob/v30.2.0/LICENSE
+ */
+
+// Based on https://github.com/jestjs/jest/tree/v30.2.0/packages/expect
+
+// - Files merged into one.
+// - Reformatted to follow the coding style.
+// - Removed spy matchers: toHaveBeenCalled, toHaveReturned, etc.
+// - Removed expect.assertions() and expect.hasAssertions() support.
+// - Removed global state, INTERNAL_MATCHER_FLAG, addCustomEqualityTesters.
+// - MatcherContext has only stubs for customTesters, getState and dontThrow.
+// - MatcherState has no expand, currentTestName, assertionCalls, etc.
+// - Extracted explicit buildCustomAsymmetricMatcher, createExpectedPromiseMessage,
+// createExpectedToResolveMessage, createExpectedToRejectMessage from internals.
+
+import {
+ arrayBufferEquality,
+ equals,
+ getObjectKeys,
+ getObjectSubset,
+ getPath,
+ isA,
+ isError,
+ iterableEquality,
+ pathAsArray,
+ sparseArrayEquality,
+ subsetEquality,
+ typeEquality,
+} from '@jest/expect-utils';
+import * as matcherUtils from 'jest-matcher-utils';
+import {
+ DIM_COLOR,
+ EXPECTED_COLOR,
+ INVERTED_COLOR,
+ RECEIVED_COLOR,
+ SUGGEST_TO_CONTAIN_EQUAL,
+ ensureExpectedIsNonNegativeInteger,
+ ensureNoExpected,
+ ensureNumbers,
+ getLabelPrinter,
+ matcherErrorMessage,
+ matcherHint,
+ printDiffOrStringify,
+ printExpected,
+ printReceived,
+ printWithType,
+ stringify,
+} from 'jest-matcher-utils';
+import { formatExecError, formatStackTrace, separateMessageFromStack } from 'jest-message-util';
+
+import type { EqualsFunction, Tester } from '@jest/expect-utils';
+import type { MatcherHintOptions } from 'jest-matcher-utils';
+
+// -----------------------------------------------------------------------------
+// Types
+// -----------------------------------------------------------------------------
+
+export type SyncExpectationResult = {
+ pass: boolean;
+ message(): string;
+};
+
+type AsyncExpectationResult = Promise<SyncExpectationResult>;
+
+export type ExpectationResult = SyncExpectationResult | AsyncExpectationResult;
+
+export type RawMatcherFn<Context extends MatcherContext = MatcherContext> = {
+ (this: Context, actual: any, ...expected: Array<any>): ExpectationResult;
+};
+
+export type MatchersObject = {
+ [name: string]: RawMatcherFn;
+};
+
+interface MatcherUtils {
+ customTesters: Array<Tester>;
+ equals: EqualsFunction;
+ utils: typeof matcherUtils & {
+ iterableEquality: Tester;
+ subsetEquality: Tester;
+ };
+}
+
+interface MatcherState {
+ error?: Error;
+ isNot?: boolean;
+ promise?: string;
+}
+
+export type MatcherContext = MatcherUtils & Readonly<MatcherState>;
+
+type AsymmetricMatcherInterface = {
+ asymmetricMatch(other: unknown): boolean;
+ toString(): string;
+ getExpectedType?(): string;
+ toAsymmetricMatcher?(): string;
+};
+
+function getType(value: unknown): string {
+ if (value === undefined)
+ return 'undefined';
+ if (value === null)
+ return 'null';
+ if (Array.isArray(value))
+ return 'array';
+ if (typeof value === 'boolean')
+ return 'boolean';
+ if (typeof value === 'function')
+ return 'function';
+ if (typeof value === 'number')
+ return 'number';
+ if (typeof value === 'string')
+ return 'string';
+ if (typeof value === 'bigint')
+ return 'bigint';
+ if (typeof value === 'object') {
+ if ((value as object).constructor === RegExp)
+ return 'regexp';
+ if ((value as object).constructor === Map)
+ return 'map';
+ if ((value as object).constructor === Set)
+ return 'set';
+ if ((value as object).constructor === Date)
+ return 'date';
+ return 'object';
+ }
+ if (typeof value === 'symbol')
+ return 'symbol';
+ throw new Error(`value of unknown type: ${value as any}`);
+}
+
+const isPrimitive = (value: unknown): boolean => Object(value) !== value;
+
+export function isPromise<T>(candidate: unknown): candidate is Promise<T> {
+ return candidate !== null && candidate !== undefined
+ && (typeof candidate === 'object' || typeof candidate === 'function')
+ && typeof (candidate as any).then === 'function';
+}
+
+// -----------------------------------------------------------------------------
+// Asymmetric matchers (asymmetricMatchers.ts)
+// -----------------------------------------------------------------------------
+
+const functionToString = Function.prototype.toString;
+
+function fnNameFor(func: () => unknown) {
+ if (func.name)
+ return func.name;
+
+ const matches = functionToString
+ .call(func)
+ .match(/^(?:async)?\s*function\s*\*?\s*([\w$]+)\s*\(/);
+ return matches ? matches[1] : '<anonymous>';
+}
+
+export const utils = Object.freeze({
+ ...matcherUtils,
+ iterableEquality,
+ subsetEquality,
+});
+
+function hasProperty(obj: object | null, property: string | symbol): boolean {
+ if (!obj)
+ return false;
+ if (Object.prototype.hasOwnProperty.call(obj, property))
+ return true;
+ return hasProperty(Object.getPrototypeOf(obj), property);
+}
+
+export abstract class AsymmetricMatcher<T> implements AsymmetricMatcherInterface {
+ $$typeof = Symbol.for('jest.asymmetricMatcher');
+
+ constructor(
+ protected sample: T,
+ protected inverse = false,
+ ) {}
+
+ protected getMatcherContext(): MatcherContext {
+ return {
+ customTesters: [],
+ equals,
+ isNot: this.inverse,
+ utils,
+ };
+ }
+
+ abstract asymmetricMatch(other: unknown): boolean;
+ abstract toString(): string;
+ getExpectedType?(): string;
+ toAsymmetricMatcher?(): string;
+}
+
+export function buildCustomAsymmetricMatcher(matcherName: string, matcher: RawMatcherFn) {
+ class CustomMatcher extends AsymmetricMatcher<[unknown, ...Array<unknown>]> {
+ constructor(inverse: boolean = false, ...sample: [unknown, ...Array<unknown>]) {
+ super(sample, inverse);
+ }
+
+ asymmetricMatch(other: unknown) {
+ const { pass } = matcher.call(
+ (this as any).getMatcherContext(),
+ other,
+ ...this.sample,
+ ) as SyncExpectationResult;
+ return this.inverse ? !pass : pass;
+ }
+
+ toString() {
+ return `${this.inverse ? 'not.' : ''}${matcherName}`;
+ }
+
+ override getExpectedType() {
+ return 'any';
+ }
+
+ override toAsymmetricMatcher() {
+ return `${this.toString()}<${this.sample.map(String).join(', ')}>`;
+ }
+ }
+ const positive = (...sample: [unknown, ...Array<unknown>]) => new CustomMatcher(false, ...sample);
+ const inverse = (...sample: [unknown, ...Array<unknown>]) => new CustomMatcher(true, ...sample);
+ return { positive, inverse };
+}
+
+class Any extends AsymmetricMatcher<any> {
+ constructor(sample: unknown) {
+ if (sample === undefined) {
+ throw new TypeError(
+ 'any() expects to be passed a constructor function. ' +
+ 'Please pass one or use anything() to match any object.',
+ );
+ }
+ super(sample);
+ }
+
+ asymmetricMatch(other: unknown) {
+ if (this.sample === String)
+ return typeof other === 'string' || other instanceof String;
+ if (this.sample === Number)
+ return typeof other === 'number' || other instanceof Number;
+ if (this.sample === Function)
+ return typeof other === 'function' || other instanceof Function;
+ if (this.sample === Boolean)
+ return typeof other === 'boolean' || other instanceof Boolean;
+ if (this.sample === BigInt)
+ return typeof other === 'bigint' || other instanceof BigInt;
+ if (this.sample === Symbol)
+ return typeof other === 'symbol' || other instanceof Symbol;
+ if (this.sample === Object)
+ return typeof other === 'object';
+ if (this.sample === Array)
+ return Array.isArray(other);
+ return other instanceof this.sample;
+ }
+
+ toString() {
+ return 'Any';
+ }
+
+ override getExpectedType() {
+ if (this.sample === String)
+ return 'string';
+ if (this.sample === Number)
+ return 'number';
+ if (this.sample === Function)
+ return 'function';
+ if (this.sample === Object)
+ return 'object';
+ if (this.sample === Boolean)
+ return 'boolean';
+ if (this.sample === Array)
+ return 'array';
+ return fnNameFor(this.sample);
+ }
+
+ override toAsymmetricMatcher() {
+ return `Any<${fnNameFor(this.sample)}>`;
+ }
+}
+
+class Anything extends AsymmetricMatcher<void> {
+ asymmetricMatch(other: unknown) {
+ return other !== null && other !== undefined;
+ }
+
+ toString() {
+ return 'Anything';
+ }
+
+ override toAsymmetricMatcher() {
+ return 'Anything';
+ }
+}
+
+class ArrayContaining extends AsymmetricMatcher<Array<unknown>> {
+ constructor(sample: Array<unknown>, inverse = false) {
+ super(sample, inverse);
+ }
+
+ asymmetricMatch(other: unknown) {
+ if (!Array.isArray(this.sample)) {
+ throw new TypeError(
+ `You must provide an array to ${this.toString()}, not '${typeof this
+ .sample}'.`,
+ );
+ }
+
+ const matcherContext = this.getMatcherContext();
+ const result =
+ this.sample.length === 0 ||
+ (Array.isArray(other) &&
+ this.sample.every(item =>
+ other.some(another =>
+ equals(item, another, matcherContext.customTesters),
+ ),
+ ));
+
+ return this.inverse ? !result : result;
+ }
+
+ toString() {
+ return `Array${this.inverse ? 'Not' : ''}Containing`;
+ }
+
+ override getExpectedType() {
+ return 'array';
+ }
+}
+
+class ArrayOf extends AsymmetricMatcher<unknown> {
+ asymmetricMatch(other: unknown) {
+ const matcherContext = this.getMatcherContext();
+ const result =
+ Array.isArray(other) &&
+ other.every(item =>
+ equals(this.sample, item, matcherContext.customTesters),
+ );
+
+ return this.inverse ? !result : result;
+ }
+
+ toString() {
+ return `${this.inverse ? 'Not' : ''}ArrayOf`;
+ }
+
+ override getExpectedType() {
+ return 'array';
+ }
+}
+
+class ObjectContaining extends AsymmetricMatcher<Record<string | symbol, unknown>> {
+ constructor(sample: Record<string | symbol, unknown>, inverse = false) {
+ super(sample, inverse);
+ }
+
+ asymmetricMatch(other: any) {
+ if (typeof this.sample !== 'object') {
+ throw new TypeError(
+ `You must provide an object to ${this.toString()}, not '${typeof this
+ .sample}'.`,
+ );
+ }
+
+ if (typeof other !== 'object' || Array.isArray(other))
+ return false;
+
+ let result = true;
+
+ const matcherContext = this.getMatcherContext();
+ const objectKeys = getObjectKeys(this.sample);
+
+ for (const key of objectKeys) {
+ if (
+ !hasProperty(other, key) ||
+ !equals(this.sample[key], other[key], matcherContext.customTesters)
+ ) {
+ result = false;
+ break;
+ }
+ }
+
+ return this.inverse ? !result : result;
+ }
+
+ toString() {
+ return `Object${this.inverse ? 'Not' : ''}Containing`;
+ }
+
+ override getExpectedType() {
+ return 'object';
+ }
+}
+
+class StringContaining extends AsymmetricMatcher<string> {
+ constructor(sample: string, inverse = false) {
+ if (!isA('String', sample))
+ throw new Error('Expected is not a string');
+ super(sample, inverse);
+ }
+
+ asymmetricMatch(other: unknown) {
+ const result = isA<string>('String', other) && other.includes(this.sample);
+ return this.inverse ? !result : result;
+ }
+
+ toString() {
+ return `String${this.inverse ? 'Not' : ''}Containing`;
+ }
+
+ override getExpectedType() {
+ return 'string';
+ }
+}
+
+class StringMatching extends AsymmetricMatcher<RegExp> {
+ constructor(sample: string | RegExp, inverse = false) {
+ if (!isA('String', sample) && !isA('RegExp', sample))
+ throw new Error('Expected is not a String or a RegExp');
+ super(new RegExp(sample), inverse);
+ }
+
+ asymmetricMatch(other: unknown) {
+ const result = isA<string>('String', other) && this.sample.test(other);
+ return this.inverse ? !result : result;
+ }
+
+ toString() {
+ return `String${this.inverse ? 'Not' : ''}Matching`;
+ }
+
+ override getExpectedType() {
+ return 'string';
+ }
+}
+
+class CloseTo extends AsymmetricMatcher<number> {
+ private readonly precision: number;
+
+ constructor(sample: number, precision = 2, inverse = false) {
+ if (!isA('Number', sample))
+ throw new Error('Expected is not a Number');
+ if (!isA('Number', precision))
+ throw new Error('Precision is not a Number');
+
+ super(sample);
+ this.inverse = inverse;
+ this.precision = precision;
+ }
+
+ asymmetricMatch(other: unknown) {
+ if (!isA<number>('Number', other))
+ return false;
+ let result = false;
+ if (other === Number.POSITIVE_INFINITY && this.sample === Number.POSITIVE_INFINITY)
+ result = true;
+ else if (other === Number.NEGATIVE_INFINITY && this.sample === Number.NEGATIVE_INFINITY)
+ result = true;
+ else
+ result = Math.abs(this.sample - other) < Math.pow(10, -this.precision) / 2;
+ return this.inverse ? !result : result;
+ }
+
+ toString() {
+ return `Number${this.inverse ? 'Not' : ''}CloseTo`;
+ }
+
+ override getExpectedType() {
+ return 'number';
+ }
+
+ override toAsymmetricMatcher(): string {
+ return [
+ this.toString(),
+ this.sample,
+ `(${this.precision} ${this.precision === 1 ? 'digit' : 'digits'})`,
+ ].join(' ');
+ }
+}
+
+export const any = (expectedObject: unknown): Any => new Any(expectedObject);
+export const anything = (): Anything => new Anything();
+export const arrayContaining = (sample: Array<unknown>): ArrayContaining => new ArrayContaining(sample);
+export const arrayNotContaining = (sample: Array<unknown>): ArrayContaining => new ArrayContaining(sample, true);
+export const arrayOf = (sample: unknown): ArrayOf => new ArrayOf(sample);
+export const notArrayOf = (sample: unknown): ArrayOf => new ArrayOf(sample, true);
+export const objectContaining = (sample: Record<string, unknown>): ObjectContaining => new ObjectContaining(sample);
+export const objectNotContaining = (sample: Record<string, unknown>): ObjectContaining => new ObjectContaining(sample, true);
+export const stringContaining = (expected: string): StringContaining => new StringContaining(expected);
+export const stringNotContaining = (expected: string): StringContaining => new StringContaining(expected, true);
+export const stringMatching = (expected: string | RegExp): StringMatching => new StringMatching(expected);
+export const stringNotMatching = (expected: string | RegExp): StringMatching => new StringMatching(expected, true);
+export const closeTo = (expected: number, precision?: number): CloseTo => new CloseTo(expected, precision);
+export const notCloseTo = (expected: number, precision?: number): CloseTo => new CloseTo(expected, precision, true);
+
+// -----------------------------------------------------------------------------
+// Print helpers (print.ts)
+// -----------------------------------------------------------------------------
+
+const printSubstring = (val: string): string => val.replace(/"|\\/g, '\\$&');
+
+const printReceivedStringContainExpectedSubstring = (
+ received: string,
+ start: number,
+ length: number,
+): string =>
+ RECEIVED_COLOR(
+ `"${printSubstring(received.slice(0, start))}${INVERTED_COLOR(
+ printSubstring(received.slice(start, start + length)),
+ )}${printSubstring(received.slice(start + length))}"`,
+ );
+
+const printReceivedStringContainExpectedResult = (
+ received: string,
+ result: RegExpExecArray | null,
+): string =>
+ result === null
+ ? printReceived(received)
+ : printReceivedStringContainExpectedSubstring(received, result.index, result[0].length);
+
+const printReceivedArrayContainExpectedItem = (
+ received: Array<unknown>,
+ index: number,
+): string =>
+ RECEIVED_COLOR(
+ `[${received
+ .map((item, i) => {
+ const stringified = stringify(item);
+ return i === index ? INVERTED_COLOR(stringified) : stringified;
+ })
+ .join(', ')}]`,
+ );
+
+const printCloseTo = (
+ receivedDiff: number,
+ expectedDiff: number,
+ precision: number,
+ isNot: boolean | undefined,
+): string => {
+ const receivedDiffString = stringify(receivedDiff);
+ const expectedDiffString = receivedDiffString.includes('e')
+ ? expectedDiff.toExponential(0)
+ : 0 <= precision && precision < 20
+ ? expectedDiff.toFixed(precision + 1)
+ : stringify(expectedDiff);
+
+ return (
+ `Expected precision: ${isNot ? ' ' : ''} ${stringify(precision)}\n` +
+ `Expected difference: ${isNot ? 'not ' : ''}< ${EXPECTED_COLOR(expectedDiffString)}\n` +
+ `Received difference: ${isNot ? ' ' : ''} ${RECEIVED_COLOR(receivedDiffString)}`
+ );
+};
+
+const printConstructorName = (
+ label: string,
+ constructor: Function,
+ isNot: boolean,
+ isExpected: boolean,
+): string =>
+ typeof constructor.name === 'string'
+ ? constructor.name.length === 0
+ ? `${label} name is an empty string`
+ : `${label}: ${isNot ? (isExpected ? 'not ' : ' ') : ''}${
+ isExpected
+ ? EXPECTED_COLOR(constructor.name)
+ : RECEIVED_COLOR(constructor.name)
+ }`
+ : `${label} name is not a string`;
+
+const printExpectedConstructorName = (label: string, expected: Function): string =>
+ `${printConstructorName(label, expected, false, true)}\n`;
+
+const printExpectedConstructorNameNot = (label: string, expected: Function): string =>
+ `${printConstructorName(label, expected, true, true)}\n`;
+
+const printReceivedConstructorName = (label: string, received: Function): string =>
+ `${printConstructorName(label, received, false, false)}\n`;
+
+const printReceivedConstructorNameNot = (
+ label: string,
+ received: Function,
+ expected: Function,
+): string =>
+ typeof expected.name === 'string' &&
+ expected.name.length > 0 &&
+ typeof received.name === 'string' &&
+ received.name.length > 0
+ ? `${printConstructorName(label, received, true, false)} ${
+ Object.getPrototypeOf(received) === expected
+ ? 'extends'
+ : 'extends … extends'
+ } ${EXPECTED_COLOR(expected.name)}\n`
+ : `${printConstructorName(label, received, false, false)}\n`;
+
+// -----------------------------------------------------------------------------
+// Default matchers (matchers.ts)
+// -----------------------------------------------------------------------------
+
+const EXPECTED_LABEL = 'Expected';
+const RECEIVED_LABEL = 'Received';
+const EXPECTED_VALUE_LABEL = 'Expected value';
+const RECEIVED_VALUE_LABEL = 'Received value';
+
+const toStrictEqualTesters = [
+ iterableEquality,
+ typeEquality,
+ sparseArrayEquality,
+ arrayBufferEquality,
+];
+
+type ContainIterable =
+ | Array<unknown>
+ | Set<unknown>
+ | NodeListOf<Node>
+ | DOMTokenList
+ | HTMLCollectionOf<any>;
+
+export const matchers: MatchersObject = {
+ toBe(received: unknown, expected: unknown) {
+ const matcherName = 'toBe';
+ const options: MatcherHintOptions = {
+ comment: 'Object.is equality',
+ isNot: this.isNot,
+ promise: this.promise,
+ };
+
+ const pass = Object.is(received, expected);
+
+ const message = pass
+ ? () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ `Expected: not ${printExpected(expected)}`
+ : () => {
+ const expectedType = getType(expected);
+
+ let deepEqualityName = null;
+ if (expectedType !== 'map' && expectedType !== 'set') {
+ if (equals(received, expected, [...this.customTesters, ...toStrictEqualTesters], true))
+ deepEqualityName = 'toStrictEqual';
+ else if (equals(received, expected, [...this.customTesters, iterableEquality]))
+ deepEqualityName = 'toEqual';
+ }
+
+ return (
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ (deepEqualityName === null
+ ? ''
+ : `${DIM_COLOR(
+ `If it should pass with deep equality, replace "${matcherName}" with "${deepEqualityName}"`,
+ )}\n\n`) +
+ printDiffOrStringify(expected, received, EXPECTED_LABEL, RECEIVED_LABEL, false)
+ );
+ };
+
+ return { actual: received, expected, message, name: matcherName, pass };
+ },
+
+ toBeCloseTo(received: number, expected: number, precision = 2) {
+ const matcherName = 'toBeCloseTo';
+ const secondArgument = arguments.length === 3 ? 'precision' : undefined;
+ const isNot = this.isNot;
+ const options: MatcherHintOptions = {
+ isNot,
+ promise: this.promise,
+ secondArgument,
+ secondArgumentColor: (arg: string) => arg,
+ };
+
+ if (typeof expected !== 'number') {
+ throw new TypeError(
+ matcherErrorMessage(
+ matcherHint(matcherName, undefined, undefined, options),
+ `${EXPECTED_COLOR('expected')} value must be a number`,
+ printWithType('Expected', expected, printExpected),
+ ),
+ );
+ }
+
+ if (typeof received !== 'number') {
+ throw new TypeError(
+ matcherErrorMessage(
+ matcherHint(matcherName, undefined, undefined, options),
+ `${RECEIVED_COLOR('received')} value must be a number`,
+ printWithType('Received', received, printReceived),
+ ),
+ );
+ }
+
+ let pass = false;
+ let expectedDiff = 0;
+ let receivedDiff = 0;
+
+ if (received === Number.POSITIVE_INFINITY && expected === Number.POSITIVE_INFINITY) {
+ pass = true;
+ } else if (received === Number.NEGATIVE_INFINITY && expected === Number.NEGATIVE_INFINITY) {
+ pass = true;
+ } else {
+ expectedDiff = Math.pow(10, -precision) / 2;
+ receivedDiff = Math.abs(expected - received);
+ pass = receivedDiff < expectedDiff;
+ }
+
+ const message = pass
+ ? () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ `Expected: not ${printExpected(expected)}\n` +
+ (receivedDiff === 0
+ ? ''
+ : `Received: ${printReceived(received)}\n` +
+ `\n${printCloseTo(receivedDiff, expectedDiff, precision, isNot)}`)
+ : () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ `Expected: ${printExpected(expected)}\n` +
+ `Received: ${printReceived(received)}\n` +
+ '\n' +
+ printCloseTo(receivedDiff, expectedDiff, precision, isNot);
+
+ return { message, pass };
+ },
+
+ toBeDefined(received: unknown, expected: void) {
+ const matcherName = 'toBeDefined';
+ const options: MatcherHintOptions = { isNot: this.isNot, promise: this.promise };
+ ensureNoExpected(expected, matcherName, options);
+
+ const pass = received !== void 0;
+ const message = () =>
+ matcherHint(matcherName, undefined, '', options) +
+ '\n\n' +
+ `Received: ${printReceived(received)}`;
+
+ return { message, pass };
+ },
+
+ toBeFalsy(received: unknown, expected: void) {
+ const matcherName = 'toBeFalsy';
+ const options: MatcherHintOptions = { isNot: this.isNot, promise: this.promise };
+ ensureNoExpected(expected, matcherName, options);
+
+ const pass = !received;
+ const message = () =>
+ matcherHint(matcherName, undefined, '', options) +
+ '\n\n' +
+ `Received: ${printReceived(received)}`;
+
+ return { message, pass };
+ },
+
+ toBeGreaterThan(received: number | bigint, expected: number | bigint) {
+ const matcherName = 'toBeGreaterThan';
+ const isNot = this.isNot;
+ const options: MatcherHintOptions = { isNot, promise: this.promise };
+ ensureNumbers(received, expected, matcherName, options);
+
+ const pass = received > expected;
+ const message = () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ `Expected:${isNot ? ' not' : ''} > ${printExpected(expected)}\n` +
+ `Received:${isNot ? ' ' : ''} ${printReceived(received)}`;
+
+ return { message, pass };
+ },
+
+ toBeGreaterThanOrEqual(received: number | bigint, expected: number | bigint) {
+ const matcherName = 'toBeGreaterThanOrEqual';
+ const isNot = this.isNot;
+ const options: MatcherHintOptions = { isNot, promise: this.promise };
+ ensureNumbers(received, expected, matcherName, options);
+
+ const pass = received >= expected;
+ const message = () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ `Expected:${isNot ? ' not' : ''} >= ${printExpected(expected)}\n` +
+ `Received:${isNot ? ' ' : ''} ${printReceived(received)}`;
+
+ return { message, pass };
+ },
+
+ toBeInstanceOf(received: any, expected: Function) {
+ const matcherName = 'toBeInstanceOf';
+ const options: MatcherHintOptions = { isNot: this.isNot, promise: this.promise };
+
+ if (typeof expected !== 'function') {
+ throw new TypeError(
+ matcherErrorMessage(
+ matcherHint(matcherName, undefined, undefined, options),
+ `${EXPECTED_COLOR('expected')} value must be a function`,
+ printWithType('Expected', expected, printExpected),
+ ),
+ );
+ }
+
+ const pass = received instanceof expected;
+
+ const message = pass
+ ? () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ printExpectedConstructorNameNot('Expected constructor', expected) +
+ (typeof received.constructor === 'function' && received.constructor !== expected
+ ? printReceivedConstructorNameNot('Received constructor', received.constructor, expected)
+ : '')
+ : () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ printExpectedConstructorName('Expected constructor', expected) +
+ (isPrimitive(received) || Object.getPrototypeOf(received) === null
+ ? `\nReceived value has no prototype\nReceived value: ${printReceived(received)}`
+ : typeof received.constructor === 'function'
+ ? printReceivedConstructorName('Received constructor', received.constructor)
+ : `\nReceived value: ${printReceived(received)}`);
+
+ return { message, pass };
+ },
+
+ toBeLessThan(received: number | bigint, expected: number | bigint) {
+ const matcherName = 'toBeLessThan';
+ const isNot = this.isNot;
+ const options: MatcherHintOptions = { isNot, promise: this.promise };
+ ensureNumbers(received, expected, matcherName, options);
+
+ const pass = received < expected;
+ const message = () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ `Expected:${isNot ? ' not' : ''} < ${printExpected(expected)}\n` +
+ `Received:${isNot ? ' ' : ''} ${printReceived(received)}`;
+
+ return { message, pass };
+ },
+
+ toBeLessThanOrEqual(received: number | bigint, expected: number | bigint) {
+ const matcherName = 'toBeLessThanOrEqual';
+ const isNot = this.isNot;
+ const options: MatcherHintOptions = { isNot, promise: this.promise };
+ ensureNumbers(received, expected, matcherName, options);
+
+ const pass = received <= expected;
+ const message = () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ `Expected:${isNot ? ' not' : ''} <= ${printExpected(expected)}\n` +
+ `Received:${isNot ? ' ' : ''} ${printReceived(received)}`;
+
+ return { message, pass };
+ },
+
+ toBeNaN(received: any, expected: void) {
+ const matcherName = 'toBeNaN';
+ const options: MatcherHintOptions = { isNot: this.isNot, promise: this.promise };
+ ensureNoExpected(expected, matcherName, options);
+
+ const pass = Number.isNaN(received);
+ const message = () =>
+ matcherHint(matcherName, undefined, '', options) +
+ '\n\n' +
+ `Received: ${printReceived(received)}`;
+
+ return { message, pass };
+ },
+
+ toBeNull(received: unknown, expected: void) {
+ const matcherName = 'toBeNull';
+ const options: MatcherHintOptions = { isNot: this.isNot, promise: this.promise };
+ ensureNoExpected(expected, matcherName, options);
+
+ const pass = received === null;
+ const message = () =>
+ matcherHint(matcherName, undefined, '', options) +
+ '\n\n' +
+ `Received: ${printReceived(received)}`;
+
+ return { message, pass };
+ },
+
+ toBeTruthy(received: unknown, expected: void) {
+ const matcherName = 'toBeTruthy';
+ const options: MatcherHintOptions = { isNot: this.isNot, promise: this.promise };
+ ensureNoExpected(expected, matcherName, options);
+
+ const pass = !!received;
+ const message = () =>
+ matcherHint(matcherName, undefined, '', options) +
+ '\n\n' +
+ `Received: ${printReceived(received)}`;
+
+ return { message, pass };
+ },
+
+ toBeUndefined(received: unknown, expected: void) {
+ const matcherName = 'toBeUndefined';
+ const options: MatcherHintOptions = { isNot: this.isNot, promise: this.promise };
+ ensureNoExpected(expected, matcherName, options);
+
+ const pass = received === void 0;
+ const message = () =>
+ matcherHint(matcherName, undefined, '', options) +
+ '\n\n' +
+ `Received: ${printReceived(received)}`;
+
+ return { message, pass };
+ },
+
+ toContain(received: ContainIterable | string, expected: unknown) {
+ const matcherName = 'toContain';
+ const isNot = this.isNot;
+ const options: MatcherHintOptions = { comment: 'indexOf', isNot, promise: this.promise };
+
+ if (received === null || received === undefined) {
+ throw new Error(
+ matcherErrorMessage(
+ matcherHint(matcherName, undefined, undefined, options),
+ `${RECEIVED_COLOR('received')} value must not be null nor undefined`,
+ printWithType('Received', received, printReceived),
+ ),
+ );
+ }
+
+ if (typeof received === 'string') {
+ const wrongTypeErrorMessage = `${EXPECTED_COLOR('expected')} value must be a string if ${RECEIVED_COLOR('received')} value is a string`;
+
+ if (typeof expected !== 'string') {
+ throw new TypeError(
+ matcherErrorMessage(
+ matcherHint(matcherName, received, String(expected), options),
+ wrongTypeErrorMessage,
+ printWithType('Expected', expected, printExpected) +
+ '\n' +
+ printWithType('Received', received, printReceived),
+ ),
+ );
+ }
+
+ const index = received.indexOf(String(expected));
+ const pass = index !== -1;
+
+ const message = () => {
+ const labelExpected = `Expected ${typeof expected === 'string' ? 'substring' : 'value'}`;
+ const labelReceived = 'Received string';
+ const printLabel = getLabelPrinter(labelExpected, labelReceived);
+
+ return (
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ `${printLabel(labelExpected)}${isNot ? 'not ' : ''}${printExpected(expected)}\n` +
+ `${printLabel(labelReceived)}${isNot ? ' ' : ''}${
+ isNot
+ ? printReceivedStringContainExpectedSubstring(received, index, String(expected).length)
+ : printReceived(received)
+ }`
+ );
+ };
+
+ return { message, pass };
+ }
+
+ const indexable = [...received];
+ const index = indexable.indexOf(expected);
+ const pass = index !== -1;
+
+ const message = () => {
+ const labelExpected = 'Expected value';
+ const labelReceived = `Received ${getType(received)}`;
+ const printLabel = getLabelPrinter(labelExpected, labelReceived);
+
+ return (
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ `${printLabel(labelExpected)}${isNot ? 'not ' : ''}${printExpected(expected)}\n` +
+ `${printLabel(labelReceived)}${isNot ? ' ' : ''}${
+ isNot && Array.isArray(received)
+ ? printReceivedArrayContainExpectedItem(received, index)
+ : printReceived(received)
+ }` +
+ (!isNot &&
+ indexable.some(item =>
+ equals(item, expected, [...this.customTesters, iterableEquality]),
+ )
+ ? `\n\n${SUGGEST_TO_CONTAIN_EQUAL}`
+ : '')
+ );
+ };
+
+ return { message, pass };
+ },
+
+ toContainEqual(received: ContainIterable, expected: unknown) {
+ const matcherName = 'toContainEqual';
+ const isNot = this.isNot;
+ const options: MatcherHintOptions = { comment: 'deep equality', isNot, promise: this.promise };
+
+ if (received === null || received === undefined) {
+ throw new Error(
+ matcherErrorMessage(
+ matcherHint(matcherName, undefined, undefined, options),
+ `${RECEIVED_COLOR('received')} value must not be null nor undefined`,
+ printWithType('Received', received, printReceived),
+ ),
+ );
+ }
+
+ const index = [...received].findIndex(item =>
+ equals(item, expected, [...this.customTesters, iterableEquality]),
+ );
+ const pass = index !== -1;
+
+ const message = () => {
+ const labelExpected = 'Expected value';
+ const labelReceived = `Received ${getType(received)}`;
+ const printLabel = getLabelPrinter(labelExpected, labelReceived);
+
+ return (
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ `${printLabel(labelExpected)}${isNot ? 'not ' : ''}${printExpected(expected)}\n` +
+ `${printLabel(labelReceived)}${isNot ? ' ' : ''}${
+ isNot && Array.isArray(received)
+ ? printReceivedArrayContainExpectedItem(received, index)
+ : printReceived(received)
+ }`
+ );
+ };
+
+ return { message, pass };
+ },
+
+ toEqual(received: unknown, expected: unknown) {
+ const matcherName = 'toEqual';
+ const options: MatcherHintOptions = { comment: 'deep equality', isNot: this.isNot, promise: this.promise };
+
+ const pass = equals(received, expected, [...this.customTesters, iterableEquality]);
+
+ const message = pass
+ ? () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ `Expected: not ${printExpected(expected)}\n` +
+ (stringify(expected) === stringify(received)
+ ? ''
+ : `Received: ${printReceived(received)}`)
+ : () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ printDiffOrStringify(expected, received, EXPECTED_LABEL, RECEIVED_LABEL, false);
+
+ return { actual: received, expected, message, name: matcherName, pass };
+ },
+
+ toHaveLength(received: any, expected: number) {
+ const matcherName = 'toHaveLength';
+ const isNot = this.isNot;
+ const options: MatcherHintOptions = { isNot, promise: this.promise };
+
+ if (typeof received?.length !== 'number') {
+ throw new TypeError(
+ matcherErrorMessage(
+ matcherHint(matcherName, undefined, undefined, options),
+ `${RECEIVED_COLOR('received')} value must have a length property whose value must be a number`,
+ printWithType('Received', received, printReceived),
+ ),
+ );
+ }
+
+ ensureExpectedIsNonNegativeInteger(expected, matcherName, options);
+
+ const pass = received.length === expected;
+
+ const message = () => {
+ const labelExpected = 'Expected length';
+ const labelReceivedLength = 'Received length';
+ const labelReceivedValue = `Received ${getType(received)}`;
+ const printLabel = getLabelPrinter(labelExpected, labelReceivedLength, labelReceivedValue);
+
+ return (
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ `${printLabel(labelExpected)}${isNot ? 'not ' : ''}${printExpected(expected)}\n` +
+ (isNot ? '' : `${printLabel(labelReceivedLength)}${printReceived(received.length)}\n`) +
+ `${printLabel(labelReceivedValue)}${isNot ? ' ' : ''}${printReceived(received)}`
+ );
+ };
+
+ return { message, pass };
+ },
+
+ toHaveProperty(received: object, expectedPath: string | Array<string>, expectedValue?: unknown) {
+ const matcherName = 'toHaveProperty';
+ const expectedArgument = 'path';
+ const hasValue = arguments.length === 3;
+ const options: MatcherHintOptions = {
+ isNot: this.isNot,
+ promise: this.promise,
+ secondArgument: hasValue ? 'value' : '',
+ };
+
+ if (received === null || received === undefined) {
+ throw new Error(
+ matcherErrorMessage(
+ matcherHint(matcherName, undefined, expectedArgument, options),
+ `${RECEIVED_COLOR('received')} value must not be null nor undefined`,
+ printWithType('Received', received, printReceived),
+ ),
+ );
+ }
+
+ const expectedPathType = getType(expectedPath);
+
+ if (expectedPathType !== 'string' && expectedPathType !== 'array') {
+ throw new Error(
+ matcherErrorMessage(
+ matcherHint(matcherName, undefined, expectedArgument, options),
+ `${EXPECTED_COLOR('expected')} path must be a string or array`,
+ printWithType('Expected', expectedPath, printExpected),
+ ),
+ );
+ }
+
+ const expectedPathLength =
+ typeof expectedPath === 'string' ? pathAsArray(expectedPath).length : expectedPath.length;
+
+ if (expectedPathType === 'array' && expectedPathLength === 0) {
+ throw new Error(
+ matcherErrorMessage(
+ matcherHint(matcherName, undefined, expectedArgument, options),
+ `${EXPECTED_COLOR('expected')} path must not be an empty array`,
+ printWithType('Expected', expectedPath, printExpected),
+ ),
+ );
+ }
+
+ const result = getPath(received, expectedPath);
+ const { lastTraversedObject, endPropIsDefined, hasEndProp, value } = result;
+ const receivedPath = result.traversedPath;
+ const hasCompletePath = receivedPath.length === expectedPathLength;
+ const receivedValue = hasCompletePath ? result.value : lastTraversedObject;
+
+ const pass =
+ hasValue && endPropIsDefined
+ ? equals(value, expectedValue, [...this.customTesters, iterableEquality])
+ : Boolean(hasEndProp);
+
+ const message = pass
+ ? () =>
+ matcherHint(matcherName, undefined, expectedArgument, options) +
+ '\n\n' +
+ (hasValue
+ ? `Expected path: ${printExpected(expectedPath)}\n\n` +
+ `Expected value: not ${printExpected(expectedValue)}${
+ stringify(expectedValue) === stringify(receivedValue)
+ ? ''
+ : `\nReceived value: ${printReceived(receivedValue)}`
+ }`
+ : `Expected path: not ${printExpected(expectedPath)}\n\n` +
+ `Received value: ${printReceived(receivedValue)}`)
+ : () =>
+ matcherHint(matcherName, undefined, expectedArgument, options) +
+ '\n\n' +
+ `Expected path: ${printExpected(expectedPath)}\n` +
+ (hasCompletePath
+ ? `\n${printDiffOrStringify(
+ expectedValue,
+ receivedValue,
+ EXPECTED_VALUE_LABEL,
+ RECEIVED_VALUE_LABEL,
+ false,
+ )}`
+ : `Received path: ${printReceived(
+ expectedPathType === 'array' || receivedPath.length === 0
+ ? receivedPath
+ : receivedPath.join('.'),
+ )}\n\n${
+ hasValue ? `Expected value: ${printExpected(expectedValue)}\n` : ''
+ }Received value: ${printReceived(receivedValue)}`);
+
+ return { message, pass };
+ },
+
+ toMatch(received: string, expected: string | RegExp) {
+ const matcherName = 'toMatch';
+ const options: MatcherHintOptions = { isNot: this.isNot, promise: this.promise };
+
+ if (typeof received !== 'string') {
+ throw new TypeError(
+ matcherErrorMessage(
+ matcherHint(matcherName, undefined, undefined, options),
+ `${RECEIVED_COLOR('received')} value must be a string`,
+ printWithType('Received', received, printReceived),
+ ),
+ );
+ }
+
+ if (!(typeof expected === 'string') && !(expected && typeof expected.test === 'function')) {
+ throw new Error(
+ matcherErrorMessage(
+ matcherHint(matcherName, undefined, undefined, options),
+ `${EXPECTED_COLOR('expected')} value must be a string or regular expression`,
+ printWithType('Expected', expected, printExpected),
+ ),
+ );
+ }
+
+ const pass =
+ typeof expected === 'string'
+ ? received.includes(expected)
+ : new RegExp(expected).test(received);
+
+ const message = pass
+ ? () =>
+ typeof expected === 'string'
+ ? matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ `Expected substring: not ${printExpected(expected)}\n` +
+ `Received string: ${printReceivedStringContainExpectedSubstring(
+ received,
+ received.indexOf(expected),
+ expected.length,
+ )}`
+ : matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ `Expected pattern: not ${printExpected(expected)}\n` +
+ `Received string: ${printReceivedStringContainExpectedResult(
+ received,
+ typeof expected.exec === 'function' ? expected.exec(received) : null,
+ )}`
+ : () => {
+ const labelExpected = `Expected ${typeof expected === 'string' ? 'substring' : 'pattern'}`;
+ const labelReceived = 'Received string';
+ const printLabel = getLabelPrinter(labelExpected, labelReceived);
+
+ return (
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ `${printLabel(labelExpected)}${printExpected(expected)}\n` +
+ `${printLabel(labelReceived)}${printReceived(received)}`
+ );
+ };
+
+ return { message, pass };
+ },
+
+ toMatchObject(received: object, expected: object) {
+ const matcherName = 'toMatchObject';
+ const options: MatcherHintOptions = { isNot: this.isNot, promise: this.promise };
+
+ if (typeof received !== 'object' || received === null) {
+ throw new Error(
+ matcherErrorMessage(
+ matcherHint(matcherName, undefined, undefined, options),
+ `${RECEIVED_COLOR('received')} value must be a non-null object`,
+ printWithType('Received', received, printReceived),
+ ),
+ );
+ }
+
+ if (typeof expected !== 'object' || expected === null) {
+ throw new Error(
+ matcherErrorMessage(
+ matcherHint(matcherName, undefined, undefined, options),
+ `${EXPECTED_COLOR('expected')} value must be a non-null object`,
+ printWithType('Expected', expected, printExpected),
+ ),
+ );
+ }
+
+ const pass = equals(received, expected, [...this.customTesters, iterableEquality, subsetEquality]);
+
+ const message = pass
+ ? () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ `Expected: not ${printExpected(expected)}` +
+ (stringify(expected) === stringify(received)
+ ? ''
+ : `\nReceived: ${printReceived(received)}`)
+ : () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ printDiffOrStringify(
+ expected,
+ getObjectSubset(received, expected, this.customTesters),
+ EXPECTED_LABEL,
+ RECEIVED_LABEL,
+ false,
+ );
+
+ return { message, pass };
+ },
+
+ toStrictEqual(received: unknown, expected: unknown) {
+ const matcherName = 'toStrictEqual';
+ const options: MatcherHintOptions = { comment: 'deep equality', isNot: this.isNot, promise: this.promise };
+
+ const pass = equals(received, expected, [...this.customTesters, ...toStrictEqualTesters], true);
+
+ const message = pass
+ ? () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ `Expected: not ${printExpected(expected)}\n` +
+ (stringify(expected) === stringify(received)
+ ? ''
+ : `Received: ${printReceived(received)}`)
+ : () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ printDiffOrStringify(expected, received, EXPECTED_LABEL, RECEIVED_LABEL, false);
+
+ return { actual: received, expected, message, name: matcherName, pass };
+ },
+};
+
+// -----------------------------------------------------------------------------
+// toThrow matcher (toThrowMatchers.ts)
+// -----------------------------------------------------------------------------
+
+const DID_NOT_THROW = 'Received function did not throw';
+
+type Thrown =
+ | { hasMessage: true; isError: true; message: string; value: Error }
+ | { hasMessage: boolean; isError: false; message: string; value: any };
+
+const getThrown = (e: any): Thrown => {
+ const hasMessage = e !== null && e !== undefined && typeof e.message === 'string';
+
+ if (hasMessage && typeof e.name === 'string' && typeof e.stack === 'string')
+ return { hasMessage, isError: true, message: e.message, value: e };
+
+ return {
+ hasMessage,
+ isError: false,
+ message: hasMessage ? e.message : String(e),
+ value: e,
+ };
+};
+
+export const createThrowMatcher = (matcherName: string, fromPromise?: boolean): RawMatcherFn =>
+ function(this: MatcherContext, received: any, expected: any): ExpectationResult {
+ const options = { isNot: this.isNot, promise: this.promise };
+
+ let thrown: Thrown | null = null;
+
+ if (fromPromise && isError(received)) {
+ thrown = getThrown(received);
+ } else {
+ if (typeof received === 'function') {
+ try {
+ received();
+ } catch (error) {
+ thrown = getThrown(error);
+ }
+ } else if (!fromPromise) {
+ const placeholder = expected === undefined ? '' : 'expected';
+ throw new Error(
+ matcherErrorMessage(
+ matcherHint(matcherName, undefined, placeholder, options),
+ `${RECEIVED_COLOR('received')} value must be a function`,
+ printWithType('Received', received, printReceived),
+ ),
+ );
+ }
+ }
+
+ if (expected === undefined)
+ return toThrow(matcherName, options, thrown);
+ if (typeof expected === 'function')
+ return toThrowExpectedClass(matcherName, options, thrown, expected);
+ if (typeof expected === 'string')
+ return toThrowExpectedString(matcherName, options, thrown, expected);
+ if (expected !== null && typeof expected.test === 'function')
+ return toThrowExpectedRegExp(matcherName, options, thrown, expected);
+ if (expected !== null && typeof expected.asymmetricMatch === 'function')
+ return toThrowExpectedAsymmetric(matcherName, options, thrown, expected);
+ if (expected !== null && typeof expected === 'object')
+ return toThrowExpectedObject(matcherName, options, thrown, expected);
+ throw new Error(
+ matcherErrorMessage(
+ matcherHint(matcherName, undefined, undefined, options),
+ `${EXPECTED_COLOR('expected')} value must be a string or regular expression or class or error`,
+ printWithType('Expected', expected, printExpected),
+ ),
+ );
+ };
+
+const toThrowExpectedRegExp = (
+ matcherName: string,
+ options: MatcherHintOptions,
+ thrown: Thrown | null,
+ expected: RegExp,
+): SyncExpectationResult => {
+ const pass = thrown !== null && expected.test(thrown.message);
+
+ const message = pass
+ ? () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ formatExpected('Expected pattern: not ', expected) +
+ (thrown !== null && thrown.hasMessage
+ ? formatReceived('Received message: ', thrown, 'message', expected) + formatStack(thrown)
+ : formatReceived('Received value: ', thrown, 'value'))
+ : () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ formatExpected('Expected pattern: ', expected) +
+ (thrown === null
+ ? `\n${DID_NOT_THROW}`
+ : thrown.hasMessage
+ ? formatReceived('Received message: ', thrown, 'message') + formatStack(thrown)
+ : formatReceived('Received value: ', thrown, 'value'));
+
+ return { message, pass };
+};
+
+type AsymmetricMatcherExpected = { asymmetricMatch: (received: unknown) => boolean };
+
+const toThrowExpectedAsymmetric = (
+ matcherName: string,
+ options: MatcherHintOptions,
+ thrown: Thrown | null,
+ expected: AsymmetricMatcherExpected,
+): SyncExpectationResult => {
+ const pass = thrown !== null && expected.asymmetricMatch(thrown.value);
+
+ const message = pass
+ ? () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ formatExpected('Expected asymmetric matcher: not ', expected) +
+ '\n' +
+ (thrown !== null && thrown.hasMessage
+ ? formatReceived('Received name: ', thrown, 'name') +
+ formatReceived('Received message: ', thrown, 'message') +
+ formatStack(thrown)
+ : formatReceived('Thrown value: ', thrown, 'value'))
+ : () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ formatExpected('Expected asymmetric matcher: ', expected) +
+ '\n' +
+ (thrown === null
+ ? DID_NOT_THROW
+ : thrown.hasMessage
+ ? formatReceived('Received name: ', thrown, 'name') +
+ formatReceived('Received message: ', thrown, 'message') +
+ formatStack(thrown)
+ : formatReceived('Thrown value: ', thrown, 'value'));
+
+ return { message, pass };
+};
+
+const toThrowExpectedObject = (
+ matcherName: string,
+ options: MatcherHintOptions,
+ thrown: Thrown | null,
+ expected: Error,
+): SyncExpectationResult => {
+ const expectedMessageAndCause = createMessageAndCause(expected);
+ const thrownMessageAndCause = thrown === null ? null : createMessageAndCause(thrown.value);
+ const isCompareErrorInstance = thrown?.isError && expected instanceof Error;
+ const isExpectedCustomErrorInstance = expected.constructor.name !== Error.name;
+
+ const pass =
+ thrown !== null &&
+ thrown.message === expected.message &&
+ thrownMessageAndCause === expectedMessageAndCause &&
+ (!isCompareErrorInstance || !isExpectedCustomErrorInstance || thrown.value instanceof expected.constructor);
+
+ const message = pass
+ ? () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ formatExpected(`Expected ${messageAndCause(expected)}: not `, expectedMessageAndCause) +
+ (thrown !== null && thrown.hasMessage
+ ? formatStack(thrown)
+ : formatReceived('Received value: ', thrown, 'value'))
+ : () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ (thrown === null
+ ? formatExpected(`Expected ${messageAndCause(expected)}: `, expectedMessageAndCause) +
+ '\n' +
+ DID_NOT_THROW
+ : thrown.hasMessage
+ ? printDiffOrStringify(
+ expectedMessageAndCause,
+ thrownMessageAndCause,
+ `Expected ${messageAndCause(expected)}`,
+ `Received ${messageAndCause(thrown.value)}`,
+ true,
+ ) +
+ '\n' +
+ formatStack(thrown)
+ : formatExpected(`Expected ${messageAndCause(expected)}: `, expectedMessageAndCause) +
+ formatReceived('Received value: ', thrown, 'value'));
+
+ return { message, pass };
+};
+
+const toThrowExpectedClass = (
+ matcherName: string,
+ options: MatcherHintOptions,
+ thrown: Thrown | null,
+ expected: Function,
+): SyncExpectationResult => {
+ const pass = thrown !== null && thrown.value instanceof expected;
+
+ const message = pass
+ ? () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ printExpectedConstructorNameNot('Expected constructor', expected) +
+ (thrown !== null &&
+ thrown.value !== null && thrown.value !== undefined &&
+ typeof thrown.value.constructor === 'function' &&
+ thrown.value.constructor !== expected
+ ? printReceivedConstructorNameNot('Received constructor', thrown.value.constructor, expected)
+ : '') +
+ '\n' +
+ (thrown !== null && thrown.hasMessage
+ ? formatReceived('Received message: ', thrown, 'message') + formatStack(thrown)
+ : formatReceived('Received value: ', thrown, 'value'))
+ : () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ printExpectedConstructorName('Expected constructor', expected) +
+ (thrown === null
+ ? `\n${DID_NOT_THROW}`
+ : `${
+ thrown.value !== null && thrown.value !== undefined && typeof thrown.value.constructor === 'function'
+ ? printReceivedConstructorName('Received constructor', thrown.value.constructor)
+ : ''
+ }\n${
+ thrown.hasMessage
+ ? formatReceived('Received message: ', thrown, 'message') + formatStack(thrown)
+ : formatReceived('Received value: ', thrown, 'value')
+ }`);
+
+ return { message, pass };
+};
+
+const toThrowExpectedString = (
+ matcherName: string,
+ options: MatcherHintOptions,
+ thrown: Thrown | null,
+ expected: string,
+): SyncExpectationResult => {
+ const pass = thrown !== null && thrown.message.includes(expected);
+
+ const message = pass
+ ? () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ formatExpected('Expected substring: not ', expected) +
+ (thrown !== null && thrown.hasMessage
+ ? formatReceived('Received message: ', thrown, 'message', expected) + formatStack(thrown)
+ : formatReceived('Received value: ', thrown, 'value'))
+ : () =>
+ matcherHint(matcherName, undefined, undefined, options) +
+ '\n\n' +
+ formatExpected('Expected substring: ', expected) +
+ (thrown === null
+ ? `\n${DID_NOT_THROW}`
+ : thrown.hasMessage
+ ? formatReceived('Received message: ', thrown, 'message') + formatStack(thrown)
+ : formatReceived('Received value: ', thrown, 'value'));
+
+ return { message, pass };
+};
+
+const toThrow = (
+ matcherName: string,
+ options: MatcherHintOptions,
+ thrown: Thrown | null,
+): SyncExpectationResult => {
+ const pass = thrown !== null;
+
+ const message = pass
+ ? () =>
+ matcherHint(matcherName, undefined, '', options) +
+ '\n\n' +
+ (thrown !== null && thrown.hasMessage
+ ? formatReceived('Error name: ', thrown, 'name') +
+ formatReceived('Error message: ', thrown, 'message') +
+ formatStack(thrown)
+ : formatReceived('Thrown value: ', thrown, 'value'))
+ : () =>
+ matcherHint(matcherName, undefined, '', options) + '\n\n' + DID_NOT_THROW;
+
+ return { message, pass };
+};
+
+const formatExpected = (label: string, expected: unknown) =>
+ `${label + printExpected(expected)}\n`;
+
+const formatReceived = (
+ label: string,
+ thrown: Thrown | null,
+ key: string,
+ expected?: string | RegExp,
+) => {
+ if (thrown === null)
+ return '';
+
+ if (key === 'message') {
+ const message = thrown.message;
+
+ if (typeof expected === 'string') {
+ const index = message.indexOf(expected);
+ if (index !== -1) {
+ return `${
+ label + printReceivedStringContainExpectedSubstring(message, index, expected.length)
+ }\n`;
+ }
+ } else if (expected instanceof RegExp) {
+ return `${
+ label +
+ printReceivedStringContainExpectedResult(
+ message,
+ typeof expected.exec === 'function' ? expected.exec(message) : null,
+ )
+ }\n`;
+ }
+
+ return `${label + printReceived(message)}\n`;
+ }
+
+ if (key === 'name')
+ return thrown.isError ? `${label + printReceived(thrown.value.name)}\n` : '';
+
+ if (key === 'value')
+ return thrown.isError ? '' : `${label + printReceived(thrown.value)}\n`;
+
+ return '';
+};
+
+const formatStack = (thrown: Thrown | null) => {
+ if (thrown === null || !thrown.isError)
+ return '';
+ const config = { rootDir: process.cwd(), testMatch: [] };
+ const options = { noStackTrace: false };
+ if (thrown.value instanceof AggregateError)
+ return formatExecError(thrown.value, config, options);
+ return formatStackTrace(
+ separateMessageFromStack(thrown.value.stack!).stack,
+ config,
+ options,
+ );
+};
+
+function createMessageAndCause(error: Error) {
+ if (error.cause) {
+ const seen = new WeakSet();
+ return JSON.stringify(buildSerializeError(error), (_, value) => {
+ if (isObject(value)) {
+ if (seen.has(value))
+ return;
+ seen.add(value);
+ }
+ if (typeof value === 'bigint' || value === undefined)
+ return String(value);
+ return value;
+ });
+ }
+
+ return error.message;
+}
+
+function buildSerializeError(error: { [key: string]: any }): any {
+ if (!isObject(error))
+ return error;
+
+ const result: { [key: string]: any } = {};
+ for (const name of Object.getOwnPropertyNames(error).sort()) {
+ if (['stack', 'fileName', 'lineNumber'].includes(name))
+ continue;
+ if (name === 'cause') {
+ result[name] = buildSerializeError(error['cause']);
+ continue;
+ }
+ result[name] = error[name];
+ }
+
+ return result;
+}
+
+function isObject(obj: unknown) {
+ return obj !== null && obj !== undefined && typeof obj === 'object';
+}
+
+function messageAndCause(error: Error) {
+ return error.cause === undefined ? 'message' : 'message and cause';
+}
+
+export const getMessage = (message?: () => string) =>
+ (message && message()) ||
+ RECEIVED_COLOR('No message was specified for this matcher.');
+
+export const validateMatcherResult = (result: any) => {
+ if (
+ typeof result !== 'object' ||
+ typeof result.pass !== 'boolean' ||
+ (result.message &&
+ typeof result.message !== 'string' &&
+ typeof result.message !== 'function')
+ ) {
+ throw new Error(
+ 'Unexpected return from a matcher function.\n' +
+ 'Matcher functions should ' +
+ 'return an object in the following format:\n' +
+ ' {message?: string | function, pass: boolean}\n' +
+ `'${stringify(result)}' was returned`,
+ );
+ }
+};
+
+export function createExpectedPromiseMessage(matcherName: string, isNot: boolean, promise: 'resolves' | 'rejects', actual: unknown) {
+ return () => matcherErrorMessage(
+ matcherHint(matcherName, undefined, '', { isNot, promise }),
+ `${RECEIVED_COLOR('received')} value must be a promise or a function returning a promise`,
+ printWithType('Received', actual, printReceived),
+ );
+}
+
+export function createExpectedToResolveMessage(matcherName: string, isNot: boolean, promise: 'resolves' | 'rejects', actual: unknown) {
+ return () =>
+ `${matcherHint(matcherName, undefined, '', { isNot, promise })}\n\n` +
+ 'Received promise rejected instead of resolved\n' +
+ `Rejected to value: ${printReceived(actual)}`;
+}
+
+export function createExpectedToRejectMessage(matcherName: string, isNot: boolean, promise: 'resolves' | 'rejects', actual: unknown) {
+ return () =>
+ `${matcherHint(matcherName, undefined, '', { isNot, promise })}\n\n` +
+ 'Received promise resolved instead of rejected\n' +
+ `Resolved to value: ${printReceived(actual)}`;
+}
diff --git a/packages/playwright/src/matchers/matcherHint.ts b/packages/playwright/src/matchers/matcherHint.ts
index 5f72da4c1c..442b459680 100644
--- a/packages/playwright/src/matchers/matcherHint.ts
+++ b/packages/playwright/src/matchers/matcherHint.ts
@@ -20,45 +20,41 @@ import { stringifyStackFrames } from '@isomorphic/stackTrace';
import type { StackFrame } from '@protocol/channels';
-export type MatcherResult<E, A> = {
- name: string;
+export type MatcherAttachment = { name: string; contentType: string; path?: string; body?: string | Buffer };
+
+export type MatcherResult<E = unknown, A = unknown> = {
+ name?: string;
expected?: E;
message: () => string;
pass: boolean;
actual?: A;
+ diff?: string;
log?: string[];
timeout?: number;
suggestedRebaseline?: string;
+ attachments?: MatcherAttachment[];
+ softError?: Error | unknown;
+ shouldNotRetryTest?: boolean;
};
-export type MatcherResultProperty = Omit<MatcherResult<unknown, unknown>, 'message'> & {
+export type MatcherResultProperty = Omit<MatcherResult, 'message' | 'name' | 'shouldNotRetryTest'> & {
+ name: string;
message: string;
};
-type JestError = Error & {
- matcherResult: MatcherResultProperty;
-};
-
export class ExpectError extends Error {
matcherResult: MatcherResultProperty;
- constructor(jestError: JestError, customMessage: string, stackFrames: StackFrame[]) {
+ constructor(matcherResult: MatcherResultProperty, customMessage: string, stackFrames: StackFrame[]) {
super('');
- // Copy to erase the JestMatcherError constructor name from the console.log(error).
- this.name = jestError.name;
- this.message = jestError.message;
- this.matcherResult = jestError.matcherResult;
-
+ this.message = matcherResult.message;
+ this.matcherResult = matcherResult;
if (customMessage)
this.message = customMessage + '\n\n' + this.message;
this.stack = this.name + ': ' + this.message + '\n' + stringifyStackFrames(stackFrames).join('\n');
}
}
-export function isJestError(e: unknown): e is JestError {
- return e instanceof Error && 'matcherResult' in e && !!e.matcherResult;
-}
-
export function expectTypes(receiver: any, types: ('APIResponse' | 'Page' | 'Locator')[], matcherName: string) {
if (typeof receiver !== 'object' || !types.includes(receiver._apiName)) {
const receiverString = typeof receiver === 'object' && receiver !== null ? `${receiver.constructor.name} ${util.inspect(receiver)}` : String(receiver);
diff --git a/packages/playwright/src/matchers/matchers.ts b/packages/playwright/src/matchers/matchers.ts
index 91c8adef46..86f3191b36 100644
--- a/packages/playwright/src/matchers/matchers.ts
+++ b/packages/playwright/src/matchers/matchers.ts
@@ -33,7 +33,7 @@ import { toHaveScreenshotStepTitle } from './toMatchSnapshot';
import { expectConfig } from './expect';
import type { ExpectMatcherState } from '../../types/test';
-import type { ExpectStepInfo, ExpectTestInfo } from './expect';
+import type { ExpectTestInfo } from './expect';
import type { InternalMatcherUtils } from './matcherHint';
import type { APIResponse, Locator, Frame, Page } from 'playwright-core';
import type { FrameExpectParams } from 'playwright-core/lib/client/types';
@@ -41,7 +41,6 @@ import type { ExpectMatcherUtils } from '../../types/test';
import type { URLPattern } from '@isomorphic/urlMatch';
export type ExpectMatcherStateInternal = Omit<ExpectMatcherState, 'utils'> & {
- _stepInfo?: ExpectStepInfo;
utils: ExpectMatcherUtils & InternalMatcherUtils;
};
diff --git a/packages/playwright/src/matchers/toMatchAriaSnapshot.ts b/packages/playwright/src/matchers/toMatchAriaSnapshot.ts
index 4dcf7dc459..cbdfa0a16c 100644
--- a/packages/playwright/src/matchers/toMatchAriaSnapshot.ts
+++ b/packages/playwright/src/matchers/toMatchAriaSnapshot.ts
@@ -137,23 +137,19 @@ export async function toMatchAriaSnapshot(
const relativePath = path.relative(process.cwd(), expectedPath);
if (updateSnapshots === 'missing') {
const message = `A snapshot doesn't exist at ${relativePath}, writing actual.`;
- testInfo._failWithError(new Error(message), 'shouldNotRetry');
- } else {
- const message = `A snapshot is generated at ${relativePath}.`;
- /* eslint-disable no-console */
- console.log(message);
+ return { pass: true, message: () => '', name: 'toMatchAriaSnapshot', softError: new Error(message), shouldNotRetryTest: true };
}
+ const message = `A snapshot is generated at ${relativePath}.`;
+ /* eslint-disable no-console */
+ console.log(message);
return { pass: true, message: () => '', name: 'toMatchAriaSnapshot' };
} else {
const suggestedRebaseline = `\`\n${escapeTemplateString(indent(typedReceived.regex, '{indent} '))}\n{indent}\``;
if (updateSnapshots === 'missing') {
const message = 'A snapshot is not provided, generating new baseline.';
- testInfo._failWithError(new Error(message), 'shouldNotRetry');
+ return { pass: true, message: () => '', name: 'toMatchAriaSnapshot', suggestedRebaseline, softError: new Error(message), shouldNotRetryTest: true };
}
- // TODO: ideally, we should return "pass: true" here because this matcher passes
- // when regenerating baselines. However, we can only access suggestedRebaseline in case
- // of an error, so we fail here and workaround it in the expect implementation.
- return { pass: false, message: () => '', name: 'toMatchAriaSnapshot', suggestedRebaseline };
+ return { pass: true, message: () => '', name: 'toMatchAriaSnapshot', suggestedRebaseline };
}
}
}
diff --git a/packages/playwright/src/matchers/toMatchSnapshot.ts b/packages/playwright/src/matchers/toMatchSnapshot.ts
index 490f15e1cb..7f89b5166f 100644
--- a/packages/playwright/src/matchers/toMatchSnapshot.ts
+++ b/packages/playwright/src/matchers/toMatchSnapshot.ts
@@ -26,17 +26,15 @@ import { addSuffixToFilePath } from '@utils/fileUtils';
import { callLogText, expectTypes, formatMatcherMessage } from './matcherHint';
import { expectConfig } from './expect';
-import type { MatcherResult } from './matcherHint';
+import type { MatcherAttachment, MatcherResult } from './matcherHint';
import type { ExpectMatcherStateInternal } from './matchers';
-import type { ExpectTestInfo, ExpectStepInfo } from './expect';
+import type { ExpectTestInfo } from './expect';
import type { Locator, Page } from 'playwright-core';
import type { ExpectScreenshotOptions, Page as PageEx } from 'playwright-core/lib/client/page';
import type { Comparator, ImageComparatorOptions } from '@utils/comparators';
type NameOrSegments = string | string[];
-type ImageMatcherResult = MatcherResult<string, string> & { diff?: string };
-
type ToHaveScreenshotConfigOptions = ImageComparatorOptions & {
animations?: 'allow' | 'disabled';
caret?: 'hide' | 'initial';
@@ -151,8 +149,8 @@ class SnapshotHelper {
this.kind = this.mimeType.startsWith('image/') ? 'Screenshot' : 'Snapshot';
}
- createMatcherResult(message: string, pass: boolean, log?: string[]): ImageMatcherResult {
- const unfiltered: ImageMatcherResult = {
+ createMatcherResult(message: string, pass: boolean, log?: string[], attachments?: MatcherAttachment[]): MatcherResult<string, string> {
+ const unfiltered: MatcherResult<string, string> = {
name: this.matcherName,
expected: this.expectedPath,
actual: this.actualPath,
@@ -160,23 +158,24 @@ class SnapshotHelper {
pass,
message: () => message,
log,
+ attachments,
};
- return Object.fromEntries(Object.entries(unfiltered).filter(([_, v]) => v !== undefined)) as ImageMatcherResult;
+ return Object.fromEntries(Object.entries(unfiltered).filter(([_, v]) => v !== undefined)) as MatcherResult<string, string>;
}
- handleMissingNegated(): ImageMatcherResult {
+ handleMissingNegated(): MatcherResult<string, string> {
const isWriteMissingMode = this.updateSnapshots !== 'none';
const message = `A snapshot doesn't exist at ${this.expectedPath}${isWriteMissingMode ? ', matchers using ".not" won\'t write them automatically.' : '.'}`;
// NOTE: 'isNot' matcher implies inversed value.
return this.createMatcherResult(message, true);
}
- handleDifferentNegated(): ImageMatcherResult {
+ handleDifferentNegated(): MatcherResult<string, string> {
// NOTE: 'isNot' matcher implies inversed value.
return this.createMatcherResult('', false);
}
- handleMatchingNegated(): ImageMatcherResult {
+ handleMatchingNegated(): MatcherResult<string, string> {
const message = [
colors.red(`${this.kind} comparison failed:`),
'',
@@ -186,24 +185,28 @@ class SnapshotHelper {
return this.createMatcherResult(message, true);
}
- handleMissing(actual: Buffer | string, step: ExpectStepInfo | undefined): ImageMatcherResult {
+ handleMissing(actual: Buffer | string): MatcherResult<string, string> {
+ const attachments: MatcherAttachment[] = [];
const isWriteMissingMode = this.updateSnapshots !== 'none';
if (isWriteMissingMode)
writeFileSync(this.expectedPath, actual);
- step?._attachToStep({ name: addSuffixToFilePath(this.attachmentBaseName, '-expected'), contentType: this.mimeType, path: this.expectedPath });
+ attachments.push({ name: addSuffixToFilePath(this.attachmentBaseName, '-expected'), contentType: this.mimeType, path: this.expectedPath });
writeFileSync(this.actualPath, actual);
- step?._attachToStep({ name: addSuffixToFilePath(this.attachmentBaseName, '-actual'), contentType: this.mimeType, path: this.actualPath });
+ attachments.push({ name: addSuffixToFilePath(this.attachmentBaseName, '-actual'), contentType: this.mimeType, path: this.actualPath });
const message = `A snapshot doesn't exist at ${this.expectedPath}${isWriteMissingMode ? ', writing actual.' : '.'}`;
if (this.updateSnapshots === 'all' || this.updateSnapshots === 'changed') {
/* eslint-disable no-console */
console.log(message);
- return this.createMatcherResult(message, true);
+ return this.createMatcherResult(message, true, undefined, attachments);
}
if (this.updateSnapshots === 'missing') {
- this.testInfo._failWithError(new Error(message), 'shouldNotRetry');
- return this.createMatcherResult('', true);
+ return {
+ ...this.createMatcherResult('', true, undefined, attachments),
+ softError: new Error(message),
+ shouldNotRetryTest: true,
+ };
}
- return this.createMatcherResult(message, false);
+ return this.createMatcherResult(message, false, undefined, attachments);
}
handleDifferent(
@@ -213,30 +216,30 @@ class SnapshotHelper {
diff: Buffer | string | undefined,
header: string,
diffError: string,
- log: string[] | undefined,
- step: ExpectStepInfo | undefined): ImageMatcherResult {
+ log: string[] | undefined): MatcherResult<string, string> {
const output = [`${header}${indent(diffError, ' ')}`];
if (this.name) {
output.push('');
output.push(` Snapshot: ${this.name}`);
}
+ const attachments: MatcherAttachment[] = [];
if (expected !== undefined) {
// Copy the expectation inside the `test-results/` folder for backwards compatibility,
// so that one can upload `test-results/` directory and have all the data inside.
writeFileSync(this.legacyExpectedPath, expected);
- step?._attachToStep({ name: addSuffixToFilePath(this.attachmentBaseName, '-expected'), contentType: this.mimeType, path: this.expectedPath });
+ attachments.push({ name: addSuffixToFilePath(this.attachmentBaseName, '-expected'), contentType: this.mimeType, path: this.expectedPath });
}
if (previous !== undefined) {
writeFileSync(this.previousPath, previous);
- step?._attachToStep({ name: addSuffixToFilePath(this.attachmentBaseName, '-previous'), contentType: this.mimeType, path: this.previousPath });
+ attachments.push({ name: addSuffixToFilePath(this.attachmentBaseName, '-previous'), contentType: this.mimeType, path: this.previousPath });
}
if (actual !== undefined) {
writeFileSync(this.actualPath, actual);
- step?._attachToStep({ name: addSuffixToFilePath(this.attachmentBaseName, '-actual'), contentType: this.mimeType, path: this.actualPath });
+ attachments.push({ name: addSuffixToFilePath(this.attachmentBaseName, '-actual'), contentType: this.mimeType, path: this.actualPath });
}
if (diff !== undefined) {
writeFileSync(this.diffPath, diff);
- step?._attachToStep({ name: addSuffixToFilePath(this.attachmentBaseName, '-diff'), contentType: this.mimeType, path: this.diffPath });
+ attachments.push({ name: addSuffixToFilePath(this.attachmentBaseName, '-diff'), contentType: this.mimeType, path: this.diffPath });
}
if (log?.length)
@@ -244,10 +247,10 @@ class SnapshotHelper {
else
output.push('');
- return this.createMatcherResult(output.join('\n'), false, log);
+ return this.createMatcherResult(output.join('\n'), false, log, attachments);
}
- handleMatching(): ImageMatcherResult {
+ handleMatching(): MatcherResult<string, string> {
return this.createMatcherResult('', true);
}
}
@@ -280,7 +283,7 @@ export function toMatchSnapshot(
}
if (!fs.existsSync(helper.expectedPath))
- return helper.handleMissing(received, this._stepInfo);
+ return helper.handleMissing(received);
const expected = fs.readFileSync(helper.expectedPath);
@@ -308,7 +311,7 @@ export function toMatchSnapshot(
return helper.handleMatching();
const header = formatMatcherMessage(this.utils, { promise: this.promise, isNot: this.isNot, matcherName: 'toMatchSnapshot', receiver: isString(received) ? 'string' : 'Buffer', expectation: 'expected' });
- return helper.handleDifferent(received, expected, undefined, result.diff, header, result.errorMessage, undefined, this._stepInfo);
+ return helper.handleDifferent(received, expected, undefined, result.diff, header, result.errorMessage, undefined);
}
export function toHaveScreenshotStepTitle(
@@ -393,11 +396,11 @@ export async function toHaveScreenshot(
// This can be due to e.g. spinning animation, so we want to show it as a diff.
if (errorMessage) {
const header = formatMatcherMessage(this.utils, { promise: this.promise, isNot: this.isNot, matcherName: 'toHaveScreenshot', locator: locator?.toString(), expectation: 'expected', timeout, timedOut });
- return helper.handleDifferent(actual, undefined, previous, diff, header, errorMessage, log, this._stepInfo);
+ return helper.handleDifferent(actual, undefined, previous, diff, header, errorMessage, log);
}
// We successfully generated new screenshot.
- return helper.handleMissing(actual!, this._stepInfo);
+ return helper.handleMissing(actual!);
}
// General case:
@@ -429,11 +432,11 @@ export async function toHaveScreenshot(
return writeFiles(actual);
let header = formatMatcherMessage(this.utils, { promise: this.promise, isNot: this.isNot, matcherName: 'toHaveScreenshot', locator: locator?.toString(), expectation: 'expected', timeout, timedOut });
header += ' Failed to re-generate expected.\n';
- return helper.handleDifferent(actual, expectScreenshotOptions.expected, previous, diff, header, errorMessage, log, this._stepInfo);
+ return helper.handleDifferent(actual, expectScreenshotOptions.expected, previous, diff, header, errorMessage, log);
}
const header = formatMatcherMessage(this.utils, { promise: this.promise, isNot: this.isNot, matcherName: 'toHaveScreenshot', locator: locator?.toString(), expectation: 'expected', timeout, timedOut });
- return helper.handleDifferent(actual, expectScreenshotOptions.expected, previous, diff, header, errorMessage, log, this._stepInfo);
+ return helper.handleDifferent(actual, expectScreenshotOptions.expected, previous, diff, header, errorMessage, log);
} finally {
await page.screencast.showOverlays();
}
diff --git a/packages/playwright/src/worker/testInfo.ts b/packages/playwright/src/worker/testInfo.ts
index 9d6bb0e087..35ecb02e96 100644
--- a/packages/playwright/src/worker/testInfo.ts
+++ b/packages/playwright/src/worker/testInfo.ts
@@ -46,7 +46,6 @@ interface TestStepData {
location?: Location;
apiName?: string;
params?: Record<string, any>;
- infectParentStepsWithError?: boolean;
box?: boolean;
// steps with any defined group are hidden from the report
// 'internal' steps are hidden from the trace
@@ -54,7 +53,7 @@ interface TestStepData {
}
export interface TestStepInternal extends TestStepData {
- complete(result: { error?: Error | unknown, suggestedRebaseline?: string }): void;
+ complete(result: { error?: Error | unknown, softError?: Error | unknown, shouldNotRetryTest?: boolean, suggestedRebaseline?: string, attachments?: TestInfo['attachments'] }): void;
info: TestStepInfoImpl;
attachmentIndices: number[];
stepId: string;
@@ -62,6 +61,7 @@ export interface TestStepInternal extends TestStepData {
steps: TestStepInternal[];
endWallTime?: number;
error?: ipc.TestInfoErrorImpl;
+ infectParentStepsWithError?: boolean;
}
type SnapshotNames = {
@@ -318,6 +318,10 @@ export class TestInfoImpl implements TestInfo {
return;
step.endWallTime = Date.now();
+ if (result.attachments) {
+ for (const attachment of result.attachments)
+ this._attach(attachment, stepId);
+ }
if (result.error) {
if (typeof result.error === 'object' && !(result.error as any)?.[stepSymbol])
(result.error as any)[stepSymbol] = step;
@@ -326,6 +330,12 @@ export class TestInfoImpl implements TestInfo {
error.stack = `${error.message}\n${stringifyStackFrames(step.boxedStack).join('\n')}`;
step.error = error;
}
+ if (result.softError) {
+ step.infectParentStepsWithError = true;
+ this._failWithError(result.softError);
+ }
+ if (result.shouldNotRetryTest)
+ this._hasNonRetriableError = true;
if (!step.error) {
// Soft errors inside try/catch will make the test fail.
@@ -403,9 +413,7 @@ export class TestInfoImpl implements TestInfo {
this.status = 'interrupted';
}
- _failWithError(error: Error | unknown, shouldNotRetry?: 'shouldNotRetry') {
- if (shouldNotRetry)
- this._hasNonRetriableError = true;
+ _failWithError(error: Error | unknown) {
if (this.status === 'passed' || this.status === 'skipped')
this.status = error instanceof TimeoutManagerError ? 'timedOut' : 'failed';
const serialized = testInfoError(error);
@@ -694,12 +702,8 @@ export class TestStepInfoImpl implements TestStepInfo {
}
}
- _attachToStep(attachment: TestInfo['attachments'][0]): void {
- this._testInfo._attach(attachment, this._stepId);
- }
-
async attach(name: string, options?: { body?: string | Buffer; contentType?: string; path?: string; }): Promise<void> {
- this._attachToStep(await normalizeAndSaveAttachment(this._testInfo.outputPath(), name, options));
+ this._testInfo._attach(await normalizeAndSaveAttachment(this._testInfo.outputPath(), name, options), this._stepId);
}
get titlePath(): string[] {
diff --git a/tests/installation/bundle-licenses.spec.ts b/tests/installation/bundle-licenses.spec.ts
index bef228b63b..c1a52decfa 100644
--- a/tests/installation/bundle-licenses.spec.ts
+++ b/tests/installation/bundle-licenses.spec.ts
@@ -25,7 +25,7 @@ const EXPECTED: Record<string, Record<string, number>> = {
'lib/utilsBundle.js.LICENSE': 80,
},
'playwright': {
- 'lib/matchers/expect.js.LICENSE': 30,
+ 'lib/matchers/expect.js.LICENSE': 20,
'lib/transform/esmLoader.js.LICENSE': 10,
'lib/transform/babelBundle.js.LICENSE': 65,
},
diff --git a/tests/page/expect-builtins.spec.ts b/tests/page/expect-builtins.spec.ts
new file mode 100644
index 0000000000..a49c8ac348
--- /dev/null
+++ b/tests/page/expect-builtins.spec.ts
@@ -0,0 +1,922 @@
+/**
+ * Copyright (c) Meta Platforms, Inc. and affiliates. All Rights Reserved.
+ * Modifications copyright (c) Microsoft Corporation.
+ *
+ * This source code is licensed under the MIT license found here
+ * https://github.com/jestjs/jest/blob/v30.2.0/LICENSE
+ */
+
+// Adapted from jest/packages/expect/src/__tests__/matchers.test.js,
+// asymmetricMatchers.test.ts and toThrowMatchers.test.ts.
+
+import { test, expect } from './pageTest';
+
+test.describe('.toBe()', () => {
+ test('does not throw', () => {
+ expect('a').not.toBe('b');
+ expect('a').toBe('a');
+ expect(1).not.toBe(2);
+ expect(1).toBe(1);
+ expect(null).not.toBe(undefined);
+ expect(null).toBe(null);
+ expect(undefined).toBe(undefined);
+ expect(NaN).toBe(NaN);
+ expect(BigInt(1)).not.toBe(BigInt(2));
+ expect(BigInt(1)).not.toBe(1);
+ expect(BigInt(1)).toBe(BigInt(1));
+ });
+
+ for (const [i, [a, b]] of [
+ [1, 2],
+ [true, false],
+ [{}, {}],
+ [{ a: 1 }, { a: 1 }],
+ [{ a: 1 }, { a: 5 }],
+ [{ a: undefined, b: 2 }, { b: 2 }],
+ [new Date('2020-02-20'), new Date('2020-02-20')],
+ [/received/, /expected/],
+ [['abc'], ['cde']],
+ [[], []],
+ [null, undefined],
+ [-0, +0],
+ ].entries()) {
+ test(`fails for: ${JSON.stringify(a)} and ${JSON.stringify(b)} #${i}`, () => {
+ expect(() => expect(a).toBe(b)).toThrow();
+ });
+ }
+
+ for (const v of [false, 1, 'a', undefined, null, {}, []]) {
+ test(`fails for '${JSON.stringify(v)}' with '.not'`, () => {
+ expect(() => expect(v).not.toBe(v)).toThrow();
+ });
+ }
+
+ test('does not crash on circular references', () => {
+ const obj: any = {};
+ obj.circular = obj;
+ expect(() => expect(obj).toBe({})).toThrow();
+ });
+
+ test('matcherResult property contains matcher name, expected and actual values', () => {
+ const actual = { a: 1 };
+ const expected = { a: 2 };
+ try {
+ expect(actual).toBe(expected);
+ } catch (error) {
+ expect(error.matcherResult).toEqual(expect.objectContaining({
+ actual,
+ expected,
+ name: 'toBe',
+ }));
+ }
+ });
+});
+
+test.describe('.toStrictEqual()', () => {
+ class TestClassA {
+ a: number;
+ b: number;
+ constructor(a: number, b: number) {
+ this.a = a;
+ this.b = b;
+ }
+ }
+
+ class TestClassB {
+ a: number;
+ b: number;
+ constructor(a: number, b: number) {
+ this.a = a;
+ this.b = b;
+ }
+ }
+
+ test('does not ignore keys with undefined values', () => {
+ expect({ a: undefined, b: 2 }).not.toStrictEqual({ b: 2 });
+ });
+
+ test('does not ignore keys with undefined values inside an array', () => {
+ expect([{ a: undefined }]).not.toStrictEqual([{}]);
+ });
+
+ test('passes when comparing same type', () => {
+ expect({ test: new TestClassA(1, 2) }).toStrictEqual({ test: new TestClassA(1, 2) });
+ });
+
+ test('does not pass for different types', () => {
+ expect({ test: new TestClassA(1, 2) }).not.toStrictEqual({ test: new TestClassB(1, 2) });
+ });
+
+ test('passes for matching buffers', () => {
+ expect(Uint8Array.from([1]).buffer).toStrictEqual(Uint8Array.from([1]).buffer);
+ expect(Uint8Array.from([]).buffer).toStrictEqual(Uint8Array.from([]).buffer);
+ expect(Uint8Array.from([9, 3]).buffer).toStrictEqual(Uint8Array.from([9, 3]).buffer);
+ });
+
+ test('does not pass when ArrayBuffers are not equal', () => {
+ expect(Uint8Array.from([1, 2]).buffer).not.toStrictEqual(Uint8Array.from([0, 0]).buffer);
+ });
+});
+
+test.describe('.toEqual()', () => {
+ for (const [a, b] of [
+ [true, false],
+ [1, 2],
+ [0, Number.MIN_VALUE],
+ [{ a: 1 }, { a: 2 }],
+ ['banana', 'apple'],
+ [null, undefined],
+ [[1], [2]],
+ [new Set([1, 2]), new Set()],
+ [new Set([1, 2]), new Set([1, 2, 3])],
+ [new Map([['a', 0]]), new Map([['b', 0]])],
+ [new Uint8Array([97, 98, 99]), new Uint8Array([97, 98, 100])],
+ [{ a: 1, b: 2 }, expect.objectContaining({ a: 2 })],
+ [[1, 3], expect.arrayContaining([1, 2])],
+ ['abd', expect.stringContaining('bc')],
+ ['abd', expect.stringMatching(/bc/i)],
+ [undefined, expect.anything()],
+ [undefined, expect.any(Function)],
+ ].entries()) {
+ test(`{pass: false} expect().toEqual() #${a}`, () => {
+ expect(() => expect(b[0]).toEqual(b[1])).toThrow();
+ expect(b[0]).not.toEqual(b[1]);
+ });
+ }
+
+ for (const [a, b] of [
+ [true, true],
+ [1, 1],
+ [NaN, NaN],
+ ['abc', 'abc'],
+ [[1], [1]],
+ [{}, {}],
+ [{ a: 99 }, { a: 99 }],
+ [new Set(), new Set()],
+ [new Set([1, 2]), new Set([1, 2])],
+ [new Set([1, 2]), new Set([2, 1])],
+ [new Map(), new Map()],
+ [new Uint8Array([97, 98, 99]), new Uint8Array([97, 98, 99])],
+ [{ a: 1, b: 2 }, expect.objectContaining({ a: 1 })],
+ [[1, 2, 3], expect.arrayContaining([2, 3])],
+ ['abcd', expect.stringContaining('bc')],
+ ['abcd', expect.stringMatching('bc')],
+ [true, expect.anything()],
+ [() => {}, expect.any(Function)],
+ ].entries()) {
+ test(`{pass: true} expect().toEqual() #${a}`, () => {
+ expect(b[0]).toEqual(b[1]);
+ expect(() => expect(b[0]).not.toEqual(b[1])).toThrow();
+ });
+ }
+
+ test('assertion error matcherResult property contains matcher name, expected and actual values', () => {
+ const actual = { a: 1 };
+ const expected = { a: 2 };
+ try {
+ expect(actual).toEqual(expected);
+ } catch (error) {
+ expect(error.matcherResult).toEqual(expect.objectContaining({
+ actual,
+ expected,
+ name: 'toEqual',
+ }));
+ }
+ });
+
+ test('symbol based keys in arrays are processed correctly', () => {
+ const mySymbol = Symbol('test');
+ const actual1: any[] = [];
+ actual1[mySymbol as any] = 3;
+ const actual2: any[] = [];
+ actual2[mySymbol as any] = 4;
+ const expected: any[] = [];
+ expected[mySymbol as any] = 3;
+
+ expect(actual1).toEqual(expected);
+ expect(actual2).not.toEqual(expected);
+ });
+
+ test('non-enumerable members should be skipped during equal', () => {
+ const actual: any = { x: 3 };
+ Object.defineProperty(actual, 'test', { enumerable: false, value: 5 });
+ expect(actual).toEqual({ x: 3 });
+ });
+
+ test('objectContaining sample can be used multiple times', () => {
+ const expected = expect.objectContaining({ b: 7 });
+ expect({ a: 1, b: 2 }).not.toEqual(expected);
+ expect({ a: 3, b: 7 }).toEqual(expected);
+ });
+
+ test.describe('cyclic object equality', () => {
+ test('properties with the same circularity are equal', () => {
+ const a: any = {};
+ a.x = a;
+ const b: any = {};
+ b.x = b;
+ expect(a).toEqual(b);
+ expect(b).toEqual(a);
+ });
+
+ test('properties with different circularity are not equal', () => {
+ const a: any = {};
+ a.x = { y: a };
+ const b: any = {};
+ const bx: any = {};
+ b.x = bx;
+ bx.y = bx;
+ expect(a).not.toEqual(b);
+ expect(b).not.toEqual(a);
+ });
+ });
+});
+
+test.describe('.toBeInstanceOf()', () => {
+ class A {}
+ class B {}
+ class C extends B {}
+ class E extends C {}
+
+ for (const [i, [a, b]] of ([
+ [new Map(), Map],
+ [[], Array],
+ [new A(), A],
+ [new C(), B],
+ [new E(), B],
+ ] as const).entries()) {
+ test(`passing instanceof #${i}`, () => {
+ expect(() => expect(a).not.toBeInstanceOf(b)).toThrow();
+ expect(a).toBeInstanceOf(b);
+ });
+ }
+
+ for (const [i, [a, b]] of ([
+ ['a', String],
+ [1, Number],
+ [true, Boolean],
+ [new A(), B],
+ ] as const).entries()) {
+ test(`failing instanceof #${i}`, () => {
+ expect(() => expect(a).toBeInstanceOf(b)).toThrow();
+ expect(a).not.toBeInstanceOf(b);
+ });
+ }
+
+ test('throws if constructor is not a function', () => {
+ expect(() => expect({}).toBeInstanceOf(4 as any)).toThrow();
+ });
+});
+
+test.describe('.toBeTruthy(), .toBeFalsy()', () => {
+ for (const v of [{}, [], true, 1, 'a', 0.5, new Map(), () => {}, Infinity]) {
+ test(`'${String(v)}' is truthy`, () => {
+ expect(v).toBeTruthy();
+ expect(v).not.toBeFalsy();
+ expect(() => expect(v).not.toBeTruthy()).toThrow();
+ expect(() => expect(v).toBeFalsy()).toThrow();
+ });
+ }
+
+ for (const v of [false, null, NaN, 0, '', undefined]) {
+ test(`'${String(v)}' is falsy`, () => {
+ expect(v).toBeFalsy();
+ expect(v).not.toBeTruthy();
+ expect(() => expect(v).toBeTruthy()).toThrow();
+ expect(() => expect(v).not.toBeFalsy()).toThrow();
+ });
+ }
+});
+
+test.describe('.toBeNaN()', () => {
+ test('{pass: true}', () => {
+ for (const v of [NaN, Math.sqrt(-1), Infinity - Infinity, 0 / 0]) {
+ expect(v).toBeNaN();
+ expect(() => expect(v).not.toBeNaN()).toThrow();
+ }
+ });
+
+ test('throws', () => {
+ for (const v of [1, '', null, undefined, {}, [], 0.2, 0, Infinity, -Infinity]) {
+ expect(() => expect(v).toBeNaN()).toThrow();
+ expect(v).not.toBeNaN();
+ }
+ });
+});
+
+test.describe('.toBeNull()', () => {
+ for (const v of [{}, [], true, 1, 'a', 0.5, new Map(), () => {}, Infinity]) {
+ test(`fails for '${String(v)}'`, () => {
+ expect(v).not.toBeNull();
+ expect(() => expect(v).toBeNull()).toThrow();
+ });
+ }
+
+ test('fails for null with .not', () => {
+ expect(() => expect(null).not.toBeNull()).toThrow();
+ });
+
+ test('pass for null', () => {
+ expect(null).toBeNull();
+ });
+});
+
+test.describe('.toBeDefined(), .toBeUndefined()', () => {
+ for (const v of [{}, [], true, 1, 'a', 0.5, new Map(), () => {}, Infinity]) {
+ test(`'${String(v)}' is defined`, () => {
+ expect(v).toBeDefined();
+ expect(v).not.toBeUndefined();
+ expect(() => expect(v).not.toBeDefined()).toThrow();
+ expect(() => expect(v).toBeUndefined()).toThrow();
+ });
+ }
+
+ test('undefined is undefined', () => {
+ expect(undefined).toBeUndefined();
+ expect(undefined).not.toBeDefined();
+ expect(() => expect(undefined).toBeDefined()).toThrow();
+ expect(() => expect(undefined).not.toBeUndefined()).toThrow();
+ });
+});
+
+test.describe('.toBeGreaterThan(), .toBeLessThan(), .toBeGreaterThanOrEqual(), .toBeLessThanOrEqual()', () => {
+ for (const [small, big] of [
+ [1, 2],
+ [-Infinity, Infinity],
+ [Number.MIN_VALUE, Number.MAX_VALUE],
+ [0.1, 0.2],
+ ] as const) {
+ test(`comparing ${small} and ${big}`, () => {
+ expect(small).toBeLessThan(big);
+ expect(big).not.toBeLessThan(small);
+ expect(big).toBeGreaterThan(small);
+ expect(small).not.toBeGreaterThan(big);
+ expect(small).toBeLessThanOrEqual(big);
+ expect(big).not.toBeLessThanOrEqual(small);
+ expect(big).toBeGreaterThanOrEqual(small);
+ expect(small).not.toBeGreaterThanOrEqual(big);
+
+ expect(() => expect(small).toBeGreaterThan(big)).toThrow();
+ expect(() => expect(small).not.toBeLessThan(big)).toThrow();
+ expect(() => expect(big).not.toBeGreaterThan(small)).toThrow();
+ expect(() => expect(big).toBeLessThan(small)).toThrow();
+ expect(() => expect(small).toBeGreaterThanOrEqual(big)).toThrow();
+ expect(() => expect(small).not.toBeLessThanOrEqual(big)).toThrow();
+ expect(() => expect(big).not.toBeGreaterThanOrEqual(small)).toThrow();
+ expect(() => expect(big).toBeLessThanOrEqual(small)).toThrow();
+ });
+ }
+
+ test('can compare BigInt to Numbers', () => {
+ const a = BigInt(2);
+ expect(a).toBeGreaterThan(1);
+ expect(a).toBeGreaterThanOrEqual(2);
+ expect(2).toBeLessThanOrEqual(a);
+ expect(a).toBeLessThan(3);
+ expect(a).toBeLessThanOrEqual(2);
+ });
+
+ for (const [n1, n2] of [
+ [1, 1],
+ [Number.MIN_VALUE, Number.MIN_VALUE],
+ [Infinity, Infinity],
+ [-Infinity, -Infinity],
+ ]) {
+ test(`equal numbers: [${n1}, ${n2}]`, () => {
+ expect(n1).toBeGreaterThanOrEqual(n2);
+ expect(n1).toBeLessThanOrEqual(n2);
+ expect(() => expect(n1).not.toBeGreaterThanOrEqual(n2)).toThrow();
+ expect(() => expect(n1).not.toBeLessThanOrEqual(n2)).toThrow();
+ });
+ }
+});
+
+test.describe('.toContain(), .toContainEqual()', () => {
+ const typedArray = new Int8Array(2);
+ typedArray[0] = 0;
+ typedArray[1] = 1;
+
+ test('iterable', () => {
+ const iterable = {
+ *[Symbol.iterator]() {
+ yield 1;
+ yield 2;
+ yield 3;
+ },
+ };
+ expect(iterable).toContain(2);
+ expect(iterable).toContainEqual(2);
+ expect(() => expect(iterable).not.toContain(1)).toThrow();
+ expect(() => expect(iterable).not.toContainEqual(1)).toThrow();
+ });
+
+ for (const [i, [list, v]] of ([
+ [[1, 2, 3, 4], 1],
+ [['a', 'b', 'c', 'd'], 'a'],
+ [[undefined, null], null],
+ [[undefined, null], undefined],
+ ['abcdef', 'abc'],
+ ['11112111', '2'],
+ [new Set(['abc', 'def']), 'abc'],
+ [typedArray, 1],
+ ] as const).entries()) {
+ test(`contains #${i}`, () => {
+ expect(list).toContain(v);
+ expect(() => expect(list).not.toContain(v)).toThrow();
+ });
+ }
+
+ for (const [i, [list, v]] of ([
+ [[1, 2, 3], 4],
+ [[null, undefined], 1],
+ [[{}, []], []],
+ [[{}, []], {}],
+ ] as const).entries()) {
+ test(`does not contain #${i}`, () => {
+ expect(list).not.toContain(v);
+ expect(() => expect(list).toContain(v)).toThrow();
+ });
+ }
+
+ test('error cases', () => {
+ expect(() => expect(null).toContain(1)).toThrow();
+ });
+
+ for (const [i, [list, v]] of ([
+ [[1, 2, 3, 4], 1],
+ [['a', 'b', 'c', 'd'], 'a'],
+ [[undefined, null], null],
+ [[{ a: 'b' }, { a: 'c' }], { a: 'b' }],
+ [new Set([1, 2, 3, 4]), 1],
+ ] as const).entries()) {
+ test(`contains a value equal #${i}`, () => {
+ expect(list).toContainEqual(v);
+ expect(() => expect(list).not.toContainEqual(v)).toThrow();
+ });
+ }
+
+ test('error cases for toContainEqual', () => {
+ expect(() => expect(null).toContainEqual(1)).toThrow();
+ });
+});
+
+test.describe('.toBeCloseTo', () => {
+ for (const [n1, n2] of [
+ [0, 0],
+ [0, 0.001],
+ [1.23, 1.229],
+ [1.23, 1.226],
+ [1.23, 1.234],
+ [Infinity, Infinity],
+ [-Infinity, -Infinity],
+ ]) {
+ test(`{pass: true} expect(${n1}).toBeCloseTo(${n2})`, () => {
+ expect(n1).toBeCloseTo(n2);
+ expect(() => expect(n1).not.toBeCloseTo(n2)).toThrow();
+ });
+ }
+
+ for (const [n1, n2] of [
+ [0, 0.01],
+ [1, 1.23],
+ [Infinity, -Infinity],
+ [Infinity, 1.23],
+ [-Infinity, -1.23],
+ ]) {
+ test(`{pass: false} expect(${n1}).toBeCloseTo(${n2})`, () => {
+ expect(n1).not.toBeCloseTo(n2);
+ expect(() => expect(n1).toBeCloseTo(n2)).toThrow();
+ });
+ }
+
+ for (const [n1, n2, p] of [
+ [0, 0.1, 0],
+ [0, 0.0001, 3],
+ [0, 0.000_004, 5],
+ [2.000_000_2, 2, 5],
+ ]) {
+ test(`{pass: true} expect(${n1}).toBeCloseTo(${n2}, ${p})`, () => {
+ expect(n1).toBeCloseTo(n2, p);
+ expect(() => expect(n1).not.toBeCloseTo(n2, p)).toThrow();
+ });
+ }
+});
+
+test.describe('.toMatch()', () => {
+ for (const [n1, n2] of [
+ ['foo', 'foo'],
+ ['Foo bar', /^foo/i],
+ ] as const) {
+ test(`{pass: true} expect(${n1}).toMatch(${n2})`, () => {
+ expect(n1).toMatch(n2);
+ expect(() => expect(n1).not.toMatch(n2)).toThrow();
+ });
+ }
+
+ for (const [n1, n2] of [
+ ['bar', 'foo'],
+ ['bar', /foo/],
+ ] as const) {
+ test(`throws: [${n1}, ${n2}]`, () => {
+ expect(() => expect(n1).toMatch(n2)).toThrow();
+ });
+ }
+
+ for (const [i, [n1, n2]] of ([
+ [1, 'foo'],
+ [{}, 'foo'],
+ [[], 'foo'],
+ [true, 'foo'],
+ [/foo/i, 'foo'],
+ [() => {}, 'foo'],
+ [undefined, 'foo'],
+ ] as const).entries()) {
+ test(`throws if non String actual value passed #${i}`, () => {
+ expect(() => expect(n1).toMatch(n2)).toThrow();
+ });
+ }
+
+ for (const [i, [n1, n2]] of ([
+ ['foo', 1],
+ ['foo', {}],
+ ['foo', []],
+ ['foo', true],
+ ['foo', () => {}],
+ ['foo', undefined],
+ ] as const).entries()) {
+ test(`throws if non String/RegExp expected value passed #${i}`, () => {
+ expect(() => expect(n1).toMatch(n2 as any)).toThrow();
+ });
+ }
+
+ test('escapes strings properly', () => {
+ expect('this?: throws').toMatch('this?: throws');
+ });
+
+ test('does not maintain RegExp state between calls', () => {
+ const regex = /f\d+/gi;
+ expect('f123').toMatch(regex);
+ expect('F456').toMatch(regex);
+ expect(regex.lastIndex).toBe(0);
+ });
+});
+
+test.describe('.toHaveLength', () => {
+ for (const [i, [received, length]] of ([
+ [[1, 2], 2],
+ [[], 0],
+ [['a', 'b'], 2],
+ ['abc', 3],
+ ['', 0],
+ [() => {}, 0],
+ ] as const).entries()) {
+ test(`{pass: true} toHaveLength(${length}) #${i}`, () => {
+ expect(received).toHaveLength(length);
+ expect(() => expect(received).not.toHaveLength(length)).toThrow();
+ });
+ }
+
+ for (const [i, [received, length]] of ([
+ [[1, 2], 3],
+ [[], 1],
+ [['a', 'b'], 99],
+ ['abc', 66],
+ ['', 1],
+ ] as const).entries()) {
+ test(`{pass: false} toHaveLength(${length}) #${i}`, () => {
+ expect(received).not.toHaveLength(length);
+ expect(() => expect(received).toHaveLength(length)).toThrow();
+ });
+ }
+
+ test('error cases', () => {
+ expect(() => expect({ a: 9 }).toHaveLength(1)).toThrow();
+ expect(() => expect(0).toHaveLength(1)).toThrow();
+ expect(() => expect(undefined).not.toHaveLength(1)).toThrow();
+ });
+});
+
+test.describe('.toHaveProperty()', () => {
+ for (const [i, [obj, keyPath, value]] of ([
+ [{ a: { b: { c: { d: 1 } } } }, 'a.b.c.d', 1],
+ [{ a: { b: { c: { d: 1 } } } }, ['a', 'b', 'c', 'd'], 1],
+ [{ 'a.b.c.d': 1 }, ['a.b.c.d'], 1],
+ [{ a: { b: [1, 2, 3] } }, ['a', 'b', 1], 2],
+ [{ a: { b: [1, 2, 3] } }, ['a', 'b', 1], expect.any(Number)],
+ [{ a: 0 }, 'a', 0],
+ [{ a: { b: undefined } }, 'a.b', undefined],
+ [{ a: { b: { c: 5 } } }, 'a.b', { c: 5 }],
+ [{ a: { b: [{ c: [{ d: 1 }] }] } }, 'a.b[0].c[0].d', 1],
+ [{ '': 1 }, '', 1],
+ ] as const).entries()) {
+ test(`{pass: true} toHaveProperty with value #${i}`, () => {
+ expect(obj).toHaveProperty(keyPath as any, value);
+ expect(() => expect(obj).not.toHaveProperty(keyPath as any, value)).toThrow();
+ });
+ }
+
+ for (const [i, [obj, keyPath, value]] of ([
+ [{ a: { b: { c: { d: 1 } } } }, 'a.b.ttt.d', 1],
+ [{ a: { b: { c: { d: 1 } } } }, 'a.b.c.d', 2],
+ [{ 'a.b.c.d': 1 }, 'a.b.c.d', 2],
+ [{ a: { b: { c: {} } } }, 'a.b.c.d', 1],
+ [{ a: 1 }, 'a.b.c.d', 5],
+ [{}, 'a', 'test'],
+ [{ a: { b: 3 } }, 'a.b', undefined],
+ ] as const).entries()) {
+ test(`{pass: false} toHaveProperty with value #${i}`, () => {
+ expect(() => expect(obj).toHaveProperty(keyPath, value)).toThrow();
+ expect(obj).not.toHaveProperty(keyPath, value);
+ });
+ }
+
+ for (const [i, [obj, keyPath]] of ([
+ [{ a: { b: { c: { d: 1 } } } }, 'a.b.c.d'],
+ [{ a: { b: { c: { d: 1 } } } }, ['a', 'b', 'c', 'd']],
+ [{ 'a.b.c.d': 1 }, ['a.b.c.d']],
+ [{ a: 0 }, 'a'],
+ [{ a: { b: undefined } }, 'a.b'],
+ ] as const).entries()) {
+ test(`{pass: true} toHaveProperty without value #${i}`, () => {
+ expect(obj).toHaveProperty(keyPath as any);
+ expect(() => expect(obj).not.toHaveProperty(keyPath as any)).toThrow();
+ });
+ }
+
+ for (const [i, [obj, keyPath]] of ([
+ [{ a: { b: { c: {} } } }, 'a.b.c.d'],
+ [{ a: 1 }, 'a.b.c.d'],
+ [{}, 'a'],
+ ] as const).entries()) {
+ test(`{pass: false} toHaveProperty without value #${i}`, () => {
+ expect(() => expect(obj).toHaveProperty(keyPath)).toThrow();
+ expect(obj).not.toHaveProperty(keyPath);
+ });
+ }
+
+ for (const [i, [obj, keyPath]] of ([
+ [null, 'a.b'],
+ [undefined, 'a'],
+ [{ a: { b: {} } }, undefined],
+ [{ a: { b: {} } }, null],
+ [{ a: { b: {} } }, 1],
+ ] as const).entries()) {
+ test(`{error} toHaveProperty #${i}`, () => {
+ expect(() => expect(obj).toHaveProperty(keyPath as any)).toThrow();
+ });
+ }
+});
+
+test.describe('.toMatchObject()', () => {
+ for (const [i, [a, b]] of ([
+ [{ a: 'b', c: 'd' }, { a: 'b' }],
+ [{ a: 'b', c: 'd' }, { a: 'b', c: 'd' }],
+ [{ a: 'b', t: { x: { r: 'r' }, z: 'z' } }, { a: 'b', t: { z: 'z' } }],
+ [{ a: [3, 4, 5], b: 'b' }, { a: [3, 4, 5] }],
+ [{ a: 1, c: 2 }, { a: expect.any(Number) }],
+ [new Set([1, 2]), new Set([1, 2])],
+ [new Date('2015-11-30'), new Date('2015-11-30')],
+ [{ a: null, b: 'b' }, { a: null }],
+ [{ a: undefined, b: 'b' }, { a: undefined }],
+ [[1, 2], [1, 2]],
+ [new Error('foo'), new Error('foo')],
+ [new Error('bar'), { message: 'bar' }],
+ ] as const).entries()) {
+ test(`{pass: true} toMatchObject #${i}`, () => {
+ expect(a).toMatchObject(b as any);
+ expect(() => expect(a).not.toMatchObject(b as any)).toThrow();
+ });
+ }
+
+ for (const [i, [a, b]] of ([
+ [{ a: 'b', c: 'd' }, { e: 'b' }],
+ [{ a: 'b', c: 'd' }, { a: 'b!', c: 'd' }],
+ [{ a: 'a', c: 'd' }, { a: expect.any(Number) }],
+ [{ a: [3, 4, 5], b: 'b' }, { a: [3, 4, 5, 6] }],
+ [[1, 2], [1, 3]],
+ [new Set([1, 2]), new Set([2])],
+ [new Error('foo'), new Error('bar')],
+ ] as const).entries()) {
+ test(`{pass: false} toMatchObject #${i}`, () => {
+ expect(a).not.toMatchObject(b as any);
+ expect(() => expect(a).toMatchObject(b as any)).toThrow();
+ });
+ }
+
+ for (const [i, [a, b]] of ([
+ [null, {}],
+ [4, {}],
+ ['44', {}],
+ [true, {}],
+ [undefined, {}],
+ [{}, null],
+ [{}, 4],
+ ] as const).entries()) {
+ test(`throws toMatchObject #${i}`, () => {
+ expect(() => expect(a).toMatchObject(b as any)).toThrow();
+ });
+ }
+
+ test('does not match properties up in the prototype chain', () => {
+ const a: any = {};
+ a.ref = a;
+ const b = Object.create(a);
+ b.other = 'child';
+ const matcher: any = { other: 'child' };
+ matcher.ref = matcher;
+ expect(b).not.toMatchObject(matcher);
+ expect(() => expect(b).toMatchObject(matcher)).toThrow();
+ });
+});
+
+test.describe('.toThrow()', () => {
+ class CustomError extends Error {}
+
+ test('to throw or not to throw', () => {
+ expect(() => {
+ throw new CustomError('apple');
+ }).toThrow();
+ expect(() => {}).not.toThrow();
+ });
+
+ test('substring passes', () => {
+ expect(() => {
+ throw new CustomError('apple');
+ }).toThrow('apple');
+ expect(() => {
+ throw new CustomError('banana');
+ }).not.toThrow('apple');
+ expect(() => {}).not.toThrow('apple');
+ });
+
+ test('substring fails when did not throw', () => {
+ expect(() => expect(() => {}).toThrow('apple')).toThrow();
+ });
+
+ test('substring fails when message did not match', () => {
+ expect(() => expect(() => {
+ throw new CustomError('apple');
+ }).toThrow('banana')).toThrow();
+ });
+
+ test('regexp passes', () => {
+ expect(() => {
+ throw new CustomError('apple');
+ }).toThrow(/apple/);
+ expect(() => {
+ throw new CustomError('banana');
+ }).not.toThrow(/apple/);
+ expect(() => {}).not.toThrow(/apple/);
+ });
+
+ test('regexp fails when did not throw', () => {
+ expect(() => expect(() => {}).toThrow(/apple/)).toThrow();
+ });
+
+ test('error class passes', () => {
+ class Err extends CustomError {}
+ class Err2 extends CustomError {}
+
+ expect(() => {
+ throw new Err('apple');
+ }).toThrow(Err);
+ expect(() => {
+ throw new Err('apple');
+ }).toThrow(CustomError);
+
+ expect(() => {
+ throw new Err('apple');
+ }).not.toThrow(Err2);
+ });
+
+ test('object with message passes', () => {
+ expect(() => {
+ throw new CustomError('apple');
+ }).toThrow({ message: 'apple' });
+ expect(() => {
+ throw new CustomError('banana');
+ }).not.toThrow({ message: 'apple' });
+ });
+
+ test('properly escapes strings when matching against errors', () => {
+ expect(() => {
+ throw new TypeError('"this"? throws.');
+ }).toThrow('"this"? throws.');
+ });
+
+ test('error message and cause properties', () => {
+ const errorCause = new Error('cause');
+ const error = new Error('message', { cause: errorCause });
+ expect(() => {
+ throw error;
+ }).toThrow({ message: 'message', cause: errorCause });
+ });
+});
+
+test.describe('asymmetric matchers', () => {
+ test('Any matches primitives', () => {
+ expect('jest').toEqual(expect.any(String));
+ expect(1).toEqual(expect.any(Number));
+ expect(() => {}).toEqual(expect.any(Function));
+ expect(true).toEqual(expect.any(Boolean));
+ expect({}).toEqual(expect.any(Object));
+ expect([]).toEqual(expect.any(Array));
+ });
+
+ test('Any throws when called with empty constructor', () => {
+ expect(() => (expect.any as any)()).toThrow(
+ 'any() expects to be passed a constructor function. Please pass one or use anything() to match any object.');
+ });
+
+ test('Anything matches any type', () => {
+ expect('jest').toEqual(expect.anything());
+ expect(1).toEqual(expect.anything());
+ expect(() => {}).toEqual(expect.anything());
+ expect(true).toEqual(expect.anything());
+ expect({}).toEqual(expect.anything());
+ expect([]).toEqual(expect.anything());
+ });
+
+ test('Anything does not match null and undefined', () => {
+ expect(null).not.toEqual(expect.anything());
+ expect(undefined).not.toEqual(expect.anything());
+ });
+
+ test('ArrayContaining matches', () => {
+ expect(['foo']).toEqual(expect.arrayContaining(['foo']));
+ expect(['foo', 'bar']).toEqual(expect.arrayContaining(['foo']));
+ });
+
+ test('ArrayContaining does not match', () => {
+ expect(['bar']).not.toEqual(expect.arrayContaining(['foo']));
+ });
+
+ test('ObjectContaining matches', () => {
+ expect({ foo: 'foo', jest: 'jest' }).toEqual(expect.objectContaining({ foo: 'foo' }));
+ expect({ foo: undefined }).toEqual(expect.objectContaining({ foo: undefined }));
+ });
+
+ test('ObjectContaining does not match', () => {
+ expect({ bar: 'bar' }).not.toEqual(expect.objectContaining({ foo: 'foo' }));
+ expect({ foo: 'foox' }).not.toEqual(expect.objectContaining({ foo: 'foo' }));
+ expect({}).not.toEqual(expect.objectContaining({ foo: undefined }));
+ });
+
+ test('ObjectContaining throws for non-objects', () => {
+ expect(() => expect({}).toEqual(expect.objectContaining(1337 as any))).toThrow(
+ "You must provide an object to ObjectContaining, not 'number'.");
+ });
+
+ test('StringContaining matches string against string', () => {
+ expect('queen*').toEqual(expect.stringContaining('en*'));
+ expect('queue').not.toEqual(expect.stringContaining('en*'));
+ });
+
+ test('StringMatching matches string against regexp', () => {
+ expect('queen').toEqual(expect.stringMatching(/en/));
+ expect('queue').not.toEqual(expect.stringMatching(/en/));
+ });
+
+ test('StringMatching matches string against string', () => {
+ expect('queen').toEqual(expect.stringMatching('en'));
+ expect('queue').not.toEqual(expect.stringMatching('en'));
+ });
+
+ test('closeTo matches', () => {
+ expect(0).toEqual(expect.closeTo(0));
+ expect(0.001).toEqual(expect.closeTo(0));
+ expect(1.229).toEqual(expect.closeTo(1.23));
+ expect(Infinity).toEqual(expect.closeTo(Infinity));
+ });
+
+ test('closeTo does not match', () => {
+ expect(0.01).not.toEqual(expect.closeTo(0));
+ expect(1.23).not.toEqual(expect.closeTo(1));
+ expect(Infinity).not.toEqual(expect.closeTo(-Infinity));
+ });
+
+ test('closeTo with precision', () => {
+ expect(0.1).toEqual(expect.closeTo(0, 0));
+ expect(0.0001).toEqual(expect.closeTo(0, 3));
+ expect(0.000_004).toEqual(expect.closeTo(0, 5));
+ });
+
+ test('closeTo throws if expected is not number', () => {
+ expect(() => (expect.closeTo as any)('a')).toThrow('Expected is not a Number');
+ });
+
+ test('arrayOf matches', () => {
+ expect([1]).toEqual(expect.arrayOf(1));
+ expect([1, 1, 1]).toEqual(expect.arrayOf(1));
+ expect([{ a: 1 }, { a: 1 }]).toEqual(expect.arrayOf({ a: 1 }));
+ expect(['a', 'b', 'c']).toEqual(expect.arrayOf(expect.any(String)));
+ });
+
+ test('arrayOf does not match', () => {
+ expect([2]).not.toEqual(expect.arrayOf(1));
+ expect([1, 2]).not.toEqual(expect.arrayOf(1));
+ expect('not an array').not.toEqual(expect.arrayOf(1));
+ expect({}).not.toEqual(expect.arrayOf(1));
+ expect([1, 2]).not.toEqual(expect.arrayOf(expect.any(String)));
+ });
+});
diff --git a/tests/page/expect-matcher-result.spec.ts b/tests/page/expect-matcher-result.spec.ts
index d5afc751d1..e4e6f28cdf 100644
--- a/tests/page/expect-matcher-result.spec.ts
+++ b/tests/page/expect-matcher-result.spec.ts
@@ -308,6 +308,7 @@ test('toHaveScreenshot should populate matcherResult', async ({ page, server, is
e.matcherResult.message = stripAnsi(e.matcherResult.message);
expect.soft(e.matcherResult).toEqual({
+ attachments: [expect.any(Object), expect.any(Object), expect.any(Object)],
actual: expect.stringContaining('screenshot-sanity-actual'),
expected: expect.stringContaining('screenshot-sanity-'),
diff: expect.stringContaining('screenshot-sanity-diff'),
diff --git a/tests/playwright-test/expect-poll.spec.ts b/tests/playwright-test/expect-poll.spec.ts
index 208ac4ccce..a90e8c7683 100644
--- a/tests/playwright-test/expect-poll.spec.ts
+++ b/tests/playwright-test/expect-poll.spec.ts
@@ -279,3 +279,29 @@ test('should show custom message', {
expect(result.output).toContain('Expected: 2');
expect(result.output).toContain('Received: 1');
});
+
+test('should swallow matcher errors and predicate errors', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'a.spec.ts': `
+ import { test, expect } from '@playwright/test';
+ test('should poll async predicate', async () => {
+ let i = 0;
+ await test.expect.poll(async () => {
+ if (++i === 2)
+ return 'some foo text';
+ return undefined;
+ }).toContain('foo');
+ });
+ test('should poll async predicate with not', async () => {
+ let i = 0;
+ await test.expect.poll(async () => {
+ if (++i === 2)
+ return 'some bar text';
+ return undefined;
+ }).not.toContain('foo');
+ });
+ `,
+ });
+ expect(result.exitCode).toBe(0);
+ expect(result.passed).toBe(2);
+});
diff --git a/tests/playwright-test/expect.spec.ts b/tests/playwright-test/expect.spec.ts
index 6dd9d06c6e..1bc4540d4d 100644
--- a/tests/playwright-test/expect.spec.ts
+++ b/tests/playwright-test/expect.spec.ts
@@ -412,72 +412,18 @@ test('should return void/Promise when appropriate', async ({ runTSC }) => {
expect(result.exitCode).toBe(0);
});
-test.describe('helpful expect errors', () => {
- test('top-level', async ({ runInlineTest }) => {
- const result = await runInlineTest({
- 'a.spec.ts': `
- import { test, expect } from '@playwright/test';
- test('explodes', () => {
- expect(1).nope();
- });
- `
- });
-
- expect(result.output).toContain(`expect: Property 'nope' not found.`);
- });
-
- test('soft', async ({ runInlineTest }) => {
- const result = await runInlineTest({
- 'a.spec.ts': `
- import { test, expect } from '@playwright/test';
- test('explodes', () => {
- expect.soft(1).nope();
- });
- `
- });
-
- expect(result.output).toContain(`expect: Property 'nope' not found.`);
- });
-
- test('poll', async ({ runInlineTest }) => {
- const result = await runInlineTest({
- 'a.spec.ts': `
- import { test, expect } from '@playwright/test';
- test('explodes', () => {
- expect.poll(() => {}).nope();
- });
- `
- });
-
- expect(result.output).toContain(`expect: Property 'nope' not found.`);
- });
-
- test('not', async ({ runInlineTest }) => {
- const result = await runInlineTest({
- 'a.spec.ts': `
- import { test, expect } from '@playwright/test';
- test('explodes', () => {
- expect(1).not.nope();
- });
- `
- });
-
- expect(result.output).toContain(`expect: Property 'nope' not found.`);
+test('bare expect call should not throw', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'a.spec.ts': `
+ import { test, expect } from '@playwright/test';
+ test('explodes', () => {
+ expect('');
+ });
+ `
});
- test('bare', async ({ runInlineTest }) => {
- const result = await runInlineTest({
- 'a.spec.ts': `
- import { test, expect } from '@playwright/test';
- test('explodes', () => {
- expect('');
- });
- `
- });
-
- expect(result.exitCode).toBe(0);
- expect(result.passed).toBe(1);
- });
+ expect(result.exitCode).toBe(0);
+ expect(result.passed).toBe(1);
});
test('should reasonably work in global setup', async ({ runInlineTest }) => {
diff --git a/tests/playwright-test/playwright.config.ts b/tests/playwright-test/playwright.config.ts
index 71531ffbd2..8cdfd3ab7d 100644
--- a/tests/playwright-test/playwright.config.ts
+++ b/tests/playwright-test/playwright.config.ts
@@ -16,6 +16,7 @@
import { config as loadEnv } from 'dotenv';
loadEnv({ path: path.join(__dirname, '..', '..', '.env') });
+process.env.PWTEST_UNDER_TEST = '1';
import { defineConfig, type ReporterDescription } from './stable-test-runner';
import * as path from 'path';
diff --git a/tests/playwright-test/ui-mode-fixtures.ts b/tests/playwright-test/ui-mode-fixtures.ts
index e345e83c23..e5afcc6b97 100644
--- a/tests/playwright-test/ui-mode-fixtures.ts
+++ b/tests/playwright-test/ui-mode-fixtures.ts
@@ -102,7 +102,6 @@ export const test = base
command: ['node', cliEntrypoint, 'test', (options.useWeb ? '--ui-host=127.0.0.1' : '--ui'), '--workers=1', ...(options.additionalArgs || [])],
env: inheritAndCleanEnv({
...env,
- PWTEST_UNDER_TEST: '1',
PWTEST_CACHE_DIR: cacheDir,
PWTEST_HEADED_FOR_TEST: headless ? '0' : '1',
PWTEST_PRINT_WS_ENDPOINT: '1',