Unverified Commit 877f9829 authored by Hunter Tunnicliff's avatar Hunter Tunnicliff Committed by GitHub

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: 's avatarjj@jjsweb.site <jj@jjsweb.site>
parent d835402f
...@@ -8,6 +8,7 @@ examples/with-typescript-eslint-jest/** ...@@ -8,6 +8,7 @@ examples/with-typescript-eslint-jest/**
examples/with-kea/** examples/with-kea/**
examples/with-custom-babel-config/** examples/with-custom-babel-config/**
examples/with-flow/** examples/with-flow/**
examples/with-jest/**
examples/with-mobx-state-tree/** examples/with-mobx-state-tree/**
examples/with-mobx/** examples/with-mobx/**
packages/next/bundles/webpack/packages/*.runtime.js packages/next/bundles/webpack/packages/*.runtime.js
......
...@@ -179,6 +179,7 @@ module.exports = { ...@@ -179,6 +179,7 @@ module.exports = {
'^.+\\.(jpg|jpeg|png|gif|webp|svg)$': '<rootDir>/__mocks__/fileMock.js', '^.+\\.(jpg|jpeg|png|gif|webp|svg)$': '<rootDir>/__mocks__/fileMock.js',
}, },
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/.next/'], testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/.next/'],
testEnvironment: 'jsdom',
transform: { transform: {
// Use babel-jest to transpile tests with the next/babel preset // Use babel-jest to transpile tests with the next/babel preset
// https://jestjs.io/docs/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object // 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 ...@@ -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: For example, we can add a test to check if the `<Index />` component successfully renders a heading:
```jsx ```jsx
// __tests__/testing-library.js // __tests__/index.test.jsx
import React from 'react' import React from 'react'
import { render } from '@testing-library/react' import { render, screen } from '@testing-library/react'
import Index from '../pages/index' import Home from '../pages/index'
describe('App', () => { describe('Home', () => {
it('renders a heading', () => { 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, name: /welcome to next\.js!/i,
}) })
......
{
"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"]
}
]
}
/**
* @jest-environment jsdom
*/
import React from 'react' import React from 'react'
import { render } from '@testing-library/react' import { render, screen } from '@testing-library/react'
import Index from '../pages/index' import Home from '../pages/index'
describe('App', () => { describe('Home', () => {
it('renders a heading', () => { 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, name: /welcome to next\.js!/i,
}) })
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "next dev", "dev": "next dev",
"lint": "next lint",
"build": "next build", "build": "next build",
"start": "next start", "start": "next start",
"test": "jest --watch", "test": "jest --watch",
...@@ -13,11 +14,15 @@ ...@@ -13,11 +14,15 @@
"react-dom": "^17.0.2" "react-dom": "^17.0.2"
}, },
"devDependencies": { "devDependencies": {
"@testing-library/jest-dom": "^5.1.0", "@testing-library/jest-dom": "5.14.1",
"@testing-library/react": "^9.4.0", "@testing-library/react": "12.0.0",
"babel-jest": "^25.1.0", "@testing-library/user-event": "13.2.1",
"identity-obj-proxy": "^3.0.0", "babel-jest": "27.0.6",
"jest": "^25.1.0", "eslint": "7.32.0",
"react-test-renderer": "^17.0.2" "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"
} }
} }
...@@ -19,10 +19,10 @@ ...@@ -19,10 +19,10 @@
"git-reset": "git reset --hard HEAD", "git-reset": "git reset --hard HEAD",
"git-clean": "git clean -d -x -e node_modules -e packages -f", "git-clean": "git clean -d -x -e node_modules -e packages -f",
"lint-typescript": "lerna run typescript", "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-no-typescript": "run-p prettier-check lint-eslint",
"lint": "run-p test-types lint-typescript prettier-check lint-eslint lint-language", "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 .", "lint-language": "alex .",
"prettier-check": "prettier --check .", "prettier-check": "prettier --check .",
"prettier-fix": "prettier --write .", "prettier-fix": "prettier --write .",
......
...@@ -18,7 +18,6 @@ export default () => ( ...@@ -18,7 +18,6 @@ export default () => (
<FakeA id="with-href">Will redirect as an `a` tag</FakeA> <FakeA id="with-href">Will redirect as an `a` tag</FakeA>
</Link> </Link>
{/* eslint-disable-next-line @next/next/link-passhref */}
<Link href="/nav"> <Link href="/nav">
<FakeA id="without-href">Will not redirect as an `a` tag</FakeA> <FakeA id="without-href">Will not redirect as an `a` tag</FakeA>
</Link> </Link>
......
/* eslint-disable @next/next/no-title-in-document-head */
import Document, { Html, Head, Main, NextScript } from 'next/document' import Document, { Html, Head, Main, NextScript } from 'next/document'
class MyDocument extends Document { class MyDocument extends Document {
......
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