Skip to content

Commit 8279208

Browse files
authored
fix: new image prefix starts at -1 (#172)
handle case when there are multiple failed test retries
1 parent eac95fa commit 8279208

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/screenshotPath.utils.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ import {
88
import sanitize from "sanitize-filename";
99

1010
const nameCacheCounter: Record<string, number> = {};
11+
const lastRetryNameCacheCounter: Record<string, number> = {};
12+
let lastRetryNumber = 0;
13+
14+
const resetMap = (map: Record<string, unknown>) => {
15+
for (const key in map) delete map[key];
16+
};
1117

1218
export const generateScreenshotPath = ({
1319
titleFromOptions,
@@ -43,15 +49,24 @@ export const generateScreenshotPath = ({
4349
nameCacheCounter[screenshotPath] = -1;
4450
}
4551

46-
// it's a retry of the same image, so let's decrease the counter
47-
if (currentRetryNumber > 0) {
48-
--nameCacheCounter[screenshotPath];
52+
// it's a retry of last test, so let's reset the counter to value before last retry
53+
if (currentRetryNumber > lastRetryNumber) {
54+
// +1 because we index screenshots starting at 0
55+
for (const screenshotPath in lastRetryNameCacheCounter)
56+
nameCacheCounter[screenshotPath] -=
57+
lastRetryNameCacheCounter[screenshotPath] + 1;
4958
}
59+
60+
resetMap(lastRetryNameCacheCounter);
61+
62+
lastRetryNumber = currentRetryNumber;
63+
lastRetryNameCacheCounter[screenshotPath] = ++nameCacheCounter[
64+
screenshotPath
65+
];
66+
5067
return path.join(
5168
IMAGE_SNAPSHOT_PREFIX,
52-
`${screenshotPath} #${++nameCacheCounter[screenshotPath]}${
53-
FILE_SUFFIX.actual
54-
}.png`
69+
`${screenshotPath} #${nameCacheCounter[screenshotPath]}${FILE_SUFFIX.actual}.png`
5570
);
5671
};
5772

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

7388
export const resetScreenshotNameCache = () => {
74-
Object.keys(nameCacheCounter).forEach((k) => delete nameCacheCounter[k]);
89+
lastRetryNumber = 0;
90+
resetMap(nameCacheCounter);
91+
resetMap(lastRetryNameCacheCounter);
7592
};

0 commit comments

Comments
 (0)