Skip to content

fix: attempt number is added to screenshot name, causing retries of failures to inappropriately succeed #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ Cypress.Commands.add(
options.title || Cypress.currentTest.titlePath.join(" "),
imagesPath,
specPath: Cypress.spec.relative,
// Although cy.state is not on cypress.d.ts it is documented publicly at
// https://docs.cypress.io/guides/guides/test-retries#Can-I-access-the-current-attempt-counter-from-the-test
attempt: Cypress._.get(
(cy as unknown as { state: (state: string) => unknown }).state(
"runnable"
),
"_currentRetry",
0
),
},
{ log: false }
)
Expand Down
5 changes: 5 additions & 0 deletions src/screenshotPath.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ export const generateScreenshotPath = ({
titleFromOptions,
imagesPath,
specPath,
attempt,
}: {
titleFromOptions: string;
imagesPath: string;
specPath: string;
attempt: number;
}) => {
const parsePathPartVariables = (pathPart: string, i: number) => {
if (pathPart === PATH_VARIABLES.specPath) {
Expand All @@ -39,7 +41,10 @@ export const generateScreenshotPath = ({

if (typeof nameCacheCounter[screenshotPath] === "undefined") {
nameCacheCounter[screenshotPath] = -1;
} else if (attempt > 0) {
nameCacheCounter[screenshotPath] -= 1;
}

return path.join(
IMAGE_SNAPSHOT_PREFIX,
`${screenshotPath} #${++nameCacheCounter[screenshotPath]}${
Expand Down
6 changes: 6 additions & 0 deletions src/task.hook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const fixturesPath = path.resolve(__dirname, "..", "__tests__", "fixtures");
const oldImgFixture = "screenshot.png";
const newImgFixture = "screenshot.actual.png";
const newFileContent = "new file content";
const attempt = 1;

const generateConfig = async (cfg: Partial<CompareImagesCfg>) => ({
updateImages: false,
Expand Down Expand Up @@ -48,6 +49,7 @@ describe("getScreenshotPathInfoTask", () => {
titleFromOptions: "some-title-withśpęćiał人物",
imagesPath: "nested/images/dir",
specPath,
attempt,
})
).toEqual({
screenshotPath:
Expand All @@ -62,6 +64,7 @@ describe("getScreenshotPathInfoTask", () => {
titleFromOptions: "some-title",
imagesPath: "{spec_path}/images/dir",
specPath,
attempt,
})
).toEqual({
screenshotPath:
Expand All @@ -76,6 +79,7 @@ describe("getScreenshotPathInfoTask", () => {
titleFromOptions: "some-title",
imagesPath: "/images/dir",
specPath,
attempt,
})
).toEqual({
screenshotPath:
Expand All @@ -88,6 +92,7 @@ describe("getScreenshotPathInfoTask", () => {
titleFromOptions: "some-title",
imagesPath: "C:/images/dir",
specPath,
attempt,
})
).toEqual({
screenshotPath:
Expand All @@ -104,6 +109,7 @@ describe("cleanupImagesTask", () => {
titleFromOptions: "some-file",
imagesPath: "images",
specPath: "some/spec/path",
attempt,
});
return path.join(
projectRoot,
Expand Down
1 change: 1 addition & 0 deletions src/task.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const getScreenshotPathInfoTask = (cfg: {
titleFromOptions: string;
imagesPath: string;
specPath: string;
attempt: number;
}) => {
const screenshotPath = generateScreenshotPath(cfg);

Expand Down