Skip to content

Instantly share code, notes, and snippets.

@trescenzi
Created June 21, 2019 18:01
Show Gist options
  • Save trescenzi/4a8d8d980a78004672cc90e306122ce8 to your computer and use it in GitHub Desktop.
Save trescenzi/4a8d8d980a78004672cc90e306122ce8 to your computer and use it in GitHub Desktop.
Writing a file upon test fail.
describe('test, () => {
it('should do the same thing it did last time', () => {
const snapshot = cy.readFile('./cypress/snapshots/snapshot.html');
let bodyHtml = '';
cy.visit('/home');
cy.get('body').then(b => bodyHtml = b.innerHTML);
cy.on('fail', () => {
// This throws a "You can't promise inside a promise"
// https://on.cypress.io/returning-promise-and-commands-in-another-command
cy.writeFile('./cypress/snapshots/snapshot.html', bodyHtml);
});
cy.on('test:after:run', () => {
// This never runs
cy.writeFile('./cypress/snapshots/snapshot.html', bodyHtml);
});
cy.wrap(null).should(() => {
expect(bodyHtml).to.be(snapshot);
});
})
})
describe('test, () => {
it('should do the same thing it did last time', () => {
const snapshot = cy.readFile('./cypress/snapshots/snapshot.html');
let bodyHtml = '';
cy.visit('/home');
cy.get('body').then(b => bodyHtml = b.innerHTML);
cy.on('test:after:run', () => {
// This never runs
cy.writeFile('./cypress/snapshots/snapshot.html', bodyHtml);
});
cy.wrap(null).should(() => {
expect(bodyHtml).to.be(snapshot);
});
})
})
describe('test, () => {
it('should do the same thing it did last time', () => {
const snapshot = cy.readFile('./cypress/snapshots/snapshot.html');
let bodyHtml = '';
cy.visit('/home');
cy.get('body').then(b => bodyHtml = b.innerHTML);
cy.wrap(null).should(() => {
expect(bodyHtml).to.be(snapshot);
}).then(() => {
// This never runs
cy.writeFile('./cypress/snapshots/snapshot.html', bodyHtml);
});
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment