- Run
npm install
on both./notes-app-client
and./notes-app-backend
directories. - Set up an
.env
file modeled after the.env.example
and fill in the variables. - Start the backend by running
npm run dev
inside the./notes-app-backend
directory. - Start the frontend by running
npm run dev
inside the./notes-app-client
directory. - Manually test the application.
- Like and subscribe.
The tests are implemented using the jest
and supertest
libraries.
The tests are an integration test suite that runs the API endpoints to ensure the correct behavior of the CRUD functionality.
-
- Create a MongoDB testing database.
Create a MongoDB test DB that is separate from your production DB. You can run it locally, on a container, or on an external MongoDB provider such as
https://www.mongodb.com/
. The important thing is that you get the DB's URI to connect to it.
- Create a MongoDB testing database.
Create a MongoDB test DB that is separate from your production DB. You can run it locally, on a container, or on an external MongoDB provider such as
-
- Add your testing database URL in the
.env
file as in the example below. Make sure it's different from your production database.
- Add your testing database URL in the
TEST_URI="Your_mongo_db_url"
Run npm run test
.
Whenever a route is added, create a new spec file to test it in the tests
directory. For example, if your new route is called users
, then you should make a users.spec.js
file.
If you add an endpoint, make sure you add tests for it in the route's corresponding spec file.
If using a common DB shared between developers, ensure that your tests clear up the database after running every time. Use the afterAll
jest function for this.
Every endpoint should be thoroughly tested. Here are things to look for:
-
CRUD Functionality
- Create endpoints should test the successful creation of new entries.
- Update should update (only) the specified fields and keep the untouched ones as they are.
- Delete endpoint tests should delete and ensure the removal of the element from the DB.
- Get (single element) endpoints should return the correct element.
- List (get multiple elements) endpoints should return a list of elements or an empty array if nothing is found.
-
Graceful Handling of Errors
- Create endpoints should gracefully fail if there's an attempt to create an existing unique element.
- Update should gracefully fail if the element being updated doesn't exist.
- Delete endpoint should fail gracefully if the element to be removed can't be found.
- Get (single element) endpoints should fail gracefully if the requested element can't be found.
-
Edge Cases
- Tests should include as many edge cases as possible.
- Tests should handle unexpected payloads gracefully.
Make sure you have Node.js installed
then:
npm install --save-dev @testing-library/react @testing-library/jest-dom jest jest-environment-jsdom @babel/preset-env @babel/preset-react
The file package.json should be extended as follows:
{
"scripts": {
// ...
"test": "jest"
}
// ...
"jest": {
"testEnvironment": "jsdom"
}
}
We also need the file .babelrc with following content:
{
"presets": [
"@babel/preset-env",
["@babel/preset-react", { "runtime": "automatic" }]
]
}
Create a test directory in your project or alongside your components and name your test files with the .test.js extension. For example, if your component is in src/components/Sidebar.js, create a test file named Sidebar.test.js
Execute the following command:
npm test