Skip to content

Commit aea345f

Browse files
committed
blog: improve vercel preview article
1 parent 49feda7 commit aea345f

File tree

2 files changed

+105
-17
lines changed

2 files changed

+105
-17
lines changed

articles/vercel-preview/index.mdx

+105-17
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,114 @@
11
---
2-
title: How to run Argos visual testing on Vercel Preview
3-
description: Guide to run Argos visual testing on Vercel Preview to ensure consistency in production-like environment.
4-
slug: vercel-preview
2+
title: "Guide: Integrating Argos Visual Testing in Vercel Preview"
3+
description: "A guide to setting up Argos integration with Vercel Preview using GitHub Actions."
54
category: Developer Experience
65
author: Jeremy Sfez
76
date: 2024-02-01
87
image: ./main.jpg
98
imageAlt: Vercel covered with Argos
109
---
1110

12-
Great news for Vercel enthusiasts! Argos seamlessly integrates visual testing with Vercel Preview, enabling you to detect visual regressions in a production-like environment effortlessly. This guide walks you through setting it up with GitHub Actions and Playwright, streamlining your workflow.
11+
Argos now seamlessly integrates with Vercel Preview for visual testing, offering a streamlined approach to catch visual regressions in a production-like environment. In this step-by-step guide, I show my journey to run Argos on Vercel Preview via GitHub Actions, and share my source code to do so.
1312

1413
<MainImage />
1514

16-
## Motivation behind this feature
15+
## Why Visual Testing with Vercel Preview? Key Advantages
1716

18-
Responding to user feedback, we recognized the need for visual testing directly on Vercel Previews, especially for environments akin to production settings. This approach is not only cost-effective, eliminating the need for separate test environments, but also enhances test accuracy by mirroring production conditions closely.
17+
- **Reducing CI cost:** Eliminating the need to set up a second environment to run the tests.
18+
- **Increasing test reliability**: Visual testing on a production-like environment.
19+
- **Increasing test reliability**: Visual testing on a production-like environment.
1920

20-
## How to run Argos on Vercel Preview
21+
**Note:** This method is optimal for repeatable tests that don't significantly alter the environment, as certain tests (e.g. those involving user deletion) may not be applicable.
2122

22-
### 1. Set up Argos
23+
## Getting Started with Argos: Setup Guide
2324

24-
Start by following the Q[Quickstart guide](https://argos-ci.com/docs) to integrate Argos with Playwright. This foundational step ensures that Argos is ready to run visual tests during your CI process.
25+
### Prerequisites
2526

26-
### 2. Run tests on your Vercel Preview
27+
Before integrating Argos into Vercel Preview for visual testing, I have:
2728

28-
Adapt your CI workflow with this GitHub Actions configuration to enable Argos visual testing on Vercel Previews:
29+
1. Set up a [Next.js](https://nextjs.org/) project with [Playwright](https://playwright.dev).
30+
2. Integrated Playwright tests into CI/CD.
31+
3. Deployed the project on Vercel.
32+
33+
### 1. Project Importation to Argos
34+
35+
[Signed up into Argos](https://app.argos-ci.com/signup) and import my project. In response, Argos generated an `ARGOS_TOKEN` for my repository that I saved it for later usage.
36+
37+
![Argos GitHub token](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wra1891953mfv4kt6tcs.png)
38+
39+
### 2. Package Installation
40+
41+
```sh
42+
npm install --save-dev @argos-ci/playwright
43+
```
44+
45+
### 3. Setup Argos in Playwright config file
46+
47+
Update the `playwright.config.ts` file:
48+
49+
```ts
50+
import { defineConfig } from "@playwright/test";
51+
52+
export default defineConfig({
53+
reporter: [
54+
process.env.CI ? ["dot"] : ["list"],
55+
[
56+
"@argos-ci/playwright/reporter",
57+
{
58+
uploadToArgos: !!process.env.CI,
59+
token: process.env.ARGOS_TOKEN,
60+
},
61+
],
62+
],
63+
use: {
64+
trace: "on-first-retry",
65+
screenshot: "only-on-failure",
66+
},
67+
});
68+
```
69+
70+
### 4. Screenshot my app
71+
72+
I utilized the [Screenshot pages script](https://argos-ci.com/docs/screenshot-pages-script) from Argos's documentation for capturing my application's homepage. This script is flexible for adding more pages in the future.
73+
74+
```ts
75+
import { argosScreenshot } from "@argos-ci/playwright";
76+
import { test } from "@playwright/test";
77+
78+
const baseUrl = "http://localhost:3000";
79+
const pages = [
80+
{ name: "homepage", path: "/" },
81+
// { name: "faq", path: "/faq" },
82+
// { name: "contact", path: "/contact-us" },
83+
// { name: "pricing", path: "/pricing" },
84+
];
85+
86+
test("screenshot pages", async ({ page }, workerInfo) => {
87+
for (const { name, path } of pages) {
88+
const browserName = workerInfo.project.name;
89+
await page.goto(`${baseUrl}${path}`);
90+
await argosScreenshot(page, `${name}-${browserName}`);
91+
}
92+
});
93+
```
94+
95+
### 5. Set up ARGOS_TOKEN and test integration
96+
97+
First, I set up `ARGOS_TOKEN` as a repository secret on GitHub. Then, I'm ready to push my code to GitHub monitor the status checks to ensure everything is working as expected: Playwright, Vercel, and Argos.
98+
99+
![Github tests with Argos](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/om5itnow41f3koolbwio.png)
100+
101+
## Run Argos on Vercel Preview
102+
103+
To run my tests on Vercel Preview, I updated my CI workflow file:
29104

30105
```yml
31106
name: Playwright + Argos Tests
107+
# 👉 Trigger on deployment event
32108
on: deployment_status:
33109
jobs:
34110
test:
111+
# 👉 Run tests only if the deployment is successful
35112
if: github.event_name == 'deployment_status' && github.event.deployment_status.state == 'success'
36113
runs-on: ubuntu-latest
37114
steps:
@@ -41,33 +118,44 @@ jobs:
41118
run: npm ci && npx playwright install --with-deps
42119
- name: Run Playwright tests
43120
run: npx playwright test
121+
# 👉 Add this variables
44122
env:
45123
BASE_URL: ${{ github.event.deployment_status.environment_url }}
46124
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
47125
ARGOS_BRANCH: ${{ github.event.deployment_status.environment == 'Production' && 'main' || '' }}
48126
```
49127
50-
For a comprehensive explanation of each step, consult the [Argos documentation](https://argos-ci.com/docs/run-on-preview-deployment).
128+
For a comprehensive explanation of each step, read [Argos documentation](https://argos-ci.com/docs/run-on-preview-deployment).
51129
52-
Next, update your Playwright config file is updated to reflect the preview's URL and configure it to ignore the Vercel toolbar for cleaner test results:
130+
Then, I updated my Playwright's config file:
53131
54132
```ts
55133
export default defineConfig({
56134
use: {
57-
# 👉 URL generated for the preview
135+
// URL generated for the preview
58136
baseURL: process.env.BASE_URL,
59137
},
60138
extraHTTPHeaders: {
61-
# 👉 Hide vercel toolbar in tests
139+
// Hide vercel toolbar in tests
62140
"x-vercel-skip-toolbar": "0",
63141
},
64142
});
65143
```
66144

67-
Assuming you have automatic deploys set up, you’ll see the results of your deploy in your pull request. Upon a successful deploy, the E2E workflow will start on the preview.
145+
After my changes are merged, upon a successful deploy, the E2E workflow start on the preview:
68146

69147
![Github pull-request check statuses](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0zu0bwdykzsu9bvfgzw.png)
70148

149+
## Explore the Source Code
150+
151+
Explore my GitHub Repository to read to code source of this integration.
152+
153+
![Github pull-request check statuses](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qhp02ieazr6f38o8q915.png)
154+
155+
- [Github Repository](https://github.com/jsfez/argos-vercel-preview)
156+
- [Pull Request #2 - Execute Argos on Vercel Preview](https://github.com/jsfez/argos-vercel-preview/pull/2)
157+
- [Deployed Project](https://argos-vercel-preview-gimral7da-jeremy-smooth-code.vercel.app/)
158+
71159
## Conclusion
72160

73-
Leveraging Argos with Vercel Previews not only enhances visual consistency in a near-production environment but also optimizes your CI process by eliminating the need for additional environments for testing. This integration streamlines your workflow, ensuring that your projects meet the highest standards of visual quality while being cost-effective.
161+
Vercel Preview and Argos serve complementary roles in ensuring web development excellence. Vercel Preview focuses on verifying expected outcomes, while Argos excels at detecting visual regressions across various viewports. Together, they strengthen the development workflow, allowing developers to harness the best of both tools for unparalleled visual consistency and reliability.

articles/vercel-preview/main.jpg

74.8 KB
Loading

0 commit comments

Comments
 (0)