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

fix/use-same-name-on-retries #218

Open
wants to merge 1 commit 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
3 changes: 2 additions & 1 deletion cypress/cypress.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
},
"video": false,
"viewportWidth": 660,
"viewportHeight": 1000
"viewportHeight": 1000,
"retries": 1
}
2 changes: 1 addition & 1 deletion cypress/cypress/integration/toMatchImageSnapshot.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
});


it.only('toMatchImageSnapshot - multiple in one test', () => {
it('toMatchImageSnapshot - multiple in one test', () => {
cy.visit('/static/stub.html')
.then(() => {
cy.get('[data-test=test]').toMatchImageSnapshot({
Expand Down
911 changes: 527 additions & 384 deletions cypress/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cypress/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"author": "Meinaart van Straalen",
"license": "MIT",
"dependencies": {
"cypress": "^4.5.0",
"cypress": "^7.3.0",
"cypress-plugin-snapshots": "file:../",
"express": "^4.16.4",
"start-server-and-test": "^1.7.9"
Expand Down
2 changes: 2 additions & 0 deletions src/commands/toMatchImageSnapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ async function toMatchImageSnapshot(subject, commandOptions) {
const options = getImageConfig(commandOptions);
const customName = getCustomName(commandOptions);
const customSeparator = getCustomSeparator(commandOptions);
const attemptNumber=cy.state('runnable')._currentRetry;

const taskData = await getTaskData({
commandName,
options,
customName,
customSeparator,
subject,
attemptNumber
});

const screenShotConfig = getScreenshotConfig(commandOptions);
Expand Down
7 changes: 5 additions & 2 deletions src/utils/commands/getTaskData.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs= require('fs');
const getTestTitle = require('../getTestTitle');
const { getSnapshotTitle } = require('../snapshotTitles');
const getSpec = require('./getSpec');
Expand Down Expand Up @@ -26,14 +27,16 @@ async function getTaskData({
options,
customName,
customSeparator,
subject: testSubject
subject: testSubject,
attemptNumber
} = {}) {

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,attemptNumber);
const subject = subjectIsImage ? testSubject : getSubject(testSubject);
const dataType = getDataType({commandName, subject: testSubject});

Expand Down
14 changes: 12 additions & 2 deletions src/utils/snapshotTitles.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,28 @@ const SNAPSHOTS_IMAGE = {};

const SNAPSHOT_TITLES_TEXT = [];
const SNAPSHOT_TITLES_IMAGE = [];
const SNAPSHOT_TEXT_ATTEMPTS = {};
const SNAPSHOT_IMAGE_ATTEMPTS = {};

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,attemptNumber) {
const name = customName || getTestTitle(test);
const separator = customSeparator || ' #';
const snapshots = isImage ? SNAPSHOTS_IMAGE : SNAPSHOTS_TEXT;
const snapshotsAttempts = isImage ? SNAPSHOT_IMAGE_ATTEMPTS : SNAPSHOT_TEXT_ATTEMPTS;

const prevSnapshotTitle = `${name}${separator}${snapshots[name]||0}`;
if(!snapshotsAttempts[prevSnapshotTitle])snapshotsAttempts[prevSnapshotTitle]=0

if (snapshots[name] !== undefined) {
snapshots[name] += 1;
if(snapshotsAttempts[prevSnapshotTitle] < attemptNumber) {
snapshotsAttempts[prevSnapshotTitle]=attemptNumber
}else{
snapshots[name] += 1;
}
} else {
snapshots[name] = 0;
}
Expand Down