You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/testing.md
+71-33Lines changed: 71 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
---
2
-
description: Learn how to set up Next.js with three commonly used testing tools — Cypress, Jest, and React Testing Library.
2
+
description: Learn how to set up Next.js with three commonly used testing tools — Cypress, Playwright, Jest, and React Testing Library.
3
3
---
4
4
5
5
# Testing
@@ -243,55 +243,96 @@ You can learn more about Playwright and Continuous Integration from these resour
243
243
244
244
## Jest and React Testing Library
245
245
246
-
Jest and React Testing Library are frequently used together for **Unit Testing**.
246
+
Jest and React Testing Library are frequently used together for **Unit Testing**. There are three ways you can start using Jest within your Next.js application:
247
+
248
+
1. Using one of our [quickstart examples](https://nextjs.org/docs/testing#quickstart-2)
249
+
2. With the [Next.js Rust Compiler](https://nextjs.org/docs/testing#setting-up-jest-with-the-rust-compiler)
250
+
3. With [Babel](https://nextjs.org/docs/testing#setting-up-jest-with-babel)
251
+
252
+
The following sections will go through how you can set up Jest with each of these options:
247
253
248
254
### Quickstart
249
255
250
-
You can use `create-next-app` with the [with-jest example](https://github.com/vercel/next.js/tree/canary/examples/with-jest) to quickly get started with Jest and React Testing Library:
256
+
You can use `create-next-app` with the [with-jest](https://github.com/vercel/next.js/tree/canary/examples/with-jest) example to quickly get started with Jest and React Testing Library:
Since the release of [Next.js 12](https://nextjs.org/blog/next-12), Next.js now has built-in configuration for Jest.
257
265
258
-
To manually set up Jest and React Testing Library, install `jest` , `@testing-library/react`, `@testing-library/jest-dom`as well as some supporting packages:
266
+
To set up Jest, install `jest` , `@testing-library/react`, `@testing-library/jest-dom`and `react-test-renderer`:
259
267
260
268
```bash
261
-
npm install --save-dev jest babel-jest @testing-library/react @testing-library/jest-dom identity-obj-proxy react-test-renderer
269
+
npm install --save-dev jest @testing-library/react @testing-library/jest-dom react-test-renderer
262
270
```
263
271
264
-
**Configuring Jest**
265
-
266
-
Create a `jest.config.js` file in your project's root directory and add the following configuration options:
272
+
Create a `jest.config.js` file in your project's root directory and add the following:
267
273
268
274
```jsx
269
275
// jest.config.js
276
+
constnextJest=require('next/jest')
277
+
278
+
constcreateJestConfig=nextJest({
279
+
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment
280
+
dir:'./',
281
+
})
282
+
283
+
// Add any custom config to be passed to Jest
284
+
constcustomJestConfig= {
285
+
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
286
+
}
287
+
288
+
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
289
+
module.exports=createJestConfig(customJestConfig)
290
+
```
291
+
292
+
Under the hood, `next/jest` is automatically configuring Jest for you, including:
293
+
294
+
- Setting up `transform` using [SWC](https://nextjs.org/docs/advanced-features/compiler)
295
+
- Auto mocking stylesheets (`.css`, `.module.css`, and their scss variants) and image imports
296
+
- Loading `.env` (and all variants) into `process.env`
297
+
- Ignoring `node_modules` from test resolving and transforms
298
+
- Ignoring `.next` from test resolving
299
+
- Loading `next.config.js` for flags that enable SWC transforms
300
+
301
+
### Setting up Jest (with Babel)
302
+
303
+
If you opt-out of the [Rust Compiler](https://nextjs.org/docs/advanced-features/compiler), you will need to manually configure Jest and install `babel-jest` and `identity-obj-proxy` in addition to the packages above.
270
304
305
+
Here are the recommended options to configure Jest for Next.js:
You can learn more about each option above in the [Jest docs](https://jestjs.io/docs/configuration).
345
+
You can learn more about each configuration option in the [Jest docs](https://jestjs.io/docs/configuration).
305
346
306
347
**Handling stylesheets and image imports**
307
348
308
-
These files aren't useful in tests but importing them may cause errors, so we will need to mock them. Create the mock files we referenced in the configuration above - `fileMock.js` and `styleMock.js` - inside a `__mocks__` directory:
349
+
Styleheets and images aren't used in the tests but importing them may cause errors, so they will need to be mocked. Create the mock files referenced in the configuration above - `fileMock.js` and `styleMock.js` - inside a `__mocks__` directory:
309
350
310
-
```json
351
+
```js
311
352
// __mocks__/fileMock.js
312
-
313
-
(module.exports = "test-file-stub")
353
+
module.exports='test-file-stub'
314
354
```
315
355
316
-
```json
356
+
```js
317
357
// __mocks__/styleMock.js
318
-
319
-
module.exports = {};
358
+
module.exports= {}
320
359
```
321
360
322
361
If you're running into the issue `"Failed to parse src "test-file-stub" on 'next/image'"`, add a '/' to your fileMock.
323
362
324
-
```json
363
+
```js
325
364
// __mocks__/fileMock.js
326
-
327
-
(module.exports = "/test-file-stub")
365
+
module.exports='/test-file-stub'
328
366
```
329
367
330
368
For more information on handling static assets, please refer to the [Jest Docs](https://jestjs.io/docs/webpack#handling-static-assets).
331
369
332
-
**Extend Jest with custom matchers**
370
+
**Optional: Extend Jest with custom matchers**
333
371
334
372
`@testing-library/jest-dom` includes a set of convenient [custom matchers](https://github.com/testing-library/jest-dom#custom-matchers) such as `.toBeInTheDocument()` making it easier to write tests. You can import the custom matchers for every test by adding the following option to the Jest configuration file:
335
373
336
-
```json
374
+
```js
337
375
// jest.config.js
338
-
339
376
setupFilesAfterEnv: ['<rootDir>/jest.setup.js']
340
377
```
341
378
342
379
Then, inside `jest.setup.js`, add the following import:
343
380
344
381
```jsx
345
382
// jest.setup.js
346
-
347
383
import'@testing-library/jest-dom/extend-expect'
348
384
```
349
385
350
386
If you need to add more setup options before each test, it's common to add them to the `jest.setup.js` file above.
351
387
352
-
**Absolute Imports and Module Path Aliases**
388
+
**Optional: Absolute Imports and Module Path Aliases**
353
389
354
390
If your project is using [Module Path Aliases](https://nextjs.org/docs/advanced-features/module-path-aliases), you will need to configure Jest to resolve the imports by matching the paths option in the `jsconfig.json` file with the `moduleNameMapper` option in the `jest.config.js` file. For example:
355
391
@@ -372,6 +408,8 @@ moduleNameMapper: {
372
408
}
373
409
```
374
410
411
+
### Creating your tests:
412
+
375
413
**Add a test script to package.json**
376
414
377
415
Add the Jest executable in watch mode to the `package.json` scripts:
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.
4
+
5
+
This includes Next.js' built-in support for Global CSS, CSS Modules, and TypeScript!
6
+
7
+
## How to Use
8
+
9
+
Quickly get started using [Create Next App](https://github.com/vercel/next.js/tree/canary/packages/create-next-app#readme)!
0 commit comments