Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use recommended pattern in testing example #28404

Merged
merged 11 commits into from
Aug 25, 2021
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ examples/with-typescript-eslint-jest/**
examples/with-kea/**
examples/with-custom-babel-config/**
examples/with-flow/**
examples/with-jest/**
examples/with-mobx-state-tree/**
examples/with-mobx/**
packages/next/bundles/webpack/packages/*.runtime.js
Expand Down
13 changes: 7 additions & 6 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ module.exports = {
'^.+\\.(jpg|jpeg|png|gif|webp|svg)$': '<rootDir>/__mocks__/fileMock.js',
},
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/.next/'],
testEnvironment: 'jsdom',
htunnicliff marked this conversation as resolved.
Show resolved Hide resolved
transform: {
// Use babel-jest to transpile tests with the next/babel preset
// https://jestjs.io/docs/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object
Expand Down Expand Up @@ -276,16 +277,16 @@ Your project is now ready to run tests. Follow Jests convention by adding tests
For example, we can add a test to check if the `<Index />` component successfully renders a heading:

```jsx
// __tests__/testing-library.js
// __tests__/index.test.jsx
import React from 'react'
import { render } from '@testing-library/react'
import Index from '../pages/index'
import { render, screen } from '@testing-library/react'
import Home from '../pages/index'

describe('App', () => {
describe('Home', () => {
it('renders a heading', () => {
const { getByRole } = render(<Index />)
render(<Home />)

const heading = getByRole('heading', {
const heading = screen.getByRole('heading', {
name: /welcome to next\.js!/i,
})

Expand Down
15 changes: 15 additions & 0 deletions examples/with-jest/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"root": true,
"extends": ["next/core-web-vitals"],
"plugins": ["testing-library"],
"overrides": [
// Only uses Testing Library lint rules in test files
{
"files": [
"**/__tests__/**/*.[jt]s?(x)",
"**/?(*.)+(spec|test).[jt]s?(x)"
],
"extends": ["plugin:testing-library/react"]
}
]
}
19 changes: 19 additions & 0 deletions examples/with-jest/__tests__/index.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @jest-environment jsdom
*/

import React from 'react'
import { render, screen } from '@testing-library/react'
import Home from '../pages/index'

describe('Home', () => {
it('renders a heading', () => {
render(<Home />)

const heading = screen.getByRole('heading', {
name: /welcome to next\.js!/i,
})

expect(heading).toBeInTheDocument()
})
})
15 changes: 0 additions & 15 deletions examples/with-jest/__tests__/testing-library.js

This file was deleted.

17 changes: 11 additions & 6 deletions examples/with-jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"private": true,
"scripts": {
"dev": "next dev",
"lint": "next lint",
"build": "next build",
"start": "next start",
"test": "jest --watch",
Expand All @@ -13,11 +14,15 @@
"react-dom": "^17.0.2"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.1.0",
"@testing-library/react": "^9.4.0",
"babel-jest": "^25.1.0",
"identity-obj-proxy": "^3.0.0",
"jest": "^25.1.0",
"react-test-renderer": "^17.0.2"
"@testing-library/jest-dom": "5.14.1",
"@testing-library/react": "12.0.0",
"@testing-library/user-event": "13.2.1",
"babel-jest": "27.0.6",
"eslint": "7.32.0",
"eslint-config-next": "latest",
"eslint-plugin-testing-library": "4.11.0",
"identity-obj-proxy": "3.0.0",
"jest": "27.0.6",
"react-test-renderer": "17.0.2"
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
"git-reset": "git reset --hard HEAD",
"git-clean": "git clean -d -x -e node_modules -e packages -f",
"lint-typescript": "lerna run typescript",
"lint-eslint": "eslint . --ext js,jsx,ts,tsx --max-warnings=0",
"lint-eslint": "eslint . --ext js,jsx,ts,tsx --max-warnings=0 --config .eslintrc.json --no-eslintrc",
"lint-no-typescript": "run-p prettier-check lint-eslint",
"lint": "run-p test-types lint-typescript prettier-check lint-eslint lint-language",
"lint-fix": "yarn prettier-fix && eslint . --ext js,jsx,ts,tsx --fix --max-warnings=0",
"lint-fix": "yarn prettier-fix && eslint . --ext js,jsx,ts,tsx --fix --max-warnings=0 --config .eslintrc.json --no-eslintrc",
"lint-language": "alex .",
"prettier-check": "prettier --check .",
"prettier-fix": "prettier --write .",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export default () => (
<FakeA id="with-href">Will redirect as an `a` tag</FakeA>
</Link>

{/* eslint-disable-next-line @next/next/link-passhref */}
<Link href="/nav">
<FakeA id="without-href">Will not redirect as an `a` tag</FakeA>
</Link>
Expand Down
2 changes: 0 additions & 2 deletions test/integration/document-head-warnings/pages/_document.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable @next/next/no-title-in-document-head */

import Document, { Html, Head, Main, NextScript } from 'next/document'

class MyDocument extends Document {
Expand Down