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

Update with-jest packages and docs #28209

Merged
merged 12 commits into from Aug 26, 2021
27 changes: 19 additions & 8 deletions docs/testing.md
@@ -1,3 +1,7 @@
---
description: Learn how to set up Next.js with three commonly used testing tools — Cypress, Jest and React Testing Library.
delbaoliveira marked this conversation as resolved.
Show resolved Hide resolved
---

# Testing

<details open>
Expand Down Expand Up @@ -135,7 +139,7 @@ You can learn more about Cypress and Continuous Integration from these resources

## Jest and React Testing Library

Jest and React Testing Library are frequently used together for Unit Testing.
Jest and React Testing Library are frequently used together for **Unit Testing**.

### Quickstart

Expand Down Expand Up @@ -167,21 +171,21 @@ module.exports = {
'!**/node_modules/**',
],
moduleNameMapper: {
// Handle CSS imports (with CSS modules)
// https://jestjs.io/docs/webpack#mocking-css-modules
/* Handle CSS imports (with CSS modules)
https://jestjs.io/docs/webpack#mocking-css-modules */
'^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',

// Handle CSS imports (without CSS modules)
'^.+\\.(css|sass|scss)$': '<rootDir>/__mocks__/styleMock.js',

// Handle image imports
// https://jestjs.io/docs/webpack#handling-static-assets
/* Handle image imports
https://jestjs.io/docs/webpack#handling-static-assets */
'^.+\\.(jpg|jpeg|png|gif|webp|svg)$': '<rootDir>/__mocks__/fileMock.js',
},
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/.next/'],
transform: {
// Use babel-jest to transpile tests with the next/babel preset
// https://jestjs.io/docs/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object
/* Use babel-jest to transpile tests with the next/babel preset
https://jestjs.io/docs/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object */
'^.+\\.(js|jsx|ts|tsx)$': ['babel-jest', { presets: ['next/babel'] }],
},
transformIgnorePatterns: [
Expand Down Expand Up @@ -277,6 +281,11 @@ For example, we can add a test to check if the `<Index />` component successfull

```jsx
// __tests__/testing-library.js

/**
* @jest-environment jsdom
*/

import React from 'react'
import { render } from '@testing-library/react'
import Index from '../pages/index'
Expand All @@ -294,6 +303,8 @@ describe('App', () => {
})
```

> **Note**: The `@jest-environment jsdom` comment above configures the testing environment as `jsdom` inside 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`.
delbaoliveira marked this conversation as resolved.
Show resolved Hide resolved

Optionally, add a [snapshot test](https://jestjs.io/docs/snapshot-testing) to keep track of any unexpected changes to your `<Index />` component:

```jsx
Expand Down Expand Up @@ -333,6 +344,6 @@ For more information on what to read next, we recommend:
<div class="card">
<a href="/docs/basic-features/environment-variables#test-environment-variable.md">
<b>Test Environment Variables</b>
<small>Learn more test environments.</small>
<small>Learn more about the test environment.</small>
delbaoliveira marked this conversation as resolved.
Show resolved Hide resolved
</a>
</div>
4 changes: 4 additions & 0 deletions examples/with-jest/__tests__/testing-library.js
@@ -1,3 +1,7 @@
/**
* @jest-environment jsdom
*/

import React from 'react'
import { render } from '@testing-library/react'
import Index from '../pages/index'
Expand Down
8 changes: 4 additions & 4 deletions examples/with-jest/package.json
Expand Up @@ -13,11 +13,11 @@
"react-dom": "^17.0.2"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.1.0",
"@testing-library/react": "^9.4.0",
"babel-jest": "^25.1.0",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^12.0.0",
"babel-jest": "^27.0.6",
"identity-obj-proxy": "^3.0.0",
"jest": "^25.1.0",
"jest": "^27.0.6",
"react-test-renderer": "^17.0.2"
}
}