Skip to content

Commit

Permalink
feat!: add Jest and React testing library
Browse files Browse the repository at this point in the history
  • Loading branch information
ixartz committed Apr 27, 2022
1 parent 7f9cf52 commit e182b87
Show file tree
Hide file tree
Showing 6 changed files with 11,002 additions and 4,662 deletions.
4 changes: 0 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
{
"editor.tabSize": 2,
"editor.detectIndentation": false,
"jest.autoRun": {
"watch": false,
"onSave": "test-file"
},
"search.exclude": {
"package-lock.json": true
},
Expand Down
22 changes: 22 additions & 0 deletions __tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { render, screen } from '@testing-library/react';

import About from '@/pages/about';

// The easiest solution to mock `next/router`: https://github.com/vercel/next.js/issues/7479
jest.mock('next/router', () => ({
useRouter() {
return {
basePath: '.',
};
},
}));

describe('About page', () => {
it('should render the About page', () => {
render(<About />);

const paragraph = screen.getAllByText(/Lorem ipsum/i);

expect(paragraph.length).toEqual(2);
});
});
19 changes: 19 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const nextJest = require('next/jest');

const createJestConfig = nextJest({
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment
dir: './',
});

const customJestConfig = {
setupFilesAfterEnv: ['./jest.setup.js'],
moduleNameMapper: {
// Handle module aliases (this will be automatically configured for you soon)
'^@/(.*)$': '<rootDir>/src/$1',

'^@/public/(.*)$': '<rootDir>/public/$1',
},
testEnvironment: 'jest-environment-jsdom',
};

module.exports = createJestConfig(customJestConfig);
1 change: 1 addition & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@testing-library/jest-dom/extend-expect';
Loading

0 comments on commit e182b87

Please sign in to comment.