diff --git a/packages/playwright/src/matchers/toMatchSnapshot.ts b/packages/playwright/src/matchers/toMatchSnapshot.ts index 449c7349f0..3b315202ca 100644 --- a/packages/playwright/src/matchers/toMatchSnapshot.ts +++ b/packages/playwright/src/matchers/toMatchSnapshot.ts @@ -101,9 +101,9 @@ class SnapshotHelper { name = nameOrOptions; this.options = { ...optOptions }; } else { - name = nameOrOptions.name; - this.options = { ...nameOrOptions }; - delete (this.options as any).name; + const { name: nameFromOptions, ...options } = nameOrOptions; + this.options = options; + name = nameFromOptions; } let snapshotNames = (testInfo as any)[snapshotNamesSymbol] as SnapshotNames; @@ -121,25 +121,32 @@ class SnapshotHelper { // // noop // expect.toMatchSnapshot('a.png') - let actualModifier = ''; + let inputPathSegments: NameOrSegments | undefined; if (!name) { const fullTitleWithoutSpec = [ ...testInfo.titlePath.slice(1), ++snapshotNames.anonymousSnapshotIndex, ].join(' '); name = sanitizeForFilePath(trimLongString(fullTitleWithoutSpec)) + '.' + anonymousSnapshotExtension; + inputPathSegments = [name]; this.snapshotName = name; } else { + // We never sanitize the name here, because it's a user-provided value. + inputPathSegments = Array.isArray(name) ? name : [name]; const joinedName = Array.isArray(name) ? name.join(path.sep) : name; snapshotNames.namedSnapshotIndex[joinedName] = (snapshotNames.namedSnapshotIndex[joinedName] || 0) + 1; const index = snapshotNames.namedSnapshotIndex[joinedName]; - if (index > 1) { - actualModifier = `-${index - 1}`; + if (index > 1) this.snapshotName = `${joinedName}-${index - 1}`; - } else { + else this.snapshotName = joinedName; - } } + this.snapshotPath = testInfo.snapshotPath(...inputPathSegments); + this.legacyExpectedPath = addSuffixToFilePath(testInfo._getOutputPath(...inputPathSegments), '-expected'); + const outputFile = testInfo._getOutputPath(this.snapshotName); + this.previousPath = addSuffixToFilePath(outputFile, '-previous'); + this.actualPath = addSuffixToFilePath(outputFile, '-actual'); + this.diffPath = addSuffixToFilePath(outputFile, '-diff'); const filteredConfigOptions = { ...configOptions }; for (const prop of NonConfigProperties) @@ -161,16 +168,6 @@ class SnapshotHelper { if (this.options.maxDiffPixelRatio !== undefined && (this.options.maxDiffPixelRatio < 0 || this.options.maxDiffPixelRatio > 1)) throw new Error('`maxDiffPixelRatio` option value must be between 0 and 1'); - // sanitizes path if string - const inputPathSegments = Array.isArray(name) ? name : [addSuffixToFilePath(name, '', undefined, true)]; - const outputPathSegments = Array.isArray(name) ? name : [addSuffixToFilePath(name, actualModifier, undefined, true)]; - this.snapshotPath = testInfo.snapshotPath(...inputPathSegments); - const inputFile = testInfo._getOutputPath(...inputPathSegments); - const outputFile = testInfo._getOutputPath(...outputPathSegments); - this.legacyExpectedPath = addSuffixToFilePath(inputFile, '-expected'); - this.previousPath = addSuffixToFilePath(outputFile, '-previous'); - this.actualPath = addSuffixToFilePath(outputFile, '-actual'); - this.diffPath = addSuffixToFilePath(outputFile, '-diff'); this.matcherName = matcherName; this.locator = locator;