diff --git a/src/commands.ts b/src/commands.ts index d189443c..effadbef 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -130,6 +130,12 @@ Cypress.Commands.add( matchAgainstPath, } = getConfig(options); + const currentRetryNumber = ( + cy as unknown as { state: (s: string) => { currentRetry: () => number } } + ) + .state("test") + .currentRetry(); + return cy .then(() => cy.task<{ screenshotPath: string; title: string }>( @@ -139,6 +145,7 @@ Cypress.Commands.add( options.title || Cypress.currentTest.titlePath.join(" "), imagesPath, specPath: Cypress.spec.relative, + currentRetryNumber, }, { log: false } ) diff --git a/src/screenshotPath.utils.ts b/src/screenshotPath.utils.ts index f308aed4..f0c33e85 100644 --- a/src/screenshotPath.utils.ts +++ b/src/screenshotPath.utils.ts @@ -13,10 +13,12 @@ export const generateScreenshotPath = ({ titleFromOptions, imagesPath, specPath, + currentRetryNumber, }: { titleFromOptions: string; imagesPath: string; specPath: string; + currentRetryNumber: number; }) => { const parsePathPartVariables = (pathPart: string, i: number) => { if (pathPart === PATH_VARIABLES.specPath) { @@ -40,6 +42,11 @@ export const generateScreenshotPath = ({ if (typeof nameCacheCounter[screenshotPath] === "undefined") { nameCacheCounter[screenshotPath] = -1; } + + // it's a retry of the same image, so let's decrease the counter + if (currentRetryNumber > 0) { + --nameCacheCounter[screenshotPath]; + } return path.join( IMAGE_SNAPSHOT_PREFIX, `${screenshotPath} #${++nameCacheCounter[screenshotPath]}${ diff --git a/src/task.hook.ts b/src/task.hook.ts index c974795a..64f4ccba 100644 --- a/src/task.hook.ts +++ b/src/task.hook.ts @@ -38,6 +38,7 @@ export const getScreenshotPathInfoTask = (cfg: { titleFromOptions: string; imagesPath: string; specPath: string; + currentRetryNumber: number; }) => { const screenshotPath = generateScreenshotPath(cfg);