Unverified Commit 6a32d854 authored by Delba de Oliveira's avatar Delba de Oliveira Committed by GitHub

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.
parent 9d3e8951
{
"presets": ["next/babel"]
}
module.exports = 'test-file-stub'
// Mock CSS files.
// If you're using CSS modules, this file can be deleted.
module.exports = {}
...@@ -8,7 +8,7 @@ exports[`renders homepage unchanged 1`] = ` ...@@ -8,7 +8,7 @@ exports[`renders homepage unchanged 1`] = `
<h1 <h1
className="title" className="title"
> >
Welcome to Welcome to
<a <a
href="https://nextjs.org" href="https://nextjs.org"
> >
...@@ -18,7 +18,7 @@ exports[`renders homepage unchanged 1`] = ` ...@@ -18,7 +18,7 @@ exports[`renders homepage unchanged 1`] = `
<p <p
className="description" className="description"
> >
Get started by editing Get started by editing
<code> <code>
pages/index.js pages/index.js
</code> </code>
......
...@@ -2,10 +2,14 @@ import React from 'react' ...@@ -2,10 +2,14 @@ import React from 'react'
import { render } from '@testing-library/react' import { render } from '@testing-library/react'
import Index from '../pages/index' import Index from '../pages/index'
test('renders deploy link', () => { describe('App', () => {
const { getByText } = render(<Index />) it('renders a heading', () => {
const linkElement = getByText( const { getByRole } = render(<Index />)
/Instantly deploy your Next\.js site to a public URL with Vercel\./
) const heading = getByRole('heading', {
expect(linkElement).toBeInTheDocument() name: /welcome to next\.js!/i,
})
expect(heading).toBeInTheDocument()
})
}) })
module.exports = {
process() {
return 'module.exports = {};'
},
getCacheKey() {
return 'cssTransform'
},
}
...@@ -4,17 +4,30 @@ module.exports = { ...@@ -4,17 +4,30 @@ module.exports = {
'!**/*.d.ts', '!**/*.d.ts',
'!**/node_modules/**', '!**/node_modules/**',
], ],
setupFilesAfterEnv: ['<rootDir>/setupTests.js'], moduleNameMapper: {
testPathIgnorePatterns: ['/node_modules/', '/.next/'], // 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: { transform: {
'^.+\\.(js|jsx|ts|tsx)$': '<rootDir>/node_modules/babel-jest', // Use babel-jest to transpile tests with the next/babel preset
'^.+\\.css$': '<rootDir>/config/jest/cssTransform.js', // https://jestjs.io/docs/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object
'^.+\\.(js|jsx|ts|tsx)$': ['babel-jest', { presets: ['next/babel'] }],
}, },
transformIgnorePatterns: [ transformIgnorePatterns: [
'/node_modules/', '/node_modules/',
'^.+\\.module\\.(css|sass|scss)$', '^.+\\.module\\.(css|sass|scss)$',
], ],
moduleNameMapper: {
'^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
},
} }
// 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'
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/components/*": ["components/*"]
}
}
}
// 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'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment