Unverified Commit 48dba645 authored by Balázs Orbán's avatar Balázs Orbán Committed by GitHub

chore: convert Jest examples to TypeScript (#32705)

* chore: convert Jest examples to TypeScript

* test: use absolute imports in test files

* chore: address review comments

* fix: add pages to tsconfig.json paths

* fix: add styles to tsconfig.json paths

* chore: upgrade dependencies/snapshots

* docs: match Testing docs with example code

* docs: link to docs from mock test files
Co-authored-by: 's avatarLee Robinson <me@leerob.io>
parent 6581ba9d
...@@ -262,10 +262,10 @@ npx create-next-app@latest --example with-jest with-jest-app ...@@ -262,10 +262,10 @@ npx create-next-app@latest --example with-jest with-jest-app
Since the release of [Next.js 12](https://nextjs.org/blog/next-12), Next.js now has built-in configuration for Jest. Since the release of [Next.js 12](https://nextjs.org/blog/next-12), Next.js now has built-in configuration for Jest.
To set up Jest, install `jest` , `@testing-library/react`, `@testing-library/jest-dom` and `react-test-renderer`: To set up Jest, install `jest` , `@testing-library/react`, `@testing-library/jest-dom`:
```bash ```bash
npm install --save-dev jest @testing-library/react @testing-library/jest-dom react-test-renderer npm install --save-dev jest @testing-library/react @testing-library/jest-dom
``` ```
Create a `jest.config.js` file in your project's root directory and add the following: Create a `jest.config.js` file in your project's root directory and add the following:
...@@ -284,6 +284,7 @@ const customJestConfig = { ...@@ -284,6 +284,7 @@ const customJestConfig = {
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'], setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
// if using TypeScript with a baseUrl set to the root directory then you need the below for alias' to work // if using TypeScript with a baseUrl set to the root directory then you need the below for alias' to work
moduleDirectories: ['node_modules', '<rootDir>/'], moduleDirectories: ['node_modules', '<rootDir>/'],
testEnvironment: 'jest-environment-jsdom',
} }
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async // createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
...@@ -340,6 +341,7 @@ module.exports = { ...@@ -340,6 +341,7 @@ module.exports = {
'/node_modules/', '/node_modules/',
'^.+\\.module\\.(css|sass|scss)$', '^.+\\.module\\.(css|sass|scss)$',
], ],
testEnvironment: 'jest-environment-jsdom',
} }
``` ```
...@@ -430,16 +432,11 @@ Add the Jest executable in watch mode to the `package.json` scripts: ...@@ -430,16 +432,11 @@ Add the Jest executable in watch mode to the `package.json` scripts:
Your project is now ready to run tests. Follow Jests convention by adding tests to the `__tests__` folder in your project's root directory. Your project is now ready to run tests. Follow Jests convention by adding tests to the `__tests__` folder in your project's root directory.
For example, we can add a test to check if the `<Index />` component successfully renders a heading: For example, we can add a test to check if the `<Home />` component successfully renders a heading:
```jsx ```jsx
// __tests__/index.test.jsx // __tests__/index.test.jsx
/**
* @jest-environment jsdom
*/
import React from 'react'
import { render, screen } from '@testing-library/react' import { render, screen } from '@testing-library/react'
import Home from '../pages/index' import Home from '../pages/index'
...@@ -456,19 +453,17 @@ describe('Home', () => { ...@@ -456,19 +453,17 @@ describe('Home', () => {
}) })
``` ```
> **Note**: The `@jest-environment jsdom` comment above configures the testing environment as `jsdom` inside the test file because React Testing Library uses DOM elements like `document.body` which will not work in Jest's default `node` testing environment. Alternatively, you can also set the `jsdom` environment globally by adding the Jest configuration option: `"testEnvironment": "jsdom"` in `jest.config.js`. Optionally, add a [snapshot test](https://jestjs.io/docs/snapshot-testing) to keep track of any unexpected changes to your `<Home />` component:
Optionally, add a [snapshot test](https://jestjs.io/docs/snapshot-testing) to keep track of any unexpected changes to your `<Index />` component:
```jsx ```jsx
// __tests__/snapshot.js // __tests__/snapshot.js
import React from 'react'
import renderer from 'react-test-renderer' import { render } from '@testing-library/react'
import Index from '../pages/index' import Home from '../pages/index'
it('renders homepage unchanged', () => { it('renders homepage unchanged', () => {
const tree = renderer.create(<Index />).toJSON() const { container } = render(<Home />)
expect(tree).toMatchSnapshot() expect(container).toMatchSnapshot()
}) })
``` ```
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
This example shows how to configure Jest to work with Next.js and Babel. Since the release of Next.js 12, Next.js has in-built configuration for Jest with SWC. See the [with-jest](https://github.com/vercel/next.js/tree/canary/examples/with-jest) example for the latest implementation. This example shows how to configure Jest to work with Next.js and Babel. Since the release of Next.js 12, Next.js has in-built configuration for Jest with SWC. See the [with-jest](https://github.com/vercel/next.js/tree/canary/examples/with-jest) example for the latest implementation.
This includes Next.js' built-in support for Global CSS, CSS Modules, and TypeScript! This includes Next.js' built-in support for Global CSS, CSS Modules and TypeScript.
## How to Use ## How to Use
......
// Read more at "Handling stylesheets and image imports" on https://nextjs.org/docs/testing
module.exports = 'test-file-stub' module.exports = 'test-file-stub'
// Mock CSS files. // Mock CSS files.
// If you're using CSS modules, this file can be deleted. // If you're using CSS modules, this file can be deleted.
// Read more at "Handling stylesheets and image imports" on https://nextjs.org/docs/testing
module.exports = {} module.exports = {}
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`renders homepage unchanged 1`] = `
<div
className="container"
>
<main>
<h1
className="title"
>
Welcome to
<a
href="https://nextjs.org"
>
Next.js!
</a>
</h1>
<p
className="description"
>
Get started by editing
<code>
pages/index.js
</code>
</p>
<div
className="grid"
>
<a
className="card"
href="https://nextjs.org/docs"
>
<h3>
Documentation →
</h3>
<p>
Find in-depth information about Next.js features and API.
</p>
</a>
<a
className="card"
href="https://nextjs.org/learn"
>
<h3>
Learn →
</h3>
<p>
Learn about Next.js in an interactive course with quizzes!
</p>
</a>
<a
className="card"
href="https://github.com/vercel/next.js/tree/master/examples"
>
<h3>
Examples →
</h3>
<p>
Discover and deploy boilerplate example Next.js projects.
</p>
</a>
<a
className="card"
href="https://vercel.com/new"
>
<h3>
Deploy →
</h3>
<p>
Instantly deploy your Next.js site to a public URL with Vercel.
</p>
</a>
</div>
</main>
<footer
className="footer"
>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
rel="noopener noreferrer"
target="_blank"
>
Powered by
<span
className="logo"
>
<span
style={
Object {
"background": "none",
"border": 0,
"boxSizing": "border-box",
"display": "inline-block",
"height": "initial",
"margin": 0,
"maxWidth": "100%",
"opacity": 1,
"overflow": "hidden",
"padding": 0,
"position": "relative",
"width": "initial",
}
}
>
<span
style={
Object {
"background": "none",
"border": 0,
"boxSizing": "border-box",
"display": "block",
"height": "initial",
"margin": 0,
"maxWidth": "100%",
"opacity": 1,
"padding": 0,
"width": "initial",
}
}
>
<img
alt=""
aria-hidden={true}
src="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNzIiIGhlaWdodD0iMTYiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIi8+"
style={
Object {
"background": "none",
"border": 0,
"display": "block",
"height": "initial",
"margin": 0,
"maxWidth": "100%",
"opacity": 1,
"padding": 0,
"width": "initial",
}
}
/>
</span>
<img
alt="Vercel Logo"
data-nimg="intrinsic"
decoding="async"
src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
style={
Object {
"border": "none",
"bottom": 0,
"boxSizing": "border-box",
"display": "block",
"height": 0,
"left": 0,
"margin": "auto",
"maxHeight": "100%",
"maxWidth": "100%",
"minHeight": "100%",
"minWidth": "100%",
"objectFit": undefined,
"objectPosition": undefined,
"padding": 0,
"position": "absolute",
"right": 0,
"top": 0,
"width": 0,
}
}
/>
<noscript>
<img
alt="Vercel Logo"
data-nimg="intrinsic"
decoding="async"
loading="lazy"
src="/_next/image?url=%2Fvercel.svg&w=256&q=75"
srcSet="/_next/image?url=%2Fvercel.svg&w=96&q=75 1x, /_next/image?url=%2Fvercel.svg&w=256&q=75 2x"
style={
Object {
"border": "none",
"bottom": 0,
"boxSizing": "border-box",
"display": "block",
"height": 0,
"left": 0,
"margin": "auto",
"maxHeight": "100%",
"maxWidth": "100%",
"minHeight": "100%",
"minWidth": "100%",
"objectFit": undefined,
"objectPosition": undefined,
"padding": 0,
"position": "absolute",
"right": 0,
"top": 0,
"width": 0,
}
}
/>
</noscript>
</span>
</span>
</a>
</footer>
</div>
`;
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`renders homepage unchanged 1`] = `
<div>
<div
class="container"
>
<main>
<h1
class="title"
>
Welcome to
<a
href="https://nextjs.org"
>
Next.js!
</a>
</h1>
<p
class="description"
>
Get started by editing
<code>
pages/index.js
</code>
</p>
<div
class="grid"
>
<a
class="card"
href="https://nextjs.org/docs"
>
<h3>
Documentation →
</h3>
<p>
Find in-depth information about Next.js features and API.
</p>
</a>
<a
class="card"
href="https://nextjs.org/learn"
>
<h3>
Learn →
</h3>
<p>
Learn about Next.js in an interactive course with quizzes!
</p>
</a>
<a
class="card"
href="https://github.com/vercel/next.js/tree/master/examples"
>
<h3>
Examples →
</h3>
<p>
Discover and deploy boilerplate example Next.js projects.
</p>
</a>
<a
class="card"
href="https://vercel.com/new"
>
<h3>
Deploy →
</h3>
<p>
Instantly deploy your Next.js site to a public URL with Vercel.
</p>
</a>
</div>
</main>
<footer
class="footer"
>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
rel="noopener noreferrer"
target="_blank"
>
Powered by
<span
class="logo"
>
<span
style="box-sizing: border-box; display: inline-block; overflow: hidden; background: none; opacity: 1; border: 0px; margin: 0px; padding: 0px; position: relative; max-width: 100%;"
>
<span
style="box-sizing: border-box; display: block; background: none; opacity: 1; border: 0px; margin: 0px; padding: 0px; max-width: 100%;"
>
<img
alt=""
aria-hidden="true"
src="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNzIiIGhlaWdodD0iMTYiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIi8+"
style="display: block; max-width: 100%; background: none; opacity: 1; border: 0px; margin: 0px; padding: 0px;"
/>
</span>
<img
alt="Vercel Logo"
data-nimg="intrinsic"
decoding="async"
src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
style="position: absolute; top: 0px; left: 0px; bottom: 0px; right: 0px; box-sizing: border-box; padding: 0px; margin: auto; display: block; width: 0px; height: 0px; min-width: 100%; max-width: 100%; min-height: 100%; max-height: 100%;"
/>
<noscript />
</span>
</span>
</a>
</footer>
</div>
</div>
`;
/**
* @jest-environment jsdom
*/
import React from 'react'
import { render, screen } from '@testing-library/react' import { render, screen } from '@testing-library/react'
import Home from '../pages/index' import Home from '@/pages/index'
describe('Home', () => { describe('Home', () => {
it('renders a heading', () => { it('renders a heading', () => {
......
import React from 'react'
import renderer from 'react-test-renderer'
import Index from '../pages/index'
it('renders homepage unchanged', () => {
const tree = renderer.create(<Index />).toJSON()
expect(tree).toMatchSnapshot()
})
import { render } from '@testing-library/react'
import Home from '@/pages/index'
it('renders homepage unchanged', () => {
const { container } = render(<Home />)
expect(container).toMatchSnapshot()
})
...@@ -20,6 +20,8 @@ module.exports = { ...@@ -20,6 +20,8 @@ module.exports = {
// Handle module aliases // Handle module aliases
'^@/components/(.*)$': '<rootDir>/components/$1', '^@/components/(.*)$': '<rootDir>/components/$1',
'^@/pages/(.*)$': '<rootDir>/pages/$1',
}, },
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'], setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/.next/'], testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/.next/'],
...@@ -32,4 +34,5 @@ module.exports = { ...@@ -32,4 +34,5 @@ module.exports = {
'/node_modules/', '/node_modules/',
'^.+\\.module\\.(css|sass|scss)$', '^.+\\.module\\.(css|sass|scss)$',
], ],
testEnvironment: 'jest-environment-jsdom',
} }
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/components/*": ["components/*"]
}
}
}
/// <reference types="next" />
/// <reference types="next/image-types/global" />
// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
...@@ -14,15 +14,16 @@ ...@@ -14,15 +14,16 @@
"react-dom": "^17.0.2" "react-dom": "^17.0.2"
}, },
"devDependencies": { "devDependencies": {
"@testing-library/jest-dom": "5.14.1", "@testing-library/jest-dom": "5.16.1",
"@testing-library/react": "12.0.0", "@testing-library/react": "12.1.2",
"@testing-library/user-event": "13.2.1", "@testing-library/user-event": "13.5.0",
"babel-jest": "27.0.6", "@types/react": "17.0.37",
"eslint": "7.32.0", "babel-jest": "27.4.5",
"eslint": "8.5.0",
"eslint-config-next": "latest", "eslint-config-next": "latest",
"eslint-plugin-testing-library": "4.11.0", "eslint-plugin-testing-library": "5.0.1",
"identity-obj-proxy": "3.0.0", "identity-obj-proxy": "3.0.0",
"jest": "27.0.6", "jest": "27.4.5",
"react-test-renderer": "17.0.2" "typescript": "4.5.4"
} }
} }
import '../styles/global.css'
// This default export is required in a new `pages/_app.js` file.
export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
import '@/styles/global.css'
import type { AppProps } from 'next/app'
export default function MyApp({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />
}
import Head from 'next/head'
import styles from './index.module.css'
import Image from 'next/image'
const Home = () => (
<div className={styles.container}>
<Head>
<title>Create Next App</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main>
<h1 className={styles.title}>
Welcome to <a href="https://nextjs.org">Next.js!</a>
</h1>
<p className={styles.description}>
Get started by editing <code>pages/index.js</code>
</p>
<div className={styles.grid}>
<a href="https://nextjs.org/docs" className={styles.card}>
<h3>Documentation &rarr;</h3>
<p>Find in-depth information about Next.js features and API.</p>
</a>
<a href="https://nextjs.org/learn" className={styles.card}>
<h3>Learn &rarr;</h3>
<p>Learn about Next.js in an interactive course with quizzes!</p>
</a>
<a
href="https://github.com/vercel/next.js/tree/master/examples"
className={styles.card}
>
<h3>Examples &rarr;</h3>
<p>Discover and deploy boilerplate example Next.js projects.</p>
</a>
<a href="https://vercel.com/new" className={styles.card}>
<h3>Deploy &rarr;</h3>
<p>Instantly deploy your Next.js site to a public URL with Vercel.</p>
</a>
</div>
</main>
<footer className={styles.footer}>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{' '}
<span className={styles.logo}>
<Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
</span>
</a>
</footer>
</div>
)
export default Home
import Head from 'next/head'
import Image from 'next/image'
import styles from '@/pages/index.module.css'
export default function Home() {
return (
<div className={styles.container}>
<Head>
<title>Create Next App</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main>
<h1 className={styles.title}>
Welcome to <a href="https://nextjs.org">Next.js!</a>
</h1>
<p className={styles.description}>
Get started by editing <code>pages/index.js</code>
</p>
<div className={styles.grid}>
<a href="https://nextjs.org/docs" className={styles.card}>
<h3>Documentation &rarr;</h3>
<p>Find in-depth information about Next.js features and API.</p>
</a>
<a href="https://nextjs.org/learn" className={styles.card}>
<h3>Learn &rarr;</h3>
<p>Learn about Next.js in an interactive course with quizzes!</p>
</a>
<a
href="https://github.com/vercel/next.js/tree/master/examples"
className={styles.card}
>
<h3>Examples &rarr;</h3>
<p>Discover and deploy boilerplate example Next.js projects.</p>
</a>
<a href="https://vercel.com/new" className={styles.card}>
<h3>Deploy &rarr;</h3>
<p>
Instantly deploy your Next.js site to a public URL with Vercel.
</p>
</a>
</div>
</main>
<footer className={styles.footer}>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{' '}
<span className={styles.logo}>
<Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
</span>
</a>
</footer>
</div>
)
}
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"baseUrl": ".",
"paths": {
"@/components/*": ["components/*"],
"@/pages/*": ["pages/*"],
"@/styles/*": ["styles/*"],
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "types.d.ts"],
"exclude": ["node_modules"]
}
declare module '*module.css' {
const styles: {
[className: string]: string
}
export default styles
}
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
This example shows how to configure Jest to work with Next.js. This example shows how to configure Jest to work with Next.js.
This includes Next.js' built-in support for Global CSS and CSS Modules. This includes Next.js' built-in support for Global CSS, CSS Modules and TypeScript.
## How to Use ## How to Use
......
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`renders homepage unchanged 1`] = `
<div
className="container"
>
<main>
<h1
className="title"
>
Welcome to
<a
href="https://nextjs.org"
>
Next.js!
</a>
</h1>
<p
className="description"
>
Get started by editing
<code>
pages/index.js
</code>
</p>
<div
className="grid"
>
<a
className="card"
href="https://nextjs.org/docs"
>
<h3>
Documentation →
</h3>
<p>
Find in-depth information about Next.js features and API.
</p>
</a>
<a
className="card"
href="https://nextjs.org/learn"
>
<h3>
Learn →
</h3>
<p>
Learn about Next.js in an interactive course with quizzes!
</p>
</a>
<a
className="card"
href="https://github.com/vercel/next.js/tree/master/examples"
>
<h3>
Examples →
</h3>
<p>
Discover and deploy boilerplate example Next.js projects.
</p>
</a>
<a
className="card"
href="https://vercel.com/new"
>
<h3>
Deploy →
</h3>
<p>
Instantly deploy your Next.js site to a public URL with Vercel.
</p>
</a>
</div>
</main>
<footer
className="footer"
>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
rel="noopener noreferrer"
target="_blank"
>
Powered by
<span
className="logo"
>
<span
style={
Object {
"background": "none",
"border": 0,
"boxSizing": "border-box",
"display": "inline-block",
"height": "initial",
"margin": 0,
"maxWidth": "100%",
"opacity": 1,
"overflow": "hidden",
"padding": 0,
"position": "relative",
"width": "initial",
}
}
>
<span
style={
Object {
"background": "none",
"border": 0,
"boxSizing": "border-box",
"display": "block",
"height": "initial",
"margin": 0,
"maxWidth": "100%",
"opacity": 1,
"padding": 0,
"width": "initial",
}
}
>
<img
alt=""
aria-hidden={true}
src="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNzIiIGhlaWdodD0iMTYiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIi8+"
style={
Object {
"background": "none",
"border": 0,
"display": "block",
"height": "initial",
"margin": 0,
"maxWidth": "100%",
"opacity": 1,
"padding": 0,
"width": "initial",
}
}
/>
</span>
<img
alt="Vercel Logo"
data-nimg="intrinsic"
decoding="async"
src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
style={
Object {
"border": "none",
"bottom": 0,
"boxSizing": "border-box",
"display": "block",
"height": 0,
"left": 0,
"margin": "auto",
"maxHeight": "100%",
"maxWidth": "100%",
"minHeight": "100%",
"minWidth": "100%",
"objectFit": undefined,
"objectPosition": undefined,
"padding": 0,
"position": "absolute",
"right": 0,
"top": 0,
"width": 0,
}
}
/>
<noscript>
<img
alt="Vercel Logo"
data-nimg="intrinsic"
decoding="async"
loading="lazy"
src="/_next/image?url=%2Fvercel.svg&w=256&q=75"
srcSet="/_next/image?url=%2Fvercel.svg&w=96&q=75 1x, /_next/image?url=%2Fvercel.svg&w=256&q=75 2x"
style={
Object {
"border": "none",
"bottom": 0,
"boxSizing": "border-box",
"display": "block",
"height": 0,
"left": 0,
"margin": "auto",
"maxHeight": "100%",
"maxWidth": "100%",
"minHeight": "100%",
"minWidth": "100%",
"objectFit": undefined,
"objectPosition": undefined,
"padding": 0,
"position": "absolute",
"right": 0,
"top": 0,
"width": 0,
}
}
/>
</noscript>
</span>
</span>
</a>
</footer>
</div>
`;
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`renders homepage unchanged 1`] = `
<div>
<div
class="container"
>
<main>
<h1
class="title"
>
Welcome to
<a
href="https://nextjs.org"
>
Next.js!
</a>
</h1>
<p
class="description"
>
Get started by editing
<code>
pages/index.js
</code>
</p>
<div
class="grid"
>
<a
class="card"
href="https://nextjs.org/docs"
>
<h3>
Documentation →
</h3>
<p>
Find in-depth information about Next.js features and API.
</p>
</a>
<a
class="card"
href="https://nextjs.org/learn"
>
<h3>
Learn →
</h3>
<p>
Learn about Next.js in an interactive course with quizzes!
</p>
</a>
<a
class="card"
href="https://github.com/vercel/next.js/tree/master/examples"
>
<h3>
Examples →
</h3>
<p>
Discover and deploy boilerplate example Next.js projects.
</p>
</a>
<a
class="card"
href="https://vercel.com/new"
>
<h3>
Deploy →
</h3>
<p>
Instantly deploy your Next.js site to a public URL with Vercel.
</p>
</a>
</div>
</main>
<footer
class="footer"
>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
rel="noopener noreferrer"
target="_blank"
>
Powered by
<span
class="logo"
>
<span
style="box-sizing: border-box; display: inline-block; overflow: hidden; background: none; opacity: 1; border: 0px; margin: 0px; padding: 0px; position: relative; max-width: 100%;"
>
<span
style="box-sizing: border-box; display: block; background: none; opacity: 1; border: 0px; margin: 0px; padding: 0px; max-width: 100%;"
>
<img
alt=""
aria-hidden="true"
src="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNzIiIGhlaWdodD0iMTYiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmVyc2lvbj0iMS4xIi8+"
style="display: block; max-width: 100%; background: none; opacity: 1; border: 0px; margin: 0px; padding: 0px;"
/>
</span>
<img
alt="Vercel Logo"
data-nimg="intrinsic"
decoding="async"
src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
style="position: absolute; top: 0px; left: 0px; bottom: 0px; right: 0px; box-sizing: border-box; padding: 0px; margin: auto; display: block; width: 0px; height: 0px; min-width: 100%; max-width: 100%; min-height: 100%; max-height: 100%;"
/>
<noscript />
</span>
</span>
</a>
</footer>
</div>
</div>
`;
/**
* @jest-environment jsdom
*/
import React from 'react'
import { render, screen } from '@testing-library/react' import { render, screen } from '@testing-library/react'
import Home from '../pages/index' import Home from '@/pages/index'
describe('Home', () => { describe('Home', () => {
it('renders a heading', () => { it('renders a heading', () => {
......
import React from 'react'
import renderer from 'react-test-renderer'
import Index from '../pages/index'
it('renders homepage unchanged', () => {
const tree = renderer.create(<Index />).toJSON()
expect(tree).toMatchSnapshot()
})
import { render } from '@testing-library/react'
import Home from '@/pages/index'
it('renders homepage unchanged', () => {
const { container } = render(<Home />)
expect(container).toMatchSnapshot()
})
...@@ -11,7 +11,10 @@ const customJestConfig = { ...@@ -11,7 +11,10 @@ const customJestConfig = {
moduleNameMapper: { moduleNameMapper: {
// Handle module aliases (this will be automatically configured for you soon) // Handle module aliases (this will be automatically configured for you soon)
'^@/components/(.*)$': '<rootDir>/components/$1', '^@/components/(.*)$': '<rootDir>/components/$1',
'^@/pages/(.*)$': '<rootDir>/pages/$1',
}, },
testEnvironment: 'jest-environment-jsdom',
} }
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async // createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
......
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/components/*": ["components/*"]
}
}
}
/// <reference types="next" />
/// <reference types="next/image-types/global" />
// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
...@@ -14,14 +14,13 @@ ...@@ -14,14 +14,13 @@
"react-dom": "^17.0.2" "react-dom": "^17.0.2"
}, },
"devDependencies": { "devDependencies": {
"@testing-library/jest-dom": "5.14.1", "@testing-library/jest-dom": "5.16.1",
"@testing-library/react": "12.0.0", "@testing-library/react": "12.1.2",
"@testing-library/user-event": "13.2.1", "@testing-library/user-event": "13.5.0",
"babel-jest": "27.0.6", "babel-jest": "27.4.5",
"eslint": "7.32.0", "eslint": "8.5.0",
"eslint-config-next": "latest", "eslint-config-next": "latest",
"eslint-plugin-testing-library": "4.11.0", "eslint-plugin-testing-library": "5.0.1",
"jest": "27.3.1", "jest": "27.4.5"
"react-test-renderer": "17.0.2"
} }
} }
import '../styles/global.css'
// This default export is required in a new `pages/_app.js` file.
export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
import '@/styles/global.css'
import type { AppProps } from 'next/app'
export default function MyApp({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />
}
import Head from 'next/head'
import Image from 'next/image'
import styles from './index.module.css'
const Home = () => (
<div className={styles.container}>
<Head>
<title>Create Next App</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main>
<h1 className={styles.title}>
Welcome to <a href="https://nextjs.org">Next.js!</a>
</h1>
<p className={styles.description}>
Get started by editing <code>pages/index.js</code>
</p>
<div className={styles.grid}>
<a href="https://nextjs.org/docs" className={styles.card}>
<h3>Documentation &rarr;</h3>
<p>Find in-depth information about Next.js features and API.</p>
</a>
<a href="https://nextjs.org/learn" className={styles.card}>
<h3>Learn &rarr;</h3>
<p>Learn about Next.js in an interactive course with quizzes!</p>
</a>
<a
href="https://github.com/vercel/next.js/tree/master/examples"
className={styles.card}
>
<h3>Examples &rarr;</h3>
<p>Discover and deploy boilerplate example Next.js projects.</p>
</a>
<a href="https://vercel.com/new" className={styles.card}>
<h3>Deploy &rarr;</h3>
<p>Instantly deploy your Next.js site to a public URL with Vercel.</p>
</a>
</div>
</main>
<footer className={styles.footer}>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{' '}
<span className={styles.logo}>
<Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
</span>
</a>
</footer>
</div>
)
export default Home
import Head from 'next/head'
import Image from 'next/image'
import styles from '@/pages/index.module.css'
export default function Home() {
return (
<div className={styles.container}>
<Head>
<title>Create Next App</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<main>
<h1 className={styles.title}>
Welcome to <a href="https://nextjs.org">Next.js!</a>
</h1>
<p className={styles.description}>
Get started by editing <code>pages/index.js</code>
</p>
<div className={styles.grid}>
<a href="https://nextjs.org/docs" className={styles.card}>
<h3>Documentation &rarr;</h3>
<p>Find in-depth information about Next.js features and API.</p>
</a>
<a href="https://nextjs.org/learn" className={styles.card}>
<h3>Learn &rarr;</h3>
<p>Learn about Next.js in an interactive course with quizzes!</p>
</a>
<a
href="https://github.com/vercel/next.js/tree/master/examples"
className={styles.card}
>
<h3>Examples &rarr;</h3>
<p>Discover and deploy boilerplate example Next.js projects.</p>
</a>
<a href="https://vercel.com/new" className={styles.card}>
<h3>Deploy &rarr;</h3>
<p>
Instantly deploy your Next.js site to a public URL with Vercel.
</p>
</a>
</div>
</main>
<footer className={styles.footer}>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{' '}
<span className={styles.logo}>
<Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
</span>
</a>
</footer>
</div>
)
}
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"baseUrl": ".",
"paths": {
"@/components/*": ["components/*"],
"@/pages/*": ["pages/*"],
"@/styles/*": ["styles/*"],
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "types.d.ts"],
"exclude": ["node_modules"]
}
declare module '*module.css' {
const styles: {
[className: string]: string
}
export default styles
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment