31
loading...
This website collects cookies to deliver better user experience
npx create-next-app
yarn create next-app
npx create-next-app --typescript
yarn create next-app --typescript
npm install cypress --save-dev
or yarn add cypress -D
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"cypress:open": "cypress open"
},
"compilerOptions": {
...
"types": ["cypress"]
},
npm run cypress:open
or yarn cypress:open
will run Cypress for the first time. It will generate automatically every necessary files for you and tests examples in a cypress folder at the root of your project, and open a dedicated page in your browsercypress/integration
folder. You can delete all of them as we'll write our own one.cypress/integration
folder create a home.spec.js file (or home.spec.tsx if you use typescript) and add/// <reference types="cypress"/>
context("Home Page", () => {
beforeEach(() => {
cy.visit("http://localhost:3000");
});
it("should render the home page and display a message", () => {
cy.get("h1").contains("Welcome");
});
});
export {}
to bypass Eslint compilation error/// <reference types="cypress"/>
context("Home Page", () => {
beforeEach(() => {
cy.visit("http://localhost:3000");
});
it("should render the home page and display a message", () => {
cy.get("h1").contains("Welcome");
});
});
export {}
<h1>
tag containing "Welcome"npm install start-server-and-test --save-dev
yarn add start-server-and-test -D
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"cypress:open": "cypress open",
"test": "start-server-and-test dev 3000 cypress:open"
},
npm run test
yarn test
pages
folder// stack.js
const Stack = (props) => {
const favorites = props.stack;
return (
<div style={{display: 'flex', justifyContent: 'center', padding: '2rem'}}>
<main>
<h1>My Favorites Stack</h1>
<ul>
{favorites.map((favorite) => {
return <li key={favorite}>{favorite}</li>;
})}
</ul>
</main>
</div>
);
};
export default Stack;
export async function getStaticProps() {
const favoriteStack = [
'Javascript',
'TypeScript',
'React.js',
'Next.js',
'GraphQL',
'Amazon Web Services',
'Firebase',
];
return {
props: {
stack: favoriteStack
}
}
}
/// <reference types="cypress"/>
const favoriteStack = [
'Javascript',
'TypeScript',
'React.js',
'Next.js',
'GraphQL',
'Amazon Web Services',
'Firebase',
];
context('Stack', () => {
beforeEach(() => {
cy.visit('http://localhost:3000/stack');
});
it('should render the stack page and display the favorite stack', () => {
cy.get('ul>li').each((item, index) => {
cy.wrap(item).should('contain.text', favoriteStack[index]);
});
});
});
getStaticProps
. To demonstrate this we create a new file under pages
platforms.jsconst Platforms = (props) => {
const favorites = props.stack;
return (
<div style={{display: 'flex', justifyContent: 'center', padding: '2rem'}}>
<main>
<h1>My Favorites Freelance platforms</h1>
<ul>
{favorites.map((favorite) => {
return <li key={favorite}>{favorite}</li>;
})}
</ul>
</main>
</div>
);
};
export default Platforms;
export async function getServerSideProps() {
const favoritesPlatforms = [
'Toptal',
'Upwork',
'Malt',
'Comet'
];
return {
props: {
stack: favoritesPlatforms
}
}
}
/// <reference types="cypress"/>
const favoritePlatforms = ['Toptal', 'Upwork', 'Malt', 'Comet'];
context('Platforms', () => {
beforeEach(() => {
cy.visit('http://localhost:3000/platforms');
});
it('should render the platforms page and display the favorite platforms', () => {
cy.get('ul>li').each((item, index) => {
cy.wrap(item).should('contain.text', favoritePlatforms[index]);
});
});
});
31