Skip to content
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

Add retry-ability #169

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ Add the configuration below to your `cypress.json` file to make changes to the d
"serverPort": 2121, // Port number for "update snapshot server"
"updateSnapshots": false, // Automatically update snapshots, useful if you have lots of changes
"backgroundBlend": "difference" // background-blend-mode for diff image, useful to switch to "overlay"
"retryCount": 0 // Amount of retries if snapshot comparison fails
"retryDelay": 200 // Delay time between snapshot retries
}
}
```
Expand Down
16 changes: 14 additions & 2 deletions src/commands/toMatchImageSnapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function afterScreenshot(taskData) {
};
}

async function toMatchImageSnapshot(subject, commandOptions) {
async function toMatchImageSnapshot(subject, commandOptions, isRetry = false) {
const options = getImageConfig(commandOptions);
const customName = getCustomName(commandOptions);
const customSeparator = getCustomSeparator(commandOptions);
Expand All @@ -30,6 +30,7 @@ async function toMatchImageSnapshot(subject, commandOptions) {
customName,
customSeparator,
subject,
isRetry,
});

const screenShotConfig = getScreenshotConfig(commandOptions);
Expand All @@ -50,7 +51,18 @@ async function toMatchImageSnapshot(subject, commandOptions) {
MATCH_IMAGE,
taskData,
NO_LOG
).then(logMessage)
).then((result) => {
if (!result.passed && commandOptions.retryCount > 0) {
return cy.wait(commandOptions.retryDelay).then(() => {
const newCommandOptions = {
...commandOptions,
retryCount: commandOptions.retryCount - 1,
};
return toMatchImageSnapshot(subject, newCommandOptions, true);
});
}
return logMessage(result);
})
);
}

Expand Down
16 changes: 14 additions & 2 deletions src/commands/toMatchSnapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,28 @@ const logMessage = require('../utils/commands/logMessage');
const { NO_LOG } = require('../constants');
const { COMMAND_MATCH_SNAPSHOT: commandName } = require('./commandNames');

function toMatchSnapshot(subject, options) {
function toMatchSnapshot(subject, options, isRetry = false) {
return getTaskData({
commandName,
options,
subject,
isRetry,
}).then(taskData => cy.task(
MATCH_TEXT,
taskData,
NO_LOG
).then(logMessage)
).then((result) => {
if (!result.passed && options.retryCount > 0) {
return cy.wait(options.retryDelay).then(() => {
const newOptions = {
...options,
retryCount: options.retryCount - 1,
};
return toMatchSnapshot(subject, newOptions, true);
});
}
return logMessage(result);
})
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const DEFAULT_CONFIG = Object.freeze({
updateSnapshots: false,
backgroundBlend: 'difference',
name: '',
retryCount: 0,
retryDelay: 200,
});

const CONFIG_KEY = 'cypress-plugin-snapshots';
Expand Down
5 changes: 3 additions & 2 deletions src/utils/commands/getTaskData.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ async function getTaskData({
options,
customName,
customSeparator,
subject: testSubject
subject: testSubject,
isRetry,
} = {}) {
const subjectIsImage = isImage(commandName);
const test = getTestForTask();
const testTitle = getTestTitle(test);
const spec = await getSpec();
const testFile = spec.absolute;
const snapshotTitle = getSnapshotTitle(test, customName, customSeparator, subjectIsImage);
const snapshotTitle = getSnapshotTitle(test, customName, customSeparator, subjectIsImage, isRetry);
const subject = subjectIsImage ? testSubject : getSubject(testSubject);
const dataType = getDataType({commandName, subject: testSubject});

Expand Down
12 changes: 7 additions & 5 deletions src/utils/snapshotTitles.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ function snapshotTitleIsUsed(snapshotTitle, isImage = false) {
return (isImage ? SNAPSHOT_TITLES_IMAGE : SNAPSHOT_TITLES_TEXT).indexOf(snapshotTitle) !== -1;
}

function getSnapshotTitle(test, customName, customSeparator, isImage = false) {
function getSnapshotTitle(test, customName, customSeparator, isImage = false, isRetry = false) {
const name = customName || getTestTitle(test);
const separator = customSeparator || ' #';
const snapshots = isImage ? SNAPSHOTS_IMAGE : SNAPSHOTS_TEXT;

if (snapshots[name] !== undefined) {
snapshots[name] += 1;
} else {
snapshots[name] = 0;
if (!isRetry) {
if (snapshots[name] !== undefined) {
snapshots[name] += 1;
} else {
snapshots[name] = 0;
}
}

const snapshotTitle = `${name}${separator}${snapshots[name]}`;
Expand Down