Skip to content

Commit 247a20a

Browse files
authored
feat: next example (#254)
1 parent 7034940 commit 247a20a

31 files changed

+1062
-62
lines changed

.eslintrc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = {
1818
parser: "@typescript-eslint/parser",
1919
parserOptions: {
2020
sourceType: "module",
21-
project: ["./packages/cypress-plugin-visual-regression-diff/tsconfig.json", "./examples/webpack/tsconfig.json"],
21+
project: ["./packages/cypress-plugin-visual-regression-diff/tsconfig.json", "./examples/next/tsconfig.json", "./examples/webpack/tsconfig.json"],
2222
tsconfigRootDir: __dirname,
2323
warnOnUnsupportedTypeScriptVersion: false,
2424
EXPERIMENTAL_useSourceOfProjectReferenceRedirect: true,

.github/workflows/ci.yml

+85-11
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,33 @@ jobs:
1111
cache:
1212
name: CI cache
1313
runs-on: ubuntu-latest
14+
outputs:
15+
pnpm-cache: ${{ steps.vars.outputs.pnpm-cache }}
16+
key: ${{ steps.vars.outputs.key }}
1417
steps:
1518
- uses: actions/checkout@v3
1619
- uses: pnpm/[email protected]
1720
- uses: actions/setup-node@v3
1821
with:
1922
node-version: "18.x"
20-
cache: 'pnpm'
21-
- run: pnpm install
23+
- id: vars
24+
run: |
25+
echo "pnpm-cache=$(pnpm store path --silent)" >> $GITHUB_OUTPUT
26+
echo "key=${{ runner.os }}-pnpm-${{ hashFiles('pnpm-lock.yaml') }}" >> $GITHUB_OUTPUT
27+
- name: Cache NPM and Cypress 📦
28+
uses: actions/cache@v3
29+
id: cache
30+
with:
31+
path: |
32+
${{ steps.vars.outputs.pnpm-cache }}
33+
~/.cache/Cypress
34+
key: ${{ steps.vars.outputs.key }}
35+
restore-keys: |
36+
${{ runner.os }}-pnpm-${{ hashFiles('pnpm-lock.yaml') }}
37+
${{ runner.os }}-pnpm-
38+
- run: pnpm install --frozen-lockfile --prefer-offline
39+
env:
40+
HUSKY: '0' # By default do not run HUSKY install
2241

2342
lint:
2443
name: lint
@@ -30,8 +49,20 @@ jobs:
3049
- uses: actions/setup-node@v3
3150
with:
3251
node-version: "18.x"
33-
cache: 'pnpm'
34-
- run: pnpm install
52+
- name: Cache NPM and Cypress 📦
53+
uses: actions/cache@v3
54+
id: cache
55+
with:
56+
path: |
57+
${{ needs.cache.outputs.pnpm-cache }}
58+
~/.cache/Cypress
59+
key: ${{ needs.cache.outputs.key }}
60+
restore-keys: |
61+
${{ runner.os }}-pnpm-${{ hashFiles('pnpm-lock.yaml') }}
62+
${{ runner.os }}-pnpm-
63+
- run: pnpm install --frozen-lockfile --prefer-offline
64+
env:
65+
HUSKY: '0' # By default do not run HUSKY install
3566
- run: pnpm lint:ci
3667
- run: pnpm format:ci
3768

@@ -45,13 +76,32 @@ jobs:
4576
- uses: actions/setup-node@v3
4677
with:
4778
node-version: "18.x"
48-
cache: 'pnpm'
49-
- run: pnpm install && cd ./examples/webpack && pnpm cypress install
79+
- name: Cache NPM and Cypress 📦
80+
uses: actions/cache@v3
81+
id: cache
82+
with:
83+
path: |
84+
${{ needs.cache.outputs.pnpm-cache }}
85+
~/.cache/Cypress
86+
key: ${{ needs.cache.outputs.key }}
87+
restore-keys: |
88+
${{ runner.os }}-pnpm-${{ hashFiles('pnpm-lock.yaml') }}
89+
${{ runner.os }}-pnpm-
90+
- run: pnpm install --frozen-lockfile --prefer-offline
91+
env:
92+
HUSKY: '0' # By default do not run HUSKY install
5093
- run: pnpm --filter cypress-plugin-visual-regression-diff -r build
5194
- name: Test e2e
5295
run: pnpm test:e2e:ci
5396
- name: Test component-testing
5497
run: pnpm test:ct:ci
98+
- uses: actions/upload-artifact@v3
99+
with:
100+
name: test
101+
path: |
102+
examples/next/**/*.png
103+
examples/next/**/*.jpg
104+
examples/next/**/*.jpeg
55105
56106
test-integration-coverage:
57107
name: test-integration-coverage
@@ -63,8 +113,20 @@ jobs:
63113
- uses: actions/setup-node@v3
64114
with:
65115
node-version: "18.x"
66-
cache: 'pnpm'
67-
- run: pnpm install
116+
- name: Cache NPM and Cypress 📦
117+
uses: actions/cache@v3
118+
id: cache
119+
with:
120+
path: |
121+
${{ needs.cache.outputs.pnpm-cache }}
122+
~/.cache/Cypress
123+
key: ${{ needs.cache.outputs.key }}
124+
restore-keys: |
125+
${{ runner.os }}-pnpm-${{ hashFiles('pnpm-lock.yaml') }}
126+
${{ runner.os }}-pnpm-
127+
- run: pnpm install --frozen-lockfile --prefer-offline
128+
env:
129+
HUSKY: '0' # By default do not run HUSKY install
68130
- name: Test integration (with coverage) and upload to CodeClimate
69131
if: ${{ github.ref == 'refs/heads/main' }}
70132
uses: paambaati/[email protected]
@@ -100,18 +162,30 @@ jobs:
100162
build-and-release:
101163
name: build and release
102164
runs-on: ubuntu-latest
103-
needs: prepare-release
165+
needs: [cache, prepare-release]
104166
if: needs.prepare-release.outputs.releases_created
105167
steps:
106168
- uses: actions/checkout@v3
107169
- uses: pnpm/[email protected]
108170
- uses: actions/setup-node@v3
109171
with:
110172
node-version: "18.x"
111-
cache: 'pnpm'
112173
registry-url: 'https://registry.npmjs.org'
113174
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
114-
- run: pnpm install
175+
- name: Cache NPM and Cypress 📦
176+
uses: actions/cache@v3
177+
id: cache
178+
with:
179+
path: |
180+
${{ needs.cache.outputs.pnpm-cache }}
181+
~/.cache/Cypress
182+
key: ${{ needs.cache.outputs.key }}
183+
restore-keys: |
184+
${{ runner.os }}-pnpm-${{ hashFiles('pnpm-lock.yaml') }}
185+
${{ runner.os }}-pnpm-
186+
- run: pnpm install --frozen-lockfile --prefer-offline
187+
env:
188+
HUSKY: '0' # By default do not run HUSKY install
115189
- name: Build packages to get cross-references working 🔧
116190
run: pnpm build
117191
- name: Release package

CONTRIBUTING.md

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ After cloning the repository, run:
5050

5151
```bash
5252
pnpm i # installs the project dependencies
53-
cd examples/webpack && pnpm i # install dependencies for example project (useful for testing)
5453
```
5554

5655
### Committing Changes

examples/next/.gitignore

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# we do not want to commit local screenshot files
12+
# as they might be different on different OSes
13+
**/__image_snapshots_local__
14+
15+
# next.js
16+
/.next/
17+
/out/
18+
19+
# production
20+
/build
21+
22+
# misc
23+
.DS_Store
24+
*.pem
25+
26+
# debug
27+
npm-debug.log*
28+
yarn-debug.log*
29+
yarn-error.log*
30+
31+
# local env files
32+
.env*.local
33+
34+
# vercel
35+
.vercel
36+
37+
# typescript
38+
*.tsbuildinfo
39+
next-env.d.ts
40+
41+
# cypress
42+
/cypress/screenshots
43+
/cypress/videos

examples/next/README.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Next.js + Cypress + @frsource/cypress-plugin-visual-regression-diff
2+
3+
This example shows how to configure @frsource/cypress-plugin-visual-regression-diff to work with Cypress & Next.js.
4+
5+
## Project setup
6+
7+
```bash
8+
pnpm install
9+
```
10+
11+
### Run end-to-end tests
12+
13+
> Important - remember to run `pnpm && pnpm build` command in this repo's root directory before starting e2e tests.
14+
15+
```bash
16+
pnpm e2e
17+
```
18+
19+
### Run component tests
20+
21+
> Important - remember to run `pnpm && pnpm build` command in this repo's root directory before starting e2e tests.
22+
23+
```bash
24+
pnpm component
25+
```
26+
27+
### Compiles and hot-reloads for development
28+
29+
```bash
30+
pnpm dev
31+
```
32+
33+
### Compiles and minifies for production
34+
35+
```bash
36+
pnpm build
37+
```
38+
39+
## Credits
40+
41+
Created using [Next.js Cypress template](https://nextjs.org/docs/pages/building-your-application/optimizing/testing#cypress).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import AboutComponent from './about-component'
2+
// Disable ESLint to prevent failing linting inside the Next.js repo.
3+
// If you're using ESLint on your project, we recommend installing the ESLint Cypress plugin instead:
4+
// https://github.com/cypress-io/eslint-plugin-cypress
5+
6+
describe('<AboutComponent />', () => {
7+
it('should render and display expected content', () => {
8+
cy.mount(<AboutComponent />).then(() => {
9+
cy.matchImage()
10+
cy.get('h1').matchImage()
11+
})
12+
})
13+
})
14+
15+
// Prevent TypeScript from reading file as legacy script
16+
export {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Link from 'next/link'
2+
import React from 'react'
3+
import styles from '../styles/Home.module.css'
4+
5+
export default function AboutComponent() {
6+
return (
7+
<>
8+
<h1>About Page</h1>
9+
<p className={styles.description}>
10+
<Link href="/">&larr; Go Back</Link>
11+
</p>
12+
</>
13+
)
14+
}

examples/next/cypress.config.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { defineConfig } from "cypress";
2+
import { initPlugin } from "@frsource/cypress-plugin-visual-regression-diff/plugins";
3+
4+
export default defineConfig({
5+
e2e: {
6+
setupNodeEvents(on, config) {
7+
initPlugin(on, config);
8+
},
9+
baseUrl: "http://localhost:3000",
10+
},
11+
component: {
12+
setupNodeEvents(on, config) {
13+
initPlugin(on, config);
14+
},
15+
devServer: {
16+
framework: "next",
17+
bundler: "webpack",
18+
},
19+
},
20+
});

examples/next/cypress/e2e/app.cy.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
describe("Navigation", () => {
2+
it("should navigate to the about page", () => {
3+
cy.visit("http://localhost:3000/");
4+
5+
cy.get('a[href*="about"]').click();
6+
7+
cy.url().should("include", "/about");
8+
9+
cy.matchImage().then(({ imgNewPath }) => {
10+
// match against image from custom path
11+
cy.matchImage({ matchAgainstPath: imgNewPath });
12+
});
13+
});
14+
});
15+
16+
export {};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "Using fixtures to represent data",
3+
"email": "[email protected]",
4+
"body": "Fixtures are a great way to mock data for responses to routes"
5+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/// <reference types="cypress" />
2+
// ***********************************************
3+
// This example commands.ts shows you how to
4+
// create various custom commands and overwrite
5+
// existing commands.
6+
//
7+
// For more comprehensive examples of custom
8+
// commands please read more here:
9+
// https://on.cypress.io/custom-commands
10+
// ***********************************************
11+
//
12+
//
13+
// -- This is a parent command --
14+
// Cypress.Commands.add('login', (email, password) => { ... })
15+
//
16+
//
17+
// -- This is a child command --
18+
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
19+
//
20+
//
21+
// -- This is a dual command --
22+
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
23+
//
24+
//
25+
// -- This will overwrite an existing command --
26+
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
27+
//
28+
// declare global {
29+
// namespace Cypress {
30+
// interface Chainable {
31+
// login(email: string, password: string): Chainable<void>
32+
// drag(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
33+
// dismiss(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
34+
// visit(originalFn: CommandOriginalFn, url: string, options: Partial<VisitOptions>): Chainable<Element>
35+
// }
36+
// }
37+
// }
38+
39+
import "@frsource/cypress-plugin-visual-regression-diff/dist/support";
40+
41+
// Prevent TypeScript from reading file as legacy script
42+
export {};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
7+
<title>Components App</title>
8+
<!-- Used by Next.js to inject CSS. -->
9+
<div id="__next_css__DO_NOT_USE__"></div>
10+
</head>
11+
<body>
12+
<div data-cy-root></div>
13+
</body>
14+
</html>

0 commit comments

Comments
 (0)