Add testing

This commit is contained in:
DefectingCat
2022-01-14 10:38:17 +08:00
parent 3f27989654
commit 2cbcf99876
9 changed files with 2011 additions and 46 deletions

4
.prettierrc.json Normal file
View File

@ -0,0 +1,4 @@
{
"semi": true,
"singleQuote": true
}

2
__mocks__/fileMock.ts Normal file
View File

@ -0,0 +1,2 @@
/* eslint-disable import/no-anonymous-default-export */
export default 'test-file-stub';

2
__mocks__/styleMock.ts Normal file
View File

@ -0,0 +1,2 @@
/* eslint-disable import/no-anonymous-default-export */
export default {};

View File

@ -0,0 +1,16 @@
/**
* @jest-environment jsdom
*/
import { render, screen } from '@testing-library/react';
import Home from 'pages/index';
describe('Home', () => {
it('renders a heading', () => {
render(<Home />);
const heading = screen.getByText(/hello/i);
expect(heading).toBeInTheDocument();
});
});

18
jest.config.js Normal file
View File

@ -0,0 +1,18 @@
// jest.config.js
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: './',
});
// Add any custom config to be passed to Jest
const customJestConfig = {
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
// if using TypeScript with a baseUrl set to the root directory then you need the below for alias' to work
moduleDirectories: ['node_modules', '<rootDir>/'],
testEnvironment: 'jest-environment-jsdom',
};
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(customJestConfig);

6
jest.setup.js Normal file
View File

@ -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';

7
layouts/MainLayout.tsx Normal file
View File

@ -0,0 +1,7 @@
import { FC } from 'react';
const MainLayout: FC = () => {
return <></>;
};
export default MainLayout;

View File

@ -5,7 +5,9 @@
"dev": "next dev", "dev": "next dev",
"build": "next build", "build": "next build",
"start": "next start", "start": "next start",
"lint": "next lint" "lint": "next lint",
"test": "jest --watch",
"test:coverage": "CI=true yarn test --env=jsdom --coverage"
}, },
"dependencies": { "dependencies": {
"@mdx-js/loader": "^1.6.22", "@mdx-js/loader": "^1.6.22",
@ -16,6 +18,9 @@
"react-dom": "^17.0.2" "react-dom": "^17.0.2"
}, },
"devDependencies": { "devDependencies": {
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/react": "^12.1.2",
"@types/jest": "^27.4.0",
"@types/mdx-js__react": "^1.5.5", "@types/mdx-js__react": "^1.5.5",
"@types/node": "17.0.8", "@types/node": "17.0.8",
"@types/react": "17.0.38", "@types/react": "17.0.38",
@ -23,6 +28,7 @@
"classnames": "^2.3.1", "classnames": "^2.3.1",
"eslint": "8.6.0", "eslint": "8.6.0",
"eslint-config-next": "12.0.8", "eslint-config-next": "12.0.8",
"jest": "^27.4.7",
"postcss": "^8.4.5", "postcss": "^8.4.5",
"tailwindcss": "^3.0.13", "tailwindcss": "^3.0.13",
"typescript": "4.5.4" "typescript": "4.5.4"

1994
yarn.lock

File diff suppressed because it is too large Load Diff