Skip to content

Commit 48dba64

Browse files
chore: convert Jest examples to TypeScript (#32705)
* chore: convert Jest examples to TypeScript * test: use absolute imports in test files * chore: address review comments * fix: add pages to tsconfig.json paths * fix: add styles to tsconfig.json paths * chore: upgrade dependencies/snapshots * docs: match Testing docs with example code * docs: link to docs from mock test files Co-authored-by: Lee Robinson <me@leerob.io>
1 parent 6581ba9 commit 48dba64

35 files changed

+502
-626
lines changed

docs/testing.md

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,10 @@ npx create-next-app@latest --example with-jest with-jest-app
262262

263263
Since the release of [Next.js 12](https://nextjs.org/blog/next-12), Next.js now has built-in configuration for Jest.
264264

265-
To set up Jest, install `jest` , `@testing-library/react`, `@testing-library/jest-dom` and `react-test-renderer`:
265+
To set up Jest, install `jest` , `@testing-library/react`, `@testing-library/jest-dom`:
266266

267267
```bash
268-
npm install --save-dev jest @testing-library/react @testing-library/jest-dom react-test-renderer
268+
npm install --save-dev jest @testing-library/react @testing-library/jest-dom
269269
```
270270

271271
Create a `jest.config.js` file in your project's root directory and add the following:
@@ -284,6 +284,7 @@ const customJestConfig = {
284284
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
285285
// if using TypeScript with a baseUrl set to the root directory then you need the below for alias' to work
286286
moduleDirectories: ['node_modules', '<rootDir>/'],
287+
testEnvironment: 'jest-environment-jsdom',
287288
}
288289

289290
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
@@ -340,6 +341,7 @@ module.exports = {
340341
'/node_modules/',
341342
'^.+\\.module\\.(css|sass|scss)$',
342343
],
344+
testEnvironment: 'jest-environment-jsdom',
343345
}
344346
```
345347

@@ -430,16 +432,11 @@ Add the Jest executable in watch mode to the `package.json` scripts:
430432

431433
Your project is now ready to run tests. Follow Jests convention by adding tests to the `__tests__` folder in your project's root directory.
432434

433-
For example, we can add a test to check if the `<Index />` component successfully renders a heading:
435+
For example, we can add a test to check if the `<Home />` component successfully renders a heading:
434436

435437
```jsx
436438
// __tests__/index.test.jsx
437439

438-
/**
439-
* @jest-environment jsdom
440-
*/
441-
442-
import React from 'react'
443440
import { render, screen } from '@testing-library/react'
444441
import Home from '../pages/index'
445442

@@ -456,19 +453,17 @@ describe('Home', () => {
456453
})
457454
```
458455

459-
> **Note**: The `@jest-environment jsdom` comment above configures the testing environment as `jsdom` inside the test file because React Testing Library uses DOM elements like `document.body` which will not work in Jest's default `node` testing environment. Alternatively, you can also set the `jsdom` environment globally by adding the Jest configuration option: `"testEnvironment": "jsdom"` in `jest.config.js`.
460-
461-
Optionally, add a [snapshot test](https://jestjs.io/docs/snapshot-testing) to keep track of any unexpected changes to your `<Index />` component:
456+
Optionally, add a [snapshot test](https://jestjs.io/docs/snapshot-testing) to keep track of any unexpected changes to your `<Home />` component:
462457

463458
```jsx
464459
// __tests__/snapshot.js
465-
import React from 'react'
466-
import renderer from 'react-test-renderer'
467-
import Index from '../pages/index'
460+
461+
import { render } from '@testing-library/react'
462+
import Home from '../pages/index'
468463

469464
it('renders homepage unchanged', () => {
470-
const tree = renderer.create(<Index />).toJSON()
471-
expect(tree).toMatchSnapshot()
465+
const { container } = render(<Home />)
466+
expect(container).toMatchSnapshot()
472467
})
473468
```
474469

examples/with-jest-babel/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This example shows how to configure Jest to work with Next.js and Babel. Since the release of Next.js 12, Next.js has in-built configuration for Jest with SWC. See the [with-jest](https://github.com/vercel/next.js/tree/canary/examples/with-jest) example for the latest implementation.
44

5-
This includes Next.js' built-in support for Global CSS, CSS Modules, and TypeScript!
5+
This includes Next.js' built-in support for Global CSS, CSS Modules and TypeScript.
66

77
## How to Use
88

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
// Read more at "Handling stylesheets and image imports" on https://nextjs.org/docs/testing
2+
13
module.exports = 'test-file-stub'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Mock CSS files.
22
// If you're using CSS modules, this file can be deleted.
3+
// Read more at "Handling stylesheets and image imports" on https://nextjs.org/docs/testing
34

45
module.exports = {}

examples/with-jest-babel/__tests__/__snapshots__/snapshot.js.snap

Lines changed: 0 additions & 206 deletions
This file was deleted.

0 commit comments

Comments
 (0)