Skip to content

Commit

Permalink
Merge pull request #203 from chromaui/jmhobbs/cap-1390-allow-ignoring…
Browse files Browse the repository at this point in the history
…-elements-from-test-configuration

Add support for Chromatic ignore parameter.
  • Loading branch information
jmhobbs authored Sep 26, 2024
2 parents cf4e059 + 164f7d4 commit f803208
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/cypress/src/support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ afterEach(() => {
...(Cypress.env('cropToViewport') && {
cropToViewport: Cypress.env('cropToViewport'),
}),
...(Cypress.env('ignore') && { ignore: Cypress.env('ignore') }),
},
pageUrl: url,
viewport: {
Expand Down
2 changes: 1 addition & 1 deletion packages/cypress/tests/cypress/e2e/ignored-regions.cy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// NOTE: This is a test that is meant to be run through Chromatic, so it doesn't actually work
// with the automated test suite.
it('ignored regions work with chromatic', () => {
it('ignored regions work with chromatic', { env: { ignore: ['.custom-ignore'] } }, () => {
cy.visit('/ignore');
cy.wait(1000);
});
3 changes: 3 additions & 0 deletions packages/playwright/src/makeTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const performChromaticSnapshot = async (
resourceArchiveTimeout,
assetDomains,
cropToViewport,
ignore,
}: ChromaticConfig & { page: Page },
use: () => Promise<void>,
testInfo: TestInfo
Expand Down Expand Up @@ -73,6 +74,7 @@ export const performChromaticSnapshot = async (
...(pauseAnimationAtEnd && { pauseAnimationAtEnd }),
...(prefersReducedMotion && { prefersReducedMotion }),
...(cropToViewport && { cropToViewport }),
...(ignore && { ignore }),
};

// TestInfo.outputDir gives us the test-specific subfolder (https://playwright.dev/docs/api/class-testconfig#test-config-output-dir);
Expand Down Expand Up @@ -115,6 +117,7 @@ export const makeTest = (
resourceArchiveTimeout: [DEFAULT_GLOBAL_RESOURCE_ARCHIVE_TIMEOUT_MS, { option: true }],
assetDomains: [[], { option: true }],
cropToViewport: [undefined, { option: true }],
ignore: [undefined, { option: true }],

chromaticSnapshot: [
performChromaticSnapshot,
Expand Down
11 changes: 7 additions & 4 deletions packages/playwright/tests/ignored-regions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { test, expect } from '../src';

// NOTE: This is a test that is meant to be run through Chromatic, so it doesn't actually work
// with the automated test suite.
test('ignored regions work with chromatic', async ({ page }) => {
test.setTimeout(2000);
await page.goto('/ignore');
await setTimeout(() => {}, 1000);
test.describe('ignore regions', () => {
test.use({ ignore: ['.custom-ignore'] });
test('ignored regions work with chromatic', async ({ page }) => {
test.setTimeout(2000);
await page.goto('/ignore');
await setTimeout(() => {}, 1000);
});
});
3 changes: 3 additions & 0 deletions packages/shared/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export interface ChromaticConfig {

// Crop snapshots to the viewport size.
cropToViewport?: boolean;

// CSS selectors of elements to ignore when comparing snapshots.
ignore?: string[];
}

export type ChromaticStorybookParameters = Omit<ChromaticConfig, 'disableAutoSnapshot'>;
4 changes: 3 additions & 1 deletion test-server/fixtures/dynamic-content.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
<h1>Hello There! The current time is:</h1>
<div data-chromatic="ignore" id="time-e2e-test-ignore"></div>
<div class="chromatic-ignore" id="time-e2e-test-ignore-2"></div>
<div class="custom-ignore" id="time-e2e-test-ignore-3"></div>
</div>

<script>
let date = new Date();
document.getElementById('time-e2e-test-ignore').innerText = date.toTimeString();
document.getElementById('time-e2e-test-ignore-2').innerText = date.toTimeString();
document.getElementById('time-e2e-test-ignore-3').innerText = date.toTimeString();
</script>
</body>
</html>
</html>

0 comments on commit f803208

Please sign in to comment.