Skip to content

Commit c2ac405

Browse files
committed
feat: next example
Signed-off-by: Jakub Freisler <[email protected]>
1 parent 6b2082f commit c2ac405

29 files changed

+1035
-50
lines changed

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,17 @@
1+
import AboutComponent from './about-component'
2+
/* eslint-disable */
3+
// Disable ESLint to prevent failing linting inside the Next.js repo.
4+
// If you're using ESLint on your project, we recommend installing the ESLint Cypress plugin instead:
5+
// https://github.com/cypress-io/eslint-plugin-cypress
6+
7+
describe('<AboutComponent />', () => {
8+
it('should render and display expected content', () => {
9+
cy.mount(<AboutComponent />).then(() => {
10+
cy.matchImage()
11+
cy.get('h1').matchImage()
12+
})
13+
})
14+
})
15+
16+
// Prevent TypeScript from reading file as legacy script
17+
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

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* eslint-disable */
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("Navigation", () => {
7+
it("should navigate to the about page", () => {
8+
cy.visit("http://localhost:3000/");
9+
10+
cy.get('a[href*="about"]').click();
11+
12+
cy.url().should("include", "/about");
13+
14+
cy.matchImage().then(({ imgNewPath }) => {
15+
// match against image from custom path
16+
cy.matchImage({ matchAgainstPath: imgNewPath });
17+
});
18+
});
19+
});
20+
21+
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>
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* eslint-disable @typescript-eslint/no-namespace */
2+
// ***********************************************************
3+
// This example support/component.ts is processed and
4+
// loaded automatically before your test files.
5+
//
6+
// This is a great place to put global configuration and
7+
// behavior that modifies Cypress.
8+
//
9+
// You can change the location of this file or turn off
10+
// automatically serving support files with the
11+
// 'supportFile' configuration option.
12+
//
13+
// You can read more here:
14+
// https://on.cypress.io/configuration
15+
// ***********************************************************
16+
17+
// Import commands.js using ES2015 syntax:
18+
import './commands'
19+
20+
// Alternatively you can use CommonJS syntax:
21+
// require('./commands')
22+
23+
import { mount } from 'cypress/react18'
24+
25+
// Augment the Cypress namespace to include type definitions for
26+
// your custom command.
27+
// Alternatively, can be defined in cypress/support/component.d.ts
28+
// with a <reference path="./component" /> at the top of your spec.
29+
declare global {
30+
namespace Cypress {
31+
interface Chainable {
32+
mount: typeof mount
33+
}
34+
}
35+
}
36+
37+
Cypress.Commands.add('mount', mount)
38+
39+
// Example use:
40+
// cy.mount(<MyComponent />)

examples/next/cypress/support/e2e.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// ***********************************************************
2+
// This example support/e2e.ts is processed and
3+
// loaded automatically before your test files.
4+
//
5+
// This is a great place to put global configuration and
6+
// behavior that modifies Cypress.
7+
//
8+
// You can change the location of this file or turn off
9+
// automatically serving support files with the
10+
// 'supportFile' configuration option.
11+
//
12+
// You can read more here:
13+
// https://on.cypress.io/configuration
14+
// ***********************************************************
15+
16+
// Import commands.js using ES2015 syntax:
17+
import './commands'
18+
19+
// Alternatively you can use CommonJS syntax:
20+
// require('./commands')

examples/next/cypress/tsconfig.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"lib": ["es5", "dom"],
5+
"types": ["cypress", "node"]
6+
},
7+
"include": ["**/*.ts"]
8+
}

examples/next/package.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"private": true,
3+
"scripts": {
4+
"dev": "next dev",
5+
"build": "next build",
6+
"start": "next start",
7+
"test:e2e": "start-server-and-test dev http://localhost:3000 \"cypress open --e2e --env \"pluginVisualRegressionImagesPath={spec_path}/__image_snapshots_local__\"\"",
8+
"test:e2e:ci": "start-server-and-test dev http://localhost:3000 \"cypress run --e2e\"",
9+
"test:ci": "cypress open --component --env \"pluginVisualRegressionImagesPath={spec_path}/__image_snapshots_local__\"",
10+
"test:ct:ci": "cypress run --component"
11+
},
12+
"dependencies": {
13+
"next": "latest",
14+
"react": "18.2.0",
15+
"react-dom": "18.2.0"
16+
},
17+
"devDependencies": {
18+
"@frsource/cypress-plugin-visual-regression-diff": "workspace:*",
19+
"@types/node": "18.0.6",
20+
"@types/react": "18.0.15",
21+
"@types/react-dom": "18.0.6",
22+
"cypress": "12.3.0",
23+
"start-server-and-test": "1.15.2",
24+
"typescript": "4.7.4"
25+
}
26+
}

examples/next/pages/_app.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { AppProps } from 'next/app'
2+
import '../styles/globals.css'
3+
4+
function MyApp({ Component, pageProps }: AppProps) {
5+
return <Component {...pageProps} />
6+
}
7+
8+
export default MyApp

examples/next/pages/about.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import AboutComponent from '../components/about-component'
2+
import styles from '../styles/Home.module.css'
3+
4+
export default function About() {
5+
return (
6+
<div className={styles.container}>
7+
<main className={styles.main}>
8+
<AboutComponent />
9+
</main>
10+
</div>
11+
)
12+
}

0 commit comments

Comments
 (0)