Skip to content

Commit 877f982

Browse files
htunnicliffijjk
andauthoredAug 25, 2021
Use recommended pattern in testing example (#28404)
* Use recommended pattern in testing example Since the official linter for testing library, `eslint-plugin-testing-library` recommends using `screen` to write queries, this MR updates the testing library example to follow the pattern recommended by the linter. > DOM Testing Library (and other Testing Library frameworks built on top of it) exports a screen object which has every query (and a debug method). This works better with autocomplete and makes each test a little simpler to write and maintain. > This rule aims to force writing tests using built-in queries directly from screen object rather than destructuring them from render result. Given the screen component does not expose utility methods such as rerender() or the container property, it is correct to use the render returned value in those scenarios. See the `prefer-screen-queries` rules docs for more info: https://github.com/testing-library/eslint-plugin-testing-library/blob/main/docs/rules/prefer-screen-queries.md * Update devDependencies * Install and configure test linting * Use recommended pattern in test * Update test names for consistency * Update docs * Set jest environment in each file * Use root true in `with-jest` eslint config * Ensure nested .eslintrcs are not loaded for repo lint Co-authored-by: jj@jjsweb.site <jj@jjsweb.site>
1 parent d835402 commit 877f982

File tree

9 files changed

+55
-32
lines changed

9 files changed

+55
-32
lines changed
 

‎.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ examples/with-typescript-eslint-jest/**
88
examples/with-kea/**
99
examples/with-custom-babel-config/**
1010
examples/with-flow/**
11+
examples/with-jest/**
1112
examples/with-mobx-state-tree/**
1213
examples/with-mobx/**
1314
packages/next/bundles/webpack/packages/*.runtime.js

‎docs/testing.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ module.exports = {
179179
'^.+\\.(jpg|jpeg|png|gif|webp|svg)$': '<rootDir>/__mocks__/fileMock.js',
180180
},
181181
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/.next/'],
182+
testEnvironment: 'jsdom',
182183
transform: {
183184
// Use babel-jest to transpile tests with the next/babel preset
184185
// https://jestjs.io/docs/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object
@@ -276,16 +277,16 @@ Your project is now ready to run tests. Follow Jests convention by adding tests
276277
For example, we can add a test to check if the `<Index />` component successfully renders a heading:
277278

278279
```jsx
279-
// __tests__/testing-library.js
280+
// __tests__/index.test.jsx
280281
import React from 'react'
281-
import { render } from '@testing-library/react'
282-
import Index from '../pages/index'
282+
import { render, screen } from '@testing-library/react'
283+
import Home from '../pages/index'
283284

284-
describe('App', () => {
285+
describe('Home', () => {
285286
it('renders a heading', () => {
286-
const { getByRole } = render(<Index />)
287+
render(<Home />)
287288

288-
const heading = getByRole('heading', {
289+
const heading = screen.getByRole('heading', {
289290
name: /welcome to next\.js!/i,
290291
})
291292

‎examples/with-jest/.eslintrc.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"root": true,
3+
"extends": ["next/core-web-vitals"],
4+
"plugins": ["testing-library"],
5+
"overrides": [
6+
// Only uses Testing Library lint rules in test files
7+
{
8+
"files": [
9+
"**/__tests__/**/*.[jt]s?(x)",
10+
"**/?(*.)+(spec|test).[jt]s?(x)"
11+
],
12+
"extends": ["plugin:testing-library/react"]
13+
}
14+
]
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @jest-environment jsdom
3+
*/
4+
5+
import React from 'react'
6+
import { render, screen } from '@testing-library/react'
7+
import Home from '../pages/index'
8+
9+
describe('Home', () => {
10+
it('renders a heading', () => {
11+
render(<Home />)
12+
13+
const heading = screen.getByRole('heading', {
14+
name: /welcome to next\.js!/i,
15+
})
16+
17+
expect(heading).toBeInTheDocument()
18+
})
19+
})

‎examples/with-jest/__tests__/testing-library.js

-15
This file was deleted.

‎examples/with-jest/package.json

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"private": true,
33
"scripts": {
44
"dev": "next dev",
5+
"lint": "next lint",
56
"build": "next build",
67
"start": "next start",
78
"test": "jest --watch",
@@ -13,11 +14,15 @@
1314
"react-dom": "^17.0.2"
1415
},
1516
"devDependencies": {
16-
"@testing-library/jest-dom": "^5.1.0",
17-
"@testing-library/react": "^9.4.0",
18-
"babel-jest": "^25.1.0",
19-
"identity-obj-proxy": "^3.0.0",
20-
"jest": "^25.1.0",
21-
"react-test-renderer": "^17.0.2"
17+
"@testing-library/jest-dom": "5.14.1",
18+
"@testing-library/react": "12.0.0",
19+
"@testing-library/user-event": "13.2.1",
20+
"babel-jest": "27.0.6",
21+
"eslint": "7.32.0",
22+
"eslint-config-next": "latest",
23+
"eslint-plugin-testing-library": "4.11.0",
24+
"identity-obj-proxy": "3.0.0",
25+
"jest": "27.0.6",
26+
"react-test-renderer": "17.0.2"
2227
}
2328
}

‎package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
"git-reset": "git reset --hard HEAD",
2020
"git-clean": "git clean -d -x -e node_modules -e packages -f",
2121
"lint-typescript": "lerna run typescript",
22-
"lint-eslint": "eslint . --ext js,jsx,ts,tsx --max-warnings=0",
22+
"lint-eslint": "eslint . --ext js,jsx,ts,tsx --max-warnings=0 --config .eslintrc.json --no-eslintrc",
2323
"lint-no-typescript": "run-p prettier-check lint-eslint",
2424
"lint": "run-p test-types lint-typescript prettier-check lint-eslint lint-language",
25-
"lint-fix": "yarn prettier-fix && eslint . --ext js,jsx,ts,tsx --fix --max-warnings=0",
25+
"lint-fix": "yarn prettier-fix && eslint . --ext js,jsx,ts,tsx --fix --max-warnings=0 --config .eslintrc.json --no-eslintrc",
2626
"lint-language": "alex .",
2727
"prettier-check": "prettier --check .",
2828
"prettier-fix": "prettier --write .",

‎test/integration/client-navigation/pages/nav/pass-href-prop.js

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export default () => (
1818
<FakeA id="with-href">Will redirect as an `a` tag</FakeA>
1919
</Link>
2020

21-
{/* eslint-disable-next-line @next/next/link-passhref */}
2221
<Link href="/nav">
2322
<FakeA id="without-href">Will not redirect as an `a` tag</FakeA>
2423
</Link>

‎test/integration/document-head-warnings/pages/_document.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable @next/next/no-title-in-document-head */
2-
31
import Document, { Html, Head, Main, NextScript } from 'next/document'
42

53
class MyDocument extends Document {

0 commit comments

Comments
 (0)
Please sign in to comment.