Skip to content

Commit 6a32d85

Browse files
Update with-jest example (#27894)
## Documentation / Examples - [x] Make sure the linting passes This PR updates our current with-jest example to match the examples in upcoming docs, add module alias configuration and be more consistent with Jest conventions.
1 parent 9d3e895 commit 6a32d85

File tree

10 files changed

+51
-32
lines changed

10 files changed

+51
-32
lines changed

examples/with-jest/.babelrc

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'test-file-stub'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Mock CSS files.
2+
// If you're using CSS modules, this file can be deleted.
3+
4+
module.exports = {}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ exports[`renders homepage unchanged 1`] = `
88
<h1
99
className="title"
1010
>
11-
Welcome to
11+
Welcome to
1212
<a
1313
href="https://nextjs.org"
1414
>
@@ -18,7 +18,7 @@ exports[`renders homepage unchanged 1`] = `
1818
<p
1919
className="description"
2020
>
21-
Get started by editing
21+
Get started by editing
2222
<code>
2323
pages/index.js
2424
</code>

examples/with-jest/__tests__/testing-library.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ import React from 'react'
22
import { render } from '@testing-library/react'
33
import Index from '../pages/index'
44

5-
test('renders deploy link', () => {
6-
const { getByText } = render(<Index />)
7-
const linkElement = getByText(
8-
/Instantly deploy your Next\.js site to a public URL with Vercel\./
9-
)
10-
expect(linkElement).toBeInTheDocument()
5+
describe('App', () => {
6+
it('renders a heading', () => {
7+
const { getByRole } = render(<Index />)
8+
9+
const heading = getByRole('heading', {
10+
name: /welcome to next\.js!/i,
11+
})
12+
13+
expect(heading).toBeInTheDocument()
14+
})
1115
})

examples/with-jest/config/jest/cssTransform.js

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

examples/with-jest/jest.config.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,30 @@ module.exports = {
44
'!**/*.d.ts',
55
'!**/node_modules/**',
66
],
7-
setupFilesAfterEnv: ['<rootDir>/setupTests.js'],
8-
testPathIgnorePatterns: ['/node_modules/', '/.next/'],
7+
moduleNameMapper: {
8+
// Handle CSS imports (with CSS modules)
9+
// https://jestjs.io/docs/webpack#mocking-css-modules
10+
'^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
11+
12+
// Handle CSS imports (without CSS modules)
13+
'^.+\\.(css|sass|scss)$': '<rootDir>/__mocks__/styleMock.js',
14+
15+
// Handle image imports
16+
// https://jestjs.io/docs/webpack#handling-static-assets
17+
'^.+\\.(jpg|jpeg|png|gif|webp|svg)$': `<rootDir>/__mocks__/fileMock.js`,
18+
19+
// Handle module aliases
20+
'^@/components/(.*)$': '<rootDir>/components/$1',
21+
},
22+
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
23+
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/.next/'],
924
transform: {
10-
'^.+\\.(js|jsx|ts|tsx)$': '<rootDir>/node_modules/babel-jest',
11-
'^.+\\.css$': '<rootDir>/config/jest/cssTransform.js',
25+
// Use babel-jest to transpile tests with the next/babel preset
26+
// https://jestjs.io/docs/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object
27+
'^.+\\.(js|jsx|ts|tsx)$': ['babel-jest', { presets: ['next/babel'] }],
1228
},
1329
transformIgnorePatterns: [
1430
'/node_modules/',
1531
'^.+\\.module\\.(css|sass|scss)$',
1632
],
17-
moduleNameMapper: {
18-
'^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
19-
},
2033
}

examples/with-jest/jest.setup.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Optional: configure or set up a testing framework before each test.
2+
// If you delete this file, remove `setupFilesAfterEnv` from `jest.config.js`
3+
4+
// Used for __tests__/testing-library.js
5+
// Learn more: https://github.com/testing-library/jest-dom
6+
import '@testing-library/jest-dom/extend-expect'

examples/with-jest/jsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": ".",
4+
"paths": {
5+
"@/components/*": ["components/*"]
6+
}
7+
}
8+
}

examples/with-jest/setupTests.js

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

0 commit comments

Comments
 (0)