The goal of this project is to practice testing hooks since they work a bit different than components.
The code for this project is identical to the code for our useLocalStorage
custom hook project.
- Testing the following features of the
useLocalStorage
hook:- Ensure the initial value passed to the
useLocalStorage
hook is stored inlocalStorage
. This should also work with passing a function touseLocalStorage
as well. - Ensure
localStorage
is updated wheneversetValue
is called. - Ensure
localStorage
is cleared wheneversetValue
is called with undefined. - Ensure
useLocalStorage
uses the value fromlocalStorage
if it exists instead of the initial value passed touseLocalStorage
.
- Ensure the initial value passed to the
-
TypeError: Cannot convert undefined or null to object in expect.extend(matchers) setUpTests.js
- make sure in package.json if you have creatd new Vit + react project or use the project package.json
"devDependencies": { "@testing-library/jest-dom": "^5.16.5", **is this version "@testing-library/react": "^14.0.0", "@testing-library/user-event": "^14.4.3", "@types/react": "^18.0.27", "@types/react-dom": "^18.0.10", "@vitejs/plugin-react-swc": "^3.0.0", "jsdom": "^22.1.0", "vite": "^4.1.0", "vitest": "^0.32.2" **is this version }
To run tests, run the following command
npm run test
Install my-project with npm
cd my-project
npm install