Skip to content

Commit

Permalink
feat: next example (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
FRSgit authored Jul 9, 2023
1 parent 7034940 commit 247a20a
Show file tree
Hide file tree
Showing 31 changed files with 1,062 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = {
parser: "@typescript-eslint/parser",
parserOptions: {
sourceType: "module",
project: ["./packages/cypress-plugin-visual-regression-diff/tsconfig.json", "./examples/webpack/tsconfig.json"],
project: ["./packages/cypress-plugin-visual-regression-diff/tsconfig.json", "./examples/next/tsconfig.json", "./examples/webpack/tsconfig.json"],
tsconfigRootDir: __dirname,
warnOnUnsupportedTypeScriptVersion: false,
EXPERIMENTAL_useSourceOfProjectReferenceRedirect: true,
Expand Down
96 changes: 85 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,33 @@ jobs:
cache:
name: CI cache
runs-on: ubuntu-latest
outputs:
pnpm-cache: ${{ steps.vars.outputs.pnpm-cache }}
key: ${{ steps.vars.outputs.key }}
steps:
- uses: actions/checkout@v3
- uses: pnpm/[email protected]
- uses: actions/setup-node@v3
with:
node-version: "18.x"
cache: 'pnpm'
- run: pnpm install
- id: vars
run: |
echo "pnpm-cache=$(pnpm store path --silent)" >> $GITHUB_OUTPUT
echo "key=${{ runner.os }}-pnpm-${{ hashFiles('pnpm-lock.yaml') }}" >> $GITHUB_OUTPUT
- name: Cache NPM and Cypress 📦
uses: actions/cache@v3
id: cache
with:
path: |
${{ steps.vars.outputs.pnpm-cache }}
~/.cache/Cypress
key: ${{ steps.vars.outputs.key }}
restore-keys: |
${{ runner.os }}-pnpm-${{ hashFiles('pnpm-lock.yaml') }}
${{ runner.os }}-pnpm-
- run: pnpm install --frozen-lockfile --prefer-offline
env:
HUSKY: '0' # By default do not run HUSKY install

lint:
name: lint
Expand All @@ -30,8 +49,20 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: "18.x"
cache: 'pnpm'
- run: pnpm install
- name: Cache NPM and Cypress 📦
uses: actions/cache@v3
id: cache
with:
path: |
${{ needs.cache.outputs.pnpm-cache }}
~/.cache/Cypress
key: ${{ needs.cache.outputs.key }}
restore-keys: |
${{ runner.os }}-pnpm-${{ hashFiles('pnpm-lock.yaml') }}
${{ runner.os }}-pnpm-
- run: pnpm install --frozen-lockfile --prefer-offline
env:
HUSKY: '0' # By default do not run HUSKY install
- run: pnpm lint:ci
- run: pnpm format:ci

Expand All @@ -45,13 +76,32 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: "18.x"
cache: 'pnpm'
- run: pnpm install && cd ./examples/webpack && pnpm cypress install
- name: Cache NPM and Cypress 📦
uses: actions/cache@v3
id: cache
with:
path: |
${{ needs.cache.outputs.pnpm-cache }}
~/.cache/Cypress
key: ${{ needs.cache.outputs.key }}
restore-keys: |
${{ runner.os }}-pnpm-${{ hashFiles('pnpm-lock.yaml') }}
${{ runner.os }}-pnpm-
- run: pnpm install --frozen-lockfile --prefer-offline
env:
HUSKY: '0' # By default do not run HUSKY install
- run: pnpm --filter cypress-plugin-visual-regression-diff -r build
- name: Test e2e
run: pnpm test:e2e:ci
- name: Test component-testing
run: pnpm test:ct:ci
- uses: actions/upload-artifact@v3
with:
name: test
path: |
examples/next/**/*.png
examples/next/**/*.jpg
examples/next/**/*.jpeg
test-integration-coverage:
name: test-integration-coverage
Expand All @@ -63,8 +113,20 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: "18.x"
cache: 'pnpm'
- run: pnpm install
- name: Cache NPM and Cypress 📦
uses: actions/cache@v3
id: cache
with:
path: |
${{ needs.cache.outputs.pnpm-cache }}
~/.cache/Cypress
key: ${{ needs.cache.outputs.key }}
restore-keys: |
${{ runner.os }}-pnpm-${{ hashFiles('pnpm-lock.yaml') }}
${{ runner.os }}-pnpm-
- run: pnpm install --frozen-lockfile --prefer-offline
env:
HUSKY: '0' # By default do not run HUSKY install
- name: Test integration (with coverage) and upload to CodeClimate
if: ${{ github.ref == 'refs/heads/main' }}
uses: paambaati/[email protected]
Expand Down Expand Up @@ -100,18 +162,30 @@ jobs:
build-and-release:
name: build and release
runs-on: ubuntu-latest
needs: prepare-release
needs: [cache, prepare-release]
if: needs.prepare-release.outputs.releases_created
steps:
- uses: actions/checkout@v3
- uses: pnpm/[email protected]
- uses: actions/setup-node@v3
with:
node-version: "18.x"
cache: 'pnpm'
registry-url: 'https://registry.npmjs.org'
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
- run: pnpm install
- name: Cache NPM and Cypress 📦
uses: actions/cache@v3
id: cache
with:
path: |
${{ needs.cache.outputs.pnpm-cache }}
~/.cache/Cypress
key: ${{ needs.cache.outputs.key }}
restore-keys: |
${{ runner.os }}-pnpm-${{ hashFiles('pnpm-lock.yaml') }}
${{ runner.os }}-pnpm-
- run: pnpm install --frozen-lockfile --prefer-offline
env:
HUSKY: '0' # By default do not run HUSKY install
- name: Build packages to get cross-references working 🔧
run: pnpm build
- name: Release package
Expand Down
1 change: 0 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ After cloning the repository, run:

```bash
pnpm i # installs the project dependencies
cd examples/webpack && pnpm i # install dependencies for example project (useful for testing)
```

### Committing Changes
Expand Down
43 changes: 43 additions & 0 deletions examples/next/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# we do not want to commit local screenshot files
# as they might be different on different OSes
**/__image_snapshots_local__

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts

# cypress
/cypress/screenshots
/cypress/videos
41 changes: 41 additions & 0 deletions examples/next/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Next.js + Cypress + @frsource/cypress-plugin-visual-regression-diff

This example shows how to configure @frsource/cypress-plugin-visual-regression-diff to work with Cypress & Next.js.

## Project setup

```bash
pnpm install
```

### Run end-to-end tests

> Important - remember to run `pnpm && pnpm build` command in this repo's root directory before starting e2e tests.
```bash
pnpm e2e
```

### Run component tests

> Important - remember to run `pnpm && pnpm build` command in this repo's root directory before starting e2e tests.
```bash
pnpm component
```

### Compiles and hot-reloads for development

```bash
pnpm dev
```

### Compiles and minifies for production

```bash
pnpm build
```

## Credits

Created using [Next.js Cypress template](https://nextjs.org/docs/pages/building-your-application/optimizing/testing#cypress).
16 changes: 16 additions & 0 deletions examples/next/components/about-component.cy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import AboutComponent from './about-component'
// Disable ESLint to prevent failing linting inside the Next.js repo.
// If you're using ESLint on your project, we recommend installing the ESLint Cypress plugin instead:
// https://github.com/cypress-io/eslint-plugin-cypress

describe('<AboutComponent />', () => {
it('should render and display expected content', () => {
cy.mount(<AboutComponent />).then(() => {
cy.matchImage()
cy.get('h1').matchImage()
})
})
})

// Prevent TypeScript from reading file as legacy script
export {}
14 changes: 14 additions & 0 deletions examples/next/components/about-component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Link from 'next/link'
import React from 'react'
import styles from '../styles/Home.module.css'

export default function AboutComponent() {
return (
<>
<h1>About Page</h1>
<p className={styles.description}>
<Link href="/">&larr; Go Back</Link>
</p>
</>
)
}
20 changes: 20 additions & 0 deletions examples/next/cypress.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { defineConfig } from "cypress";
import { initPlugin } from "@frsource/cypress-plugin-visual-regression-diff/plugins";

export default defineConfig({
e2e: {
setupNodeEvents(on, config) {
initPlugin(on, config);
},
baseUrl: "http://localhost:3000",
},
component: {
setupNodeEvents(on, config) {
initPlugin(on, config);
},
devServer: {
framework: "next",
bundler: "webpack",
},
},
});
16 changes: 16 additions & 0 deletions examples/next/cypress/e2e/app.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
describe("Navigation", () => {
it("should navigate to the about page", () => {
cy.visit("http://localhost:3000/");

cy.get('a[href*="about"]').click();

cy.url().should("include", "/about");

cy.matchImage().then(({ imgNewPath }) => {
// match against image from custom path
cy.matchImage({ matchAgainstPath: imgNewPath });
});
});
});

export {};
5 changes: 5 additions & 0 deletions examples/next/cypress/fixtures/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Using fixtures to represent data",
"email": "[email protected]",
"body": "Fixtures are a great way to mock data for responses to routes"
}
42 changes: 42 additions & 0 deletions examples/next/cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/// <reference types="cypress" />
// ***********************************************
// This example commands.ts shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add('login', (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
//
// declare global {
// namespace Cypress {
// interface Chainable {
// login(email: string, password: string): Chainable<void>
// drag(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
// dismiss(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
// visit(originalFn: CommandOriginalFn, url: string, options: Partial<VisitOptions>): Chainable<Element>
// }
// }
// }

import "@frsource/cypress-plugin-visual-regression-diff/dist/support";

// Prevent TypeScript from reading file as legacy script
export {};
14 changes: 14 additions & 0 deletions examples/next/cypress/support/component-index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<title>Components App</title>
<!-- Used by Next.js to inject CSS. -->
<div id="__next_css__DO_NOT_USE__"></div>
</head>
<body>
<div data-cy-root></div>
</body>
</html>
Loading

0 comments on commit 247a20a

Please sign in to comment.