This repository has been archived by the owner on Mar 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- closes #155 Co-authored-by: Gleb Bahmutov <[email protected]>
- Loading branch information
1 parent
54857cd
commit bf94dea
Showing
23 changed files
with
5,835 additions
and
2,161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ package-lock.json | |
.nyc_output | ||
coverage | ||
*.generated.css | ||
.next |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# example: Next.js | ||
|
||
> A typical project using [next.js](https://nextjs.org/) | ||
## Configuration | ||
|
||
In order to reuse next's webpack configuration and all the custom configuration defined in `next.config.js` connect special plugin in [plugin file](./cypress/plugins/index.js) | ||
|
||
```js | ||
const preprocessor = require('cypress-react-unit-test/plugins/next') | ||
|
||
module.exports = (on, config) => { | ||
preprocessor(on, config) | ||
|
||
return config | ||
} | ||
``` | ||
|
||
## Usage | ||
|
||
1. Run `npm install` in this folder to install dependencies. | ||
|
||
```bash | ||
# in this folder | ||
npm install | ||
``` | ||
|
||
3. Start Cypress | ||
|
||
```bash | ||
npm run cy:open | ||
# or just run headless tests | ||
npm test | ||
``` | ||
|
||
## Server side props | ||
|
||
⚠️⚠️ **Important:** ⚠️⚠️ Please do not test the page components using component testing. These components have too much responsibility and need to be tested as a part of your application flow. Consider using cypress `integration` tests. | ||
|
||
But if you still want to test the page component, make sure that it will be mounted as any other pure `React` component. It means that next's specific functions like `getInitialProps` or `getStaticProps` **won't be called**. | ||
|
||
But still you can call them manually: | ||
|
||
```js | ||
IndexPage.getInitialProps().then(props => { | ||
mount(<IndexPage {...props} />) | ||
}) | ||
|
||
cy.contains( | ||
'`.getInitialProps()` was called and passed props to this component', | ||
) | ||
``` | ||
|
||
Find more examples in [Page.spec.jsx](./cypress/components/Page.spec.jsx). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Hello, *world*! | ||
|
||
Below is an example of JSX embedded in Markdown. <br /> **Try and change | ||
the background color!** | ||
|
||
<div style={{ padding: '20px', backgroundColor: 'tomato' }}> | ||
<h3>This is JSX</h3> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import * as React from 'react' | ||
|
||
export function Search() { | ||
const [value, setValue] = React.useState('') | ||
return ( | ||
<div> | ||
<input | ||
aria-label="search" | ||
value={value} | ||
onChange={e => setValue(e.currentTarget.value)} | ||
/> | ||
|
||
<p className="search-text">You are searching for: {value}</p> | ||
|
||
<style jsx>{` | ||
input { | ||
border-radius: 20px; | ||
} | ||
div { | ||
padding: 16px; | ||
background: tomato; | ||
} | ||
`}</style> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"testFiles": "**/*.spec.{js,jsx}", | ||
"viewportWidth": 500, | ||
"viewportHeight": 800, | ||
"experimentalComponentTesting": true, | ||
"experimentalFetchPolyfill": true, | ||
"componentFolder": "cypress/components" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/// <reference types="cypress" /> | ||
import * as React from 'react' | ||
import IndexPage from '../../pages/index' | ||
import { mount } from 'cypress-react-unit-test' | ||
|
||
describe('NextJS page', () => { | ||
it('Renders page component', () => { | ||
mount(<IndexPage />) | ||
|
||
cy.contains('Welcome to Next.js') | ||
}) | ||
|
||
it("It doesn't run the `.getInitialProps()`", () => { | ||
mount(<IndexPage />) | ||
|
||
cy.get('[data-testid="server-result"').should( | ||
'not.contain', | ||
'`.getInitialProps()` was called and passed props to this component', | ||
) | ||
}) | ||
|
||
it('Allows to manually mock the server side props', () => { | ||
mount(<IndexPage asyncProp />) | ||
|
||
cy.contains( | ||
'`.getInitialProps()` was called and passed props to this component', | ||
) | ||
}) | ||
|
||
it('can be tested with real .getInitialProps call', () => { | ||
IndexPage.getInitialProps().then(props => { | ||
mount(<IndexPage {...props} />) | ||
}) | ||
|
||
cy.contains( | ||
'`.getInitialProps()` was called and passed props to this component', | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/// <reference types="cypress" /> | ||
import * as React from 'react' | ||
import { mount } from 'cypress-react-unit-test' | ||
import { Search } from '../../components/Search' | ||
import HelloWorld from '../../components/HelloWorld.mdx' | ||
|
||
describe('<Search /> NextJS component', () => { | ||
it('Renders component', () => { | ||
mount(<Search />) | ||
|
||
cy.get('input').type('124152') | ||
cy.contains('.search-text', '124152').should('be.visible') | ||
}) | ||
|
||
it('Renders mdx component using custom next.config.js', () => { | ||
mount(<HelloWorld />) | ||
|
||
cy.contains('Hello').should('have.css', 'fontWeight', '700') | ||
cy.contains('This is JSX') | ||
.parent() | ||
.should('have.css', 'background-color', 'rgb(255, 99, 71)') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/// <reference types="cypress" /> | ||
describe('integration spec', () => { | ||
it('works', () => { | ||
expect(1).to.equal(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const preprocessor = require('cypress-react-unit-test/plugins/next') | ||
|
||
/** | ||
* @type {Cypress.PluginConfig} | ||
*/ | ||
module.exports = (on, config) => { | ||
preprocessor(on, config) | ||
|
||
return config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import 'cypress-react-unit-test/support' | ||
import '@cypress/code-coverage/support' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
const withMDX = require('@next/mdx')() | ||
const withSass = require('@zeit/next-sass') | ||
|
||
module.exports = withSass(withMDX()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "example-nextjs", | ||
"version": "0.1.0", | ||
"license": "MIT", | ||
"description": "Fancy Next.js app", | ||
"scripts": { | ||
"dev": "next", | ||
"build": "next build", | ||
"start": "next start", | ||
"test": "node ../../scripts/cypress-expect run --passing 7", | ||
"cy:open": "../../node_modules/.bin/cypress open", | ||
"build:static": "next build && next out", | ||
"check-coverage": "echo no code coverage yet", | ||
"only-covered": "echo no code coverage yet" | ||
}, | ||
"devDependencies": { | ||
"cypress-react-unit-test": "file:../.." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Search } from '../components/Search' | ||
import HelloWorld from '../components/HelloWorld.mdx' | ||
|
||
function IndexPage({ asyncProp }) { | ||
return ( | ||
<main> | ||
<h1>Welcome to Next.js</h1> | ||
|
||
{asyncProp && ( | ||
<p data-testid="server-result"> | ||
`.getInitialProps()` was called and passed props to this component | ||
</p> | ||
)} | ||
|
||
<Search /> | ||
<HelloWorld /> | ||
</main> | ||
) | ||
} | ||
|
||
IndexPage.getInitialProps = async ctx => { | ||
return { asyncProp: true } | ||
} | ||
|
||
export default IndexPage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.