Simple React Todo Application
- Jest Documentation
- Testing Library Documentation
- How to fetch data with React Hooks
- Typesafe JSON parsing
In order to jsdom to works, se need to mock the matchMedia
. Need to add script on setupTests.js
Object.defineProperty(window, 'matchMedia', {
value: () => {
return {
matches: false,
addListener: () => { },
removeListener: () => { }
};
}
});
Add below to package.json
tp ensure the antd can works with testing.
{
"jest": {
"transformIgnorePatterns": [
"/node_modules/(?!antd|@ant-design|rc-.+?|@babel/runtime).+(js|jsx)$"
],
}
}
Add below to package.json
to ensure the test Jest mock / jest.fn()
can works
{
"jest": {
"resetMocks": false
}
}
Ref: jestjs/jest#9131
Install dependencies
npm install --save typescript @types/node @types/react @types/react-dom @types/jest
Add tsconfig.json
on the project root directory
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"src"
]
}
References: