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
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"
}
}