address comments

This commit is contained in:
Yury Semikhatsky 2024-10-21 10:31:15 -07:00
parent 2d88d7a5af
commit 7a3253e89d
8 changed files with 24 additions and 28 deletions

View file

@ -22,18 +22,18 @@ Receiver's locator.
Call log. Call log.
## property: TestError.message
* since: v1.10
- type: ?<[string]>
Error message. Set when [Error] (or its subclass) has been thrown.
## property: TestError.matcherName ## property: TestError.matcherName
* since: v1.49 * since: v1.49
- type: ?<[string]> - type: ?<[string]>
Expect matcher name. Expect matcher name.
## property: TestError.message
* since: v1.10
- type: ?<[string]>
Error message. Set when [Error] (or its subclass) has been thrown.
## property: TestError.received ## property: TestError.received
* since: v1.49 * since: v1.49
- type: ?<[string]> - type: ?<[string]>
@ -50,7 +50,7 @@ Error stack. Set when [Error] (or its subclass) has been thrown.
* since: v1.49 * since: v1.49
- type: ?<[int]> - type: ?<[int]>
Timeout in milliseconds, if the error was caused by a timeout.. Timeout in milliseconds, if the error was caused by a timeout.
## property: TestError.value ## property: TestError.value
* since: v1.10 * since: v1.10

View file

@ -39,20 +39,21 @@ export async function toBeTruthy(
}; };
const timeout = options.timeout ?? this.timeout; const timeout = options.timeout ?? this.timeout;
const { matches, log, timedOut, received } = await query(!!this.isNot, timeout); const { matches: pass, log, timedOut, received } = await query(!!this.isNot, timeout);
if (matches === !this.isNot) { if (pass === !this.isNot) {
return { return {
name: matcherName, name: matcherName,
message: () => '', message: () => '',
pass: matches, pass,
expected expected
}; };
} }
const notFound = received === kNoElementsFoundError ? received : undefined; const notFound = received === kNoElementsFoundError ? received : undefined;
const actual = matches ? expected : unexpected; const actual = pass ? expected : unexpected;
let printedReceived: string | undefined; let printedReceived: string | undefined;
let printedExpected: string | undefined; let printedExpected: string | undefined;
if (matches) { if (pass) {
printedExpected = `Expected: not ${expected}`; printedExpected = `Expected: not ${expected}`;
printedReceived = `Received: ${notFound ? kNoElementsFoundError : expected}`; printedReceived = `Received: ${notFound ? kNoElementsFoundError : expected}`;
} else { } else {
@ -66,7 +67,7 @@ export async function toBeTruthy(
}; };
return { return {
message, message,
pass: matches, pass,
actual, actual,
name: matcherName, name: matcherName,
expected, expected,

View file

@ -56,11 +56,9 @@ export async function toEqual<T>(
let printedReceived: string | undefined; let printedReceived: string | undefined;
let printedExpected: string | undefined; let printedExpected: string | undefined;
let printedDiff: string | undefined; let printedDiff: string | undefined;
if (pass) { if (pass) {
printedExpected = `Expected: not ${this.utils.printExpected(expected)}`; printedExpected = `Expected: not ${this.utils.printExpected(expected)}`;
printedReceived = `Received: ${this.utils.printReceived(received)}`; printedReceived = `Received: ${this.utils.printReceived(received)}`;
} else { } else {
printedDiff = this.utils.printDiffOrStringify( printedDiff = this.utils.printDiffOrStringify(
expected, expected,
@ -69,14 +67,12 @@ export async function toEqual<T>(
RECEIVED_LABEL, RECEIVED_LABEL,
false, false,
); );
} }
const message = () => { const message = () => {
const header = matcherHint(this, receiver, matcherName, 'locator', undefined, matcherOptions, timedOut ? timeout : undefined); const header = matcherHint(this, receiver, matcherName, 'locator', undefined, matcherOptions, timedOut ? timeout : undefined);
const details = printedDiff || `${printedExpected}\n${printedReceived}`; const details = printedDiff || `${printedExpected}\n${printedReceived}`;
return `${header}${details}${callLogText(log)}`; return `${header}${details}${callLogText(log)}`;
} };
// Passing the actual and expected objects so that a custom reporter // Passing the actual and expected objects so that a custom reporter
// could access them, for example in order to display a custom visual diff, // could access them, for example in order to display a custom visual diff,
// or create a different error message // or create a different error message

View file

@ -186,7 +186,6 @@ class SnapshotHelper {
} }
createMatcherResult(message: string, pass: boolean, log?: string[]): ImageMatcherResult { createMatcherResult(message: string, pass: boolean, log?: string[]): ImageMatcherResult {
const unfiltered: ImageMatcherResult = { const unfiltered: ImageMatcherResult = {
name: this.matcherName, name: this.matcherName,
expected: this.expectedPath, expected: this.expectedPath,
@ -195,6 +194,7 @@ class SnapshotHelper {
pass, pass,
message: () => message, message: () => message,
log, log,
// eslint-disable-next-line @typescript-eslint/no-base-to-string
...(this.locator ? { locator: this.locator.toString() } : {}), ...(this.locator ? { locator: this.locator.toString() } : {}),
printedExpected: this.expectedPath, printedExpected: this.expectedPath,
printedReceived: this.actualPath, printedReceived: this.actualPath,

View file

@ -66,6 +66,7 @@ export async function toMatchText(
expected expected
}; };
} }
const stringSubstring = options.matchSubstring ? 'substring' : 'string'; const stringSubstring = options.matchSubstring ? 'substring' : 'string';
const receivedString = received || ''; const receivedString = received || '';
const messagePrefix = matcherHint(this, receiver, matcherName, 'locator', undefined, matcherOptions, timedOut ? timeout : undefined); const messagePrefix = matcherHint(this, receiver, matcherName, 'locator', undefined, matcherOptions, timedOut ? timeout : undefined);
@ -117,6 +118,7 @@ export async function toMatchText(
actual: received, actual: received,
log, log,
timeout: timedOut ? timeout : undefined, timeout: timedOut ? timeout : undefined,
// eslint-disable-next-line @typescript-eslint/no-base-to-string
locator: receiver.toString(), locator: receiver.toString(),
...(printedReceived ? { printedReceived } : {}), ...(printedReceived ? { printedReceived } : {}),
...(printedExpected ? { printedExpected } : {}), ...(printedExpected ? { printedExpected } : {}),

View file

@ -32,9 +32,6 @@ type Annotation = {
type ErrorDetails = { type ErrorDetails = {
message: string; message: string;
location?: Location; location?: Location;
};
type TestResultErrorDetails = ErrorDetails & {
timeout?: number; timeout?: number;
matcherName?: string; matcherName?: string;
locator?: string; locator?: string;
@ -374,8 +371,8 @@ function quotePathIfNeeded(path: string): string {
return path; return path;
} }
export function formatResultFailure(test: TestCase, result: TestResult, initialIndent: string, highlightCode: boolean): TestResultErrorDetails[] { export function formatResultFailure(test: TestCase, result: TestResult, initialIndent: string, highlightCode: boolean): ErrorDetails[] {
const errorDetails: TestResultErrorDetails[] = []; const errorDetails: ErrorDetails[] = [];
if (result.status === 'passed' && test.expectedStatus === 'failed') { if (result.status === 'passed' && test.expectedStatus === 'failed') {
errorDetails.push({ errorDetails.push({

View file

@ -600,7 +600,7 @@ export interface TestError {
stack?: string; stack?: string;
/** /**
* Timeout in milliseconds, if the error was caused by a timeout.. * Timeout in milliseconds, if the error was caused by a timeout.
*/ */
timeout?: number; timeout?: number;

View file

@ -31,7 +31,7 @@ test('toMatchText-based assertions should have matcher result', async ({ page })
message: expect.stringContaining(`Timed out 1ms waiting for expect(locator).toHaveText(expected)`), message: expect.stringContaining(`Timed out 1ms waiting for expect(locator).toHaveText(expected)`),
name: 'toHaveText', name: 'toHaveText',
pass: false, pass: false,
locator:`locator('#node')`, locator: `locator('#node')`,
printedDiff: `Expected pattern: /Text2/ printedDiff: `Expected pattern: /Text2/
Received string: \"Text content\"`, Received string: \"Text content\"`,
log: expect.any(Array), log: expect.any(Array),
@ -58,7 +58,7 @@ Call log`);
message: expect.stringContaining(`Timed out 1ms waiting for expect(locator).not.toHaveText(expected)`), message: expect.stringContaining(`Timed out 1ms waiting for expect(locator).not.toHaveText(expected)`),
name: 'toHaveText', name: 'toHaveText',
pass: true, pass: true,
locator:`locator('#node')`, locator: `locator('#node')`,
printedExpected: 'Expected pattern: not /Text/', printedExpected: 'Expected pattern: not /Text/',
printedReceived: `Received string: \"Text content\"`, printedReceived: `Received string: \"Text content\"`,
log: expect.any(Array), log: expect.any(Array),