chore: call testInfo.snapshotPath directly (#29755)

Reference https://github.com/microsoft/playwright/issues/29719
This commit is contained in:
Yury Semikhatsky 2024-02-29 16:59:39 -08:00 committed by GitHub
parent 989cf8f179
commit c08a4e72d1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -22,7 +22,7 @@ import { getComparator, sanitizeForFilePath, zones } from 'playwright-core/lib/u
import { import {
addSuffixToFilePath, addSuffixToFilePath,
trimLongString, callLogText, trimLongString, callLogText,
expectTypes } from '../util'; expectTypes } from '../util';
import { colors } from 'playwright-core/lib/utilsBundle'; import { colors } from 'playwright-core/lib/utilsBundle';
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
@ -91,7 +91,6 @@ class SnapshotHelper {
testInfo: TestInfoImpl, testInfo: TestInfoImpl,
matcherName: string, matcherName: string,
locator: Locator | undefined, locator: Locator | undefined,
snapshotPathResolver: (...pathSegments: string[]) => string,
anonymousSnapshotExtension: string, anonymousSnapshotExtension: string,
configOptions: ToHaveScreenshotConfigOptions, configOptions: ToHaveScreenshotConfigOptions,
nameOrOptions: NameOrSegments | { name?: NameOrSegments } & ToHaveScreenshotOptions, nameOrOptions: NameOrSegments | { name?: NameOrSegments } & ToHaveScreenshotOptions,
@ -165,7 +164,7 @@ class SnapshotHelper {
// sanitizes path if string // sanitizes path if string
const inputPathSegments = Array.isArray(name) ? name : [addSuffixToFilePath(name, '', undefined, true)]; const inputPathSegments = Array.isArray(name) ? name : [addSuffixToFilePath(name, '', undefined, true)];
const outputPathSegments = Array.isArray(name) ? name : [addSuffixToFilePath(name, actualModifier, undefined, true)]; const outputPathSegments = Array.isArray(name) ? name : [addSuffixToFilePath(name, actualModifier, undefined, true)];
this.snapshotPath = snapshotPathResolver(...inputPathSegments); this.snapshotPath = testInfo.snapshotPath(...inputPathSegments);
const inputFile = testInfo._getOutputPath(...inputPathSegments); const inputFile = testInfo._getOutputPath(...inputPathSegments);
const outputFile = testInfo._getOutputPath(...outputPathSegments); const outputFile = testInfo._getOutputPath(...outputPathSegments);
this.legacyExpectedPath = addSuffixToFilePath(inputFile, '-expected'); this.legacyExpectedPath = addSuffixToFilePath(inputFile, '-expected');
@ -304,10 +303,10 @@ export function toMatchSnapshot(
if (testInfo._configInternal.ignoreSnapshots) if (testInfo._configInternal.ignoreSnapshots)
return { pass: !this.isNot, message: () => '', name: 'toMatchSnapshot', expected: nameOrOptions }; return { pass: !this.isNot, message: () => '', name: 'toMatchSnapshot', expected: nameOrOptions };
const configOptions = testInfo._projectInternal.expect?.toMatchSnapshot || {};
const helper = new SnapshotHelper( const helper = new SnapshotHelper(
testInfo, 'toMatchSnapshot', undefined, testInfo.snapshotPath.bind(testInfo), determineFileExtension(received), testInfo, 'toMatchSnapshot', undefined, determineFileExtension(received),
testInfo._projectInternal.expect?.toMatchSnapshot || {}, configOptions, nameOrOptions, optOptions);
nameOrOptions, optOptions);
if (this.isNot) { if (this.isNot) {
if (!fs.existsSync(helper.snapshotPath)) if (!fs.existsSync(helper.snapshotPath))
@ -362,10 +361,7 @@ export async function toHaveScreenshot(
expectTypes(pageOrLocator, ['Page', 'Locator'], 'toHaveScreenshot'); expectTypes(pageOrLocator, ['Page', 'Locator'], 'toHaveScreenshot');
const [page, locator] = pageOrLocator.constructor.name === 'Page' ? [(pageOrLocator as PageEx), undefined] : [(pageOrLocator as Locator).page() as PageEx, pageOrLocator as Locator]; const [page, locator] = pageOrLocator.constructor.name === 'Page' ? [(pageOrLocator as PageEx), undefined] : [(pageOrLocator as Locator).page() as PageEx, pageOrLocator as Locator];
const configOptions = testInfo._projectInternal.expect?.toHaveScreenshot || {}; const configOptions = testInfo._projectInternal.expect?.toHaveScreenshot || {};
const snapshotPathResolver = testInfo.snapshotPath.bind(testInfo); const helper = new SnapshotHelper(testInfo, 'toHaveScreenshot', locator, 'png', configOptions, nameOrOptions, optOptions);
const helper = new SnapshotHelper(
testInfo, 'toHaveScreenshot', locator, snapshotPathResolver, 'png',
configOptions, nameOrOptions, optOptions);
if (!helper.snapshotPath.toLowerCase().endsWith('.png')) if (!helper.snapshotPath.toLowerCase().endsWith('.png'))
throw new Error(`Screenshot name "${path.basename(helper.snapshotPath)}" must have '.png' extension`); throw new Error(`Screenshot name "${path.basename(helper.snapshotPath)}" must have '.png' extension`);
expectTypes(pageOrLocator, ['Page', 'Locator'], 'toHaveScreenshot'); expectTypes(pageOrLocator, ['Page', 'Locator'], 'toHaveScreenshot');