Skip to content

Commit

Permalink
Merge pull request #70 from chromaui/steven/cypress-command-name
Browse files Browse the repository at this point in the history
Cypress: Manual snapshots
  • Loading branch information
skitterm authored Jan 18, 2024
2 parents c63a77c + 0909035 commit 8edb736
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/cypress/src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { snapshot } from 'rrweb-snapshot';
import type { elementNode } from 'rrweb-snapshot';
// @ts-expect-error will fix when Cypress has its own package
Cypress.Commands.add('takeChromaticArchive', () => {
Cypress.Commands.add('takeSnapshot', (name?: string) => {
cy.document().then((doc) => {
// here, handle the source map
const manualSnapshot = snapshot(doc);
// reassign manualSnapshots so it includes this new snapshot
cy.get('@manualSnapshots')
// @ts-expect-error will fix when Cypress has its own package
.then((snapshots: elementNode[]) => {
return [...snapshots, manualSnapshot];
return [...snapshots, { name, snapshot: manualSnapshot }];
})
.as('manualSnapshots');
});
Expand Down
15 changes: 13 additions & 2 deletions packages/cypress/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ import {
ResourceArchive,
} from '@chromaui/shared-e2e';

interface CypressSnapshot {
// the name of the snapshot (optionally provided for manual snapshots, never provided for automatic snapshots)
name?: string;
// the DOM snapshot
snapshot: elementNode;
}

interface WriteParams {
testTitle: string;
domSnapshots: elementNode[];
domSnapshots: CypressSnapshot[];
chromaticStorybookParams: ChromaticStorybookParameters;
pageUrl: string;
}
Expand Down Expand Up @@ -39,7 +46,11 @@ const writeArchives = async ({
});

const allSnapshots = Object.fromEntries(
domSnapshots.map((item, index) => [`Snapshot #${index + 1}`, Buffer.from(JSON.stringify(item))])
// manual snapshots can be given a name; otherwise, just use the snapshot's place in line as the name
domSnapshots.map(({ name, snapshot }, index) => [
name ?? `Snapshot #${index + 1}`,
Buffer.from(JSON.stringify(snapshot)),
])
);

await writeTestResult(
Expand Down
4 changes: 2 additions & 2 deletions packages/cypress/src/support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ afterEach(() => {
}
// can we be sure this always fires after all the requests are back?
cy.document().then((doc) => {
const snap = snapshot(doc);
const automaticSnapshot = snapshot(doc);
// @ts-expect-error will fix when Cypress has its own package
cy.get('@manualSnapshots').then((manualSnapshots = []) => {
cy.url().then((url) => {
Expand All @@ -28,7 +28,7 @@ afterEach(() => {
action: 'save-archives',
payload: {
testTitle: Cypress.currentTest.title,
domSnapshots: [...manualSnapshots, snap],
domSnapshots: [...manualSnapshots, { snapshot: automaticSnapshot }],
chromaticStorybookParams: {
...(Cypress.env('diffThreshold') && { diffThreshold: Cypress.env('diffThreshold') }),
...(Cypress.env('delay') && { delay: Cypress.env('delay') }),
Expand Down
10 changes: 10 additions & 0 deletions packages/cypress/tests/cypress/e2e/manual-snapshots.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
it('Manual Snapshots / multiple snapshots are taken', () => {
cy.visit('/manual-snapshots');
// manual snapshot with name
cy.takeSnapshot('accordion collapsed');
cy.contains("I'm an accordion, click me!").click();
// manual snapshot without name
cy.takeSnapshot();
cy.contains("I'm an accordion, click me!").click();
cy.get('details').should('not.have.attr', 'open');
});
8 changes: 8 additions & 0 deletions packages/playwright/tests/manual-snapshots.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { test, expect, takeSnapshot } from '../src';

test('Manual snapshots / multiple snapshots are taken', async ({ page }, testInfo) => {
await page.goto('/manual-snapshots');
await takeSnapshot(page, 'accordion collapsed', testInfo);
await page.locator('summary').click();
await expect(page.getByText('I am hiding inside!')).toBeVisible();
});
20 changes: 20 additions & 0 deletions test-server/fixtures/manual-snapshots.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!doctype html>
<html>
<head>
<style>
summary {
font-size: 22px;
}

p {
padding-inline-start: 32px;
}
</style>
</head>
<body>
<details>
<summary>I'm an accordion, click me!</summary>
<p>I am hiding inside!</p>
</details>
</body>
</html>
4 changes: 4 additions & 0 deletions test-server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ app.get('/form-success', (req, res) => {
res.send(`${htmlIntro}<body><p>OK!</p></body>${htmlOutro}`);
});

app.get('/manual-snapshots', (req, res) => {
res.sendFile(path.join(__dirname, 'fixtures/manual-snapshots.html'));
});

app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});

0 comments on commit 8edb736

Please sign in to comment.