Skip to content

fix: new image prefix starts at -1 #172

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

Merged
merged 1 commit into from
Nov 4, 2022
Merged
Changes from all 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
31 changes: 24 additions & 7 deletions src/screenshotPath.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import {
import sanitize from "sanitize-filename";

const nameCacheCounter: Record<string, number> = {};
const lastRetryNameCacheCounter: Record<string, number> = {};
let lastRetryNumber = 0;

const resetMap = (map: Record<string, unknown>) => {
for (const key in map) delete map[key];
};

export const generateScreenshotPath = ({
titleFromOptions,
Expand Down Expand Up @@ -43,15 +49,24 @@ export const generateScreenshotPath = ({
nameCacheCounter[screenshotPath] = -1;
}

// it's a retry of the same image, so let's decrease the counter
if (currentRetryNumber > 0) {
--nameCacheCounter[screenshotPath];
// it's a retry of last test, so let's reset the counter to value before last retry
if (currentRetryNumber > lastRetryNumber) {
// +1 because we index screenshots starting at 0
for (const screenshotPath in lastRetryNameCacheCounter)
nameCacheCounter[screenshotPath] -=
lastRetryNameCacheCounter[screenshotPath] + 1;
}

resetMap(lastRetryNameCacheCounter);

lastRetryNumber = currentRetryNumber;
lastRetryNameCacheCounter[screenshotPath] = ++nameCacheCounter[
screenshotPath
];

return path.join(
IMAGE_SNAPSHOT_PREFIX,
`${screenshotPath} #${++nameCacheCounter[screenshotPath]}${
FILE_SUFFIX.actual
}.png`
`${screenshotPath} #${nameCacheCounter[screenshotPath]}${FILE_SUFFIX.actual}.png`
);
};

Expand All @@ -71,5 +86,7 @@ export const wasScreenshotUsed = (imagePath: string) => {
};

export const resetScreenshotNameCache = () => {
Object.keys(nameCacheCounter).forEach((k) => delete nameCacheCounter[k]);
lastRetryNumber = 0;
resetMap(nameCacheCounter);
resetMap(lastRetryNameCacheCounter);
};