Skip to content

Commit

Permalink
make top level viewports page agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
winkerVSbecks committed Oct 7, 2024
1 parent 9a1a117 commit b49d83d
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 115 deletions.
Binary file modified public/assets/addon-viewports-optimized.mp4
Binary file not shown.
70 changes: 16 additions & 54 deletions src/content/cypress/configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ You can enhance your Cypress and Chromatic tests further by configuring them usi

Cypress can be configured with [Cypress environment variables](https://docs.cypress.io/guides/guides/environment-variables). You can set the available options globally in your Cypress configuration file as follows:


```ts title="cypress.config.js|ts"
export default defineConfig({
env: {
Expand All @@ -29,26 +28,30 @@ You can also override them for specific tests using via the [`env`](https://docs

```ts title="cypress/e2e/HomePage.cy.js|ts"
describe("HomePage", () => {
it("Loads the page with auto snapshotting disabled", {
env: {
// 👇 Overrides the option in the test.
disableAutoSnapshot: true,
it(
"Loads the page with auto snapshotting disabled",
{
env: {
// 👇 Overrides the option in the test.
disableAutoSnapshot: true,
},
},
() => {
cy.visit("/");
},
}, () => {
cy.visit("/");
})
);
});
```

### E2E options

These options control how the Chromatic archive fixture behaves.

| Option | Type | Description |
| ------------------------ | --------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `disableAutoSnapshot` | `boolean` | When `true`, it will disable the snapshot that happens automatically at the end of a test when using the Chromatic test fixture. |
| `resourceArchiveTimeout` | `number` | Maximum amount of time each test will wait for the network to be idle while archiving resources. |
| `assetDomains` | `array[string]` | A list of domains external to the test location that you want Chromatic to also capture assets from, e.g., <code style="display: inline;">['other-domain.com','our-cdn.com']</code>. |
| Option | Type | Description |
| ------------------------ | --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `disableAutoSnapshot` | `boolean` | When `true`, it will disable the snapshot that happens automatically at the end of a test when using the Chromatic test fixture. |
| `resourceArchiveTimeout` | `number` | Maximum amount of time each test will wait for the network to be idle while archiving resources. |
| `assetDomains` | `array[string]` | A list of domains external to the test location that you want Chromatic to also capture assets from, e.g., <code style="display: inline;">['other-domain.com','our-cdn.com']</code>. |

### Chromatic options

Expand All @@ -71,44 +74,3 @@ Some options can be configured through environment variables.
| Environment variable | Description |
| ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `CHROMATIC_ARCHIVE_LOCATION` | If you have configured your project's [`downloadsFolder`](https://docs.cypress.io/guides/references/configuration#Downloads) option to be different than the default, you must set the `CHROMATIC_ARCHIVE_LOCATION` environment variable to the same value. This ensures that the Chromatic can find the archives generated by Cypress tests. |

---

## Viewports

Chromatic will capture the DOM and take snapshots at each viewport size in which a test is configured to run.

[Viewports in Cypress](https://docs.cypress.io/api/commands/viewport) can be configured [globally in your main Cypress configuration file](https://docs.cypress.io/api/commands/viewport#Default-sizing) as follows:

```ts title="cypress.config.js|ts"
import { defineConfig } from "cypress";

export default defineConfig({
viewportWidth: 1000,
viewportHeight: 660,
});
```

Or using [configuration at the test level](https://docs.cypress.io/api/commands/viewport#Set-viewport-in-the-test-configuration):

```ts title="cypress/e2e/Viewports.cy.js|ts"
describe(
"Page display on medium size screen",
{
// 👇 Overrides the option in the test.
viewportHeight: 1000,
viewportWidth: 400,
},
() => {
it("Does not display sidebar", () => {
cy.visit("/");
});
},
);
```

<div class="aside">

ℹ️ Currently, setting the viewport using `cy.viewport()` is not supported.

</div>
2 changes: 1 addition & 1 deletion src/content/modes/legacy-viewports.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: chromatic.viewports feature is now being replaced by the new Modes
sidebar: { order: 5 }
---

# Viewports (legacy) for responsive UIs
# Viewports (legacy Storybook API)

<div class="aside" style="margin-bottom: 2rem;">

Expand Down
18 changes: 4 additions & 14 deletions src/content/modes/viewports.mdx
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
---
layout: "../../layouts/Layout.astro"
title: Viewports
description: Configure Chromatic to test responsive components at various viewports and breakpoints
description: Configure Chromatic in Storybook to test responsive components at various viewports and breakpoints.
sidebar: { order: 2 }
slug: "storybook/viewports"
---

import { TabItem, Tabs } from "../../components/Tabs";

# Viewports for responsive UIs with breakpoints
# Configure viewports for stories

<div class="aside" style="margin-bottom: 2rem;">
<p>ℹ️&nbsp;&nbsp;This page documents viewports using the modes API. Learn how to <a href="/docs/modes">get started</a>.</p>

<p>
If you are transitioning from the <code>chromatic.viewports</code> API to the
modes API, please consult the{" "}
<a href="#migration-from-viewports-legacy-to-modes">migration guide</a>.
</p>
ℹ️&nbsp;&nbsp;This page documents viewports using the modes API. Learn how to [get started](/docs/modes). If you are transitioning from the <code>chromatic.viewports</code> API to the modes API, please consult the [migration guide](#migration-from-viewports-legacy-to-modes).

<p style="margin-bottom: 0;">
If you are using Chromatic with Playwright, please refer to the{" "}
<a href="/docs/playwright/configure#viewports">Playwright documentation</a>.
</p>

<p style="margin-bottom: 0;">If you are using Chromatic with Cypress, please refer to the <a href="/docs/cypress/configure#viewports">Cypress documentation</a>.</p>
</div>

## Define viewport modes
Expand Down
48 changes: 2 additions & 46 deletions src/content/playwright/configure.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ The Chromatic [Playwright Fixture](https://playwright.dev/docs/test-fixtures) ca
// Other project configuration options
});
```

</TabItem>
<TabItem label="JavaScript">
```js title="playwright.config.js"
Expand All @@ -43,6 +44,7 @@ The Chromatic [Playwright Fixture](https://playwright.dev/docs/test-fixtures) ca
// Other project configuration options
});
```

</TabItem>
</Tabs>

Expand Down Expand Up @@ -93,52 +95,6 @@ Some options can be configured through environment variables.

---

## Viewports

Chromatic will capture the DOM and take snapshots at each viewport size in which a test is configured to run.

[Viewports in Playwright](https://playwright.dev/docs/emulation#viewport) can be configured globally in your main Playwright configuration file as follows:

```ts title="playwright.config.js|ts"
import { defineConfig } from "@playwright/test";
export default defineConfig({
projects: [
{
name: "chromium",
use: {
...devices["Desktop Chrome"],
viewport: { width: 1280, height: 720 },
},
},
{
name: "Mobile",
use: {
...devices["Desktop Chrome"],
viewport: { width: 500, height: 600 },
},
},
],
});
```

Or at the test level:

```ts title="Viewports.spec.js|ts"
test.describe("Page display on medium size screen", () => {
// 👇 Overrides the option in the test.
test.use({
viewport: { width: 1600, height: 1200 },
});
test("Does not display sidebar", async ({ page }) => {
await page.goto("/");
});
});
```

---

## Working in Monorepos

Often, when using a monorepo, developers tend to keep their e2e tests in a subdirectory instead of in the root of the project. At the same time, the Storybook and Chromatic configuration details live at the projects root. In these cases, you will need to update the `archive-storybook` and `build-archive-storybook` scripts in your `package.json` by setting the [`-c` flag](https://storybook.js.org/docs/api/cli-options#build) and `CHROMATIC_ARCHIVE_LOCATION` environment variable. For example:
Expand Down
107 changes: 107 additions & 0 deletions src/content/snapshotOptions/viewports.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
layout: "../../layouts/Layout.astro"
title: Viewports
description: Configure Chromatic to test responsive UIs at various viewports and breakpoints.
sidebar: { order: 2 }
---

import { TabItem, Tabs } from "../../components/Tabs";

# Viewports for responsive UIs with breakpoints

UI components respond and adapt to different device widths. Chromatic streamlines the process for visual testing by enabling you to capture each test at multiple viewport sizes.

## Storybook

For Storybook, viewports are configured using the [Modes API](/docs/modes), which allows you to set global configurations that affect how a component renders (e.g., viewport size, theme & locale).

Chromatic also integrates with Storybook's viewport feature. You can specify viewports by the names configured in Storybook's `.storybook/preview.js` file. And if a story has a `defaultViewport` set, Chromatic will automatically use that to capture the snapshot.

For detailed usage instructions, refer to the [**configure viewports for stories**](/docs/storybook/viewports) page.

<video autoPlay muted playsInline loop width="600px" class="center">
<source src="/docs/assets/addon-viewports-optimized.mp4" type="video/mp4" />
</video>

## Playwright

Chromatic will capture the DOM and take snapshots at the viewport size in which a test is configured to run.

[Viewports in Playwright](https://playwright.dev/docs/emulation#viewport) can be configured globally in your main Playwright configuration file as follows:

```ts title="playwright.config.js|ts"
import { defineConfig } from "@playwright/test";

export default defineConfig({
projects: [
{
name: "chromium",
use: {
...devices["Desktop Chrome"],
viewport: { width: 1280, height: 720 },
},
},
{
name: "Mobile",
use: {
...devices["Desktop Chrome"],
viewport: { width: 500, height: 600 },
},
},
],
});
```

Or at the test level:

```ts title="Viewports.spec.js|ts"
test.describe("Page display on medium size screen", () => {
// 👇 Overrides the option in the test.
test.use({
viewport: { width: 1600, height: 1200 },
});

test("Does not display sidebar", async ({ page }) => {
await page.goto("/");
});
});
```

## Cypress

Chromatic will capture the DOM and take snapshots at the viewport size in which a test is configured to run.

[Viewports in Cypress](https://docs.cypress.io/api/commands/viewport) can be configured [globally in your main Cypress configuration file](https://docs.cypress.io/api/commands/viewport#Default-sizing) as follows:

```ts title="cypress.config.js|ts"
import { defineConfig } from "cypress";

export default defineConfig({
viewportWidth: 1000,
viewportHeight: 660,
});
```

Or using [configuration at the test level](https://docs.cypress.io/api/commands/viewport#Set-viewport-in-the-test-configuration):

```ts title="cypress/e2e/Viewports.cy.js|ts"
describe(
"Page display on medium size screen",
{
// 👇 Overrides the option in the test.
viewportHeight: 1000,
viewportWidth: 400,
},
() => {
it("Does not display sidebar", () => {
cy.visit("/");
});
},
);
```

<div class="aside">

ℹ️ Currently, setting the viewport using `cy.viewport()` is not supported.

</div>

0 comments on commit b49d83d

Please sign in to comment.