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: support Cypress v5.0 (test-retries) #141

Open
wants to merge 9 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 27 additions & 31 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: test

on: [push]
Expand All @@ -14,32 +11,31 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x]

cypress-version: ["current", 4.12.1, 3.4.1]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Cache Node.js modules
uses: actions/cache@v1
with:
path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.OS }}-node-
${{ runner.OS }}-
- name: npm install, build, and test
run: |
npm ci
npm --prefix cypress ci
npm run lint &
npm run ci:test
kill $(jobs -p) || true
- name: Archive Cypress snapshots
uses: actions/upload-artifact@v1
if: failure()
with:
name: cypress-snapshots
path: cypress/cypress/integration/__*snapshots__/*
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: "12.x"
- name: Cache Node.js modules
uses: actions/cache@v1
with:
path: ~/.npm # npm cache files are stored in `~/.npm` on Linux/macOS
key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.OS }}-node-
${{ runner.OS }}-
- name: npm install, build, and test
run: |
npm ci
npm --prefix cypress ci
bash -c "[[ ${{ matrix.cypress-version }} == 'current' ]] || npm --prefix cypress install --no-save cypress@${{ matrix.cypress-version }}"
npm run lint &
npm run ci:test
kill $(jobs -p) || true
- name: Archive Cypress snapshots
uses: actions/upload-artifact@v1
if: failure()
with:
name: cypress-snapshots
path: cypress/cypress/integration/__*snapshots__/*
6 changes: 6 additions & 0 deletions cypress/cypress/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rules": {
"spaced-comment": "off",
"func-names": "off"
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions cypress/cypress/integration/__snapshots__/retries.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
exports[`cypress retries > textSnapshot > fails after snapshot #0`] =
{
"foo": true
};

exports[`cypress retries > textSnapshot > fails before snapshot #0`] =
{
"foo": true
};

exports[`cypress retries > textSnapshot > fails between snapshots #0`] =
{
"foo": true
};

exports[`cypress retries > textSnapshot > fails between snapshots #1`] =
{
"foo": true
};

exports[`cypress retries > textSnapshot > retries after snapshot #0`] =
{
"foo": true
};

exports[`cypress retries > textSnapshot > retries before snapshot #0`] =
{
"foo": true
};

exports[`cypress retries > textSnapshot > retries between snapshots #0`] =
{
"foo": true
};

exports[`cypress retries > textSnapshot > retries between snapshots #1`] =
{
"foo": true
};
77 changes: 77 additions & 0 deletions cypress/cypress/integration/retries.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/// <reference types="cypress" />

const {
failBeforeRetry,
verifySnapshotsExclude,
verifySnapshots,
} = require('../support/testUtils');

if (+Cypress.version.slice(0, 1) < 5) {
describe = function() {
it('nothing to test, Cypress version < 5.0.0');
};
}

describe('cypress retries', { retries: 2 }, () => {
describe('textSnapshot', () => {
it('retries after snapshot', () => {
cy.wrap({ foo: true }).toMatchSnapshot();
failBeforeRetry(2);
verifySnapshots(['#0']);
verifySnapshotsExclude(['#1']);
});

it('retries before snapshot', () => {
failBeforeRetry(2);
cy.wrap({ foo: true }).toMatchSnapshot();
verifySnapshots(['#0']);
verifySnapshotsExclude(['#1']);
});

it('retries between snapshots', () => {
cy.wrap({ foo: true }).toMatchSnapshot();
failBeforeRetry(2);
cy.wrap({ foo: true }).toMatchSnapshot();
verifySnapshots(['#0', '#1']);
verifySnapshotsExclude(['#2']);
});
});
describe('imageSnapshot', () => {
it('retries after snapshot', () => {
cy.visit('/static/stub.html');
failBeforeRetry(2);
cy.document().toMatchImageSnapshot({
threshold: 0.1,
});
failBeforeRetry(2);
});

it('retries before snapshot', () => {
cy.visit('/static/stub.html');
cy.document().toMatchImageSnapshot({
threshold: 0.1,
});
failBeforeRetry(2);
});

it('retries between snapshots', () => {
cy.visit('/static/stub.html');
cy.document().toMatchImageSnapshot({
threshold: 0.1,
});
failBeforeRetry(2);
cy.document().toMatchImageSnapshot({
threshold: 0.1,
});
});

it('retries with named snapshots', () => {
cy.visit('/static/stub.html');
cy.document().toMatchImageSnapshot({
threshold: 0.1,
name: 'foo',
});
failBeforeRetry(2);
});
});
});
35 changes: 35 additions & 0 deletions cypress/cypress/support/testUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { _ } = Cypress;

export const getSnapshotName = () => {
const specPath = Cypress.spec.relative;
const lastIndex = specPath.lastIndexOf('/');
const specDir = specPath.slice(0, lastIndex);
const specBasename = specPath.slice(lastIndex + 1);
return `${specDir}/__snapshots__/${specBasename}.snap`;
};

export const getTitlePath = () =>
cy
.state('test')
.titlePath()
.join(' > ');

export const failBeforeRetry = n =>
cy.then(() => expect(cy.state('test').currentRetry(), 'currentRetry').eq(n));

export function verifySnapshots(arr, include) {
include = include != null ? include : true;
const titlePath = getTitlePath();
cy.readFile(getSnapshotName()).then(snapshotContents => {
arr.forEach(item => {
const match = item.startsWith('#') ? `${titlePath} ${item}` : item;
if (include) {
expect(snapshotContents).includes(match);
return;
}
expect(snapshotContents).not.includes(match);
});
});
}

export const verifySnapshotsExclude = _.curryRight(verifySnapshots)(false);