Skip to content
This repository was archived by the owner on Oct 13, 2022. It is now read-only.

Setup Create Next App (Webpack 5) with Cypress Code Coverage #105

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions setup-create-next-app-webpack-5/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["next/babel"],
"plugins": ["istanbul"]
}
35 changes: 35 additions & 0 deletions setup-create-next-app-webpack-5/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage
/.nyc_output

# 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
.env.development.local
.env.test.local
.env.production.local

# vercel
.vercel
54 changes: 54 additions & 0 deletions setup-create-next-app-webpack-5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Component Testing Example: Create Next App (Webpack 5)

This is an example **Create Next App (Webpack 5)** project with Cypress component testing.

- `yarn` to install the dependencies
- `npx cypress open-ct` to run interactively
- `npx cypress run-ct` to run all tests headlessly

The following was done to create this example:

1. Initialize a baseline "nested-components" project in the [setup-create-next-app-webpack-5](.) subdirectory
1. `yarn create next-app --example nested-components setup-create-next-app-webpack-5`
2. `cd setup-create-next-app-webpack-5`
3. Commit [`3c29c7b`](https://github.com/cypress-io/cypress-component-testing-examples/commit/3c29c7b0b1562d29c2dc667bbce182daeae93663)
2. Add Cypress component testing support with sample tests
1. `yarn add -D https://cdn.cypress.io/beta/npm/10.0.0/linux-x64/10.0-release-188b9a742ee2ef51102167bfd84b3696a3f72a26/cypress.tgz webpack webpack-dev-server @cypress/react @cypress/webpack-dev-server html-webpack-plugin`
2. Add [cypress.json](cypress.json)
3. Add [cypress/plugins/index.js](cypress/plugins/index.js)
4. Add [components/paragraph.spec.ct.js](components/paragraph.spec.ct.js), [components/post.spec.ct.js](components/post.spec.ct.js), [pages/index.spec.ct.js](pages/index.spec.ct.js)
5. `npx cypress open-ct`
6. Commit [`78fb6bb`](https://github.com/cypress-io/cypress-component-testing-examples/commit/78fb6bb83345f9909ada9a58474910f8df04a4c4)
3. Add Cypress Code Coverage
1. `yarn add -D @cypress/code-coverage@3.10.0-dev.1 babel-plugin-istanbul`
2. Edit [cypress.config.js](cypress.config.js) to enable `coverage` and configure the Cypress code coverage task
3. Edit [.gitignore](.gitignore) to ignore .nyc_output directory
4. Edit [cypress/support/component.js](cypress/support/component.js) to import Cypress code coverage support
5. Add [.babelrc](.babelrc) to `babel-plugin-istanbul` when Cypress Component tests are running
6. Commit [`c8cfcf0`](https://github.com/cypress-io/cypress-component-testing-examples/commit/c8cfcf0ef4e12bda3817d81af1ddcca06c078c79)

_This example was generated by [setup-create-next-app-webpack-5.sh](https://github.com/cypress-io/cypress-component-testing-examples/blob/main/scripts/setup-create-next-app-webpack-5.sh) on 2022-03-22. The original README follows._

---

# Example app using nested components

Taking advantage of the composable nature of React components we can modularize our apps in self-contained, meaningful components. This example has a page under `pages/index.js` that uses `components/paragraph.js` and `components/post.js` that can be styled and managed separately.

## Deploy your own

Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) or preview live with [StackBlitz](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/nested-components)

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/nested-components&project-name=nested-components&repository-name=nested-components)

## How to use

Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:

```bash
npx create-next-app --example nested-components nested-components-app
# or
yarn create next-app --example nested-components nested-components-app
```

Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
7 changes: 7 additions & 0 deletions setup-create-next-app-webpack-5/components/paragraph.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { mount } from '@cypress/react'
import P from './paragraph'

it('renders a paragraph', () => {
mount(<P>hello world</P>)
cy.get('p').contains('hello world')
})
13 changes: 13 additions & 0 deletions setup-create-next-app-webpack-5/components/paragraph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default function Paragraph({ children }) {
return (
<p>
{children}
<style jsx>{`
p {
font: 13px Helvetica, Arial;
margin: 10px 0;
}
`}</style>
</p>
)
}
12 changes: 12 additions & 0 deletions setup-create-next-app-webpack-5/components/post.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { mount } from '@cypress/react'
import Post from './post'

it('renders a post', () => {
mount(
<Post title='Hello World'>
<p>This is some sample content</p>
</Post>
)
cy.get('h1').contains('Hello World')
cy.get('p').contains('This is some sample content')
})
21 changes: 21 additions & 0 deletions setup-create-next-app-webpack-5/components/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default function Post({ title, children }) {
return (
<div className="main">
<h1>{title}</h1>
{children}
<style jsx>{`
.main {
font: 15px Helvetica, Arial;
border: 1px solid #eee;
padding: 0 10px;
}

h1 {
font-size: 16px;
font-weight: bold;
margin: 10px 0;
}
`}</style>
</div>
)
}
18 changes: 18 additions & 0 deletions setup-create-next-app-webpack-5/cypress.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { defineConfig } = require('cypress')
const { devServer } = require('@cypress/react/plugins/next')
const codeCoverageTask = require("@cypress/code-coverage/task")

module.exports = defineConfig({
coverage: true,
codeCoverage: {
exclude: "cypress/**/*.*",
},
component: {
devServer,
devServerConfig: {},
setupNodeEvents(on, config) {
codeCoverageTask(on, config);
return config;
},
},
})
5 changes: 5 additions & 0 deletions setup-create-next-app-webpack-5/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": "hello@cypress.io",
"body": "Fixtures are a great way to mock data for responses to routes"
}
25 changes: 25 additions & 0 deletions setup-create-next-app-webpack-5/cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ***********************************************
// This example commands.js 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) => { ... })
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>
<div id="__next_css__DO_NOT_USE__"></div>
</head>
<body>

<div id="__cy_root"></div>
</body>
</html>
22 changes: 22 additions & 0 deletions setup-create-next-app-webpack-5/cypress/support/component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// ***********************************************************
// This example support/component.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

import '@cypress/code-coverage/support'

// Import commands.js using ES2015 syntax:
import './commands'

// Alternatively you can use CommonJS syntax:
// require('./commands')
23 changes: 23 additions & 0 deletions setup-create-next-app-webpack-5/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"private": true,
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"devDependencies": {
"@cypress/code-coverage": "3.10.0-dev.1",
"@cypress/react": "^5.12.4",
"@cypress/webpack-dev-server": "^1.8.3",
"babel-plugin-istanbul": "^6.1.1",
"cypress": "https://cdn.cypress.io/beta/npm/10.0.0/linux-x64/10.0-release-188b9a742ee2ef51102167bfd84b3696a3f72a26/cypress.tgz",
"html-webpack-plugin": "^5.5.0",
"webpack": "^5.70.0",
"webpack-dev-server": "^4.7.4"
}
}
9 changes: 9 additions & 0 deletions setup-create-next-app-webpack-5/pages/index.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { mount } from '@cypress/react'
import Home from './index'

it('renders the Home page', () => {
mount(<Home />)
cy.get('h1').should('have.length', 3)
cy.get('p').should('have.length', 6)
cy.get('hr').should('have.length', 2)
})
47 changes: 47 additions & 0 deletions setup-create-next-app-webpack-5/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import P from '../components/paragraph'
import Post from '../components/post'

export default function Home() {
return (
<div className="main">
<Post title="My first blog post">
<P>Hello there</P>
<P>This is an example of a componentized blog post</P>
</Post>

<hr />

<Post title="My second blog post">
<P>Hello there</P>
<P>This is another example.</P>
<P>Wa-hoo!</P>
</Post>

<hr />

<Post title="The final blog post">
<P>C’est fin</P>
</Post>

<style jsx>{`
.main {
margin: auto;
max-width: 420px;
padding: 10px;
}

hr {
width: 100px;
border-width: 0;
margin: 20px auto;
text-align: center;
}

hr::before {
content: '***';
color: #ccc;
}
`}</style>
</div>
)
}
Loading