npm install --global yarn
yarn --version
npx create-next-app@12.3.2 .
yarn add next@12.3.2
yarn add -D jest jest-environment-jsdom @testing-library/react @testing-library/jest-dom jest-css-modules
touch jest.config.js
const nextJest = require('next/jest')
const createJestConfig = nextJest({
dir: './',
})
const customJestConfig = {
moduleDirectories: ['node_modules', '<rootDir>/'],
testEnvironment: 'jest-environment-jsdom',
}
module.exports = createJestConfig(customJestConfig)
"scripts": {
...
"test": "jest --watch"
},
touch .prettierrc
{
"singleQuote": true,
"semi": false
}
import { render, screen } from '@testing-library/react'
import '@testing-library/jest-dom'
import Home from '../pages/index'
it('Should render title text', () => {
render(<Home />)
expect(screen.getByText('Next.js!')).toBeInTheDocument()
})
PASS __tests__/Home.test.js
✓ Should render hello text (20 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.728 s, estimated 2 s