Skip to content

Commit

Permalink
Update with-jest example (#27894)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
delbaoliveira committed Aug 9, 2021
1 parent 9d3e895 commit 6a32d85
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 32 deletions.
3 changes: 0 additions & 3 deletions examples/with-jest/.babelrc

This file was deleted.

1 change: 1 addition & 0 deletions examples/with-jest/__mocks__/fileMock.js
@@ -0,0 +1 @@
module.exports = 'test-file-stub'
4 changes: 4 additions & 0 deletions examples/with-jest/__mocks__/styleMock.js
@@ -0,0 +1,4 @@
// Mock CSS files.
// If you're using CSS modules, this file can be deleted.

module.exports = {}
4 changes: 2 additions & 2 deletions examples/with-jest/__tests__/__snapshots__/snapshot.js.snap
Expand Up @@ -8,7 +8,7 @@ exports[`renders homepage unchanged 1`] = `
<h1
className="title"
>
Welcome to
Welcome to
<a
href="https://nextjs.org"
>
Expand All @@ -18,7 +18,7 @@ exports[`renders homepage unchanged 1`] = `
<p
className="description"
>
Get started by editing
Get started by editing
<code>
pages/index.js
</code>
Expand Down
16 changes: 10 additions & 6 deletions examples/with-jest/__tests__/testing-library.js
Expand Up @@ -2,10 +2,14 @@ import React from 'react'
import { render } from '@testing-library/react'
import Index from '../pages/index'

test('renders deploy link', () => {
const { getByText } = render(<Index />)
const linkElement = getByText(
/Instantly deploy your Next\.js site to a public URL with Vercel\./
)
expect(linkElement).toBeInTheDocument()
describe('App', () => {
it('renders a heading', () => {
const { getByRole } = render(<Index />)

const heading = getByRole('heading', {
name: /welcome to next\.js!/i,
})

expect(heading).toBeInTheDocument()
})
})
8 changes: 0 additions & 8 deletions examples/with-jest/config/jest/cssTransform.js

This file was deleted.

27 changes: 20 additions & 7 deletions examples/with-jest/jest.config.js
Expand Up @@ -4,17 +4,30 @@ module.exports = {
'!**/*.d.ts',
'!**/node_modules/**',
],
setupFilesAfterEnv: ['<rootDir>/setupTests.js'],
testPathIgnorePatterns: ['/node_modules/', '/.next/'],
moduleNameMapper: {
// 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
'^.+\\.(jpg|jpeg|png|gif|webp|svg)$': `<rootDir>/__mocks__/fileMock.js`,

// Handle module aliases
'^@/components/(.*)$': '<rootDir>/components/$1',
},
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/.next/'],
transform: {
'^.+\\.(js|jsx|ts|tsx)$': '<rootDir>/node_modules/babel-jest',
'^.+\\.css$': '<rootDir>/config/jest/cssTransform.js',
// 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: [
'/node_modules/',
'^.+\\.module\\.(css|sass|scss)$',
],
moduleNameMapper: {
'^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
},
}
6 changes: 6 additions & 0 deletions examples/with-jest/jest.setup.js
@@ -0,0 +1,6 @@
// Optional: configure or set up a testing framework before each test.
// If you delete this file, remove `setupFilesAfterEnv` from `jest.config.js`

// Used for __tests__/testing-library.js
// Learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom/extend-expect'
8 changes: 8 additions & 0 deletions examples/with-jest/jsconfig.json
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/components/*": ["components/*"]
}
}
}
6 changes: 0 additions & 6 deletions examples/with-jest/setupTests.js

This file was deleted.

0 comments on commit 6a32d85

Please sign in to comment.