app.js - Main application entry point. It is responsible for setting up the express application and starting the server.api.js - Contains route definitions for our API endpoints.products.js - Contains the Products model and business logic for CRUD operations on products.orders.js - Contains the Orders model and business logic for CRUD operations on orders.--save-dev flag indicates that this is dependency that should only be available during the development build of the application. We are telling NPM that this is not a dependency that will be required by the production or final build of the application.supertest. This package is often used for making HTTP test assertions.app.test.js. This file will reside in the tests directory. Where to place test files is often up to the project maintainer. Some developers choose to place their tests alongside the domain code, others choose to place them in a separate directory. For the simplicity of this lab, we will place them in a separate directory.app.test.js file:200. If an error is encountered, that error will be notified to the console.beforeAll callback is ran before each test in the test suite is run, this ensures that the previous job is done, and any dangling services have been shut down./orders route. It should look very similar to the /products route test we just wrote. We want to check that the route exists, and that it returns a 200 response.Products or Orders modules from the API controller, our tests would fail. We'd have no way to verify that our Products or Orders modules worked correctly. So instead, let's begin to write some tests for the Products module.tests/products.test.js. This file will be used to house our Products tests. In the file, we'll need to establish our test suite and setup some config:npm run test again to verify that your test suite works. You should see an output simialr to this:tests/db.mock.js. Next add the following to the file:product.test.js file so that it uses our mocks instead of the actual database module.npm run test. You should see all tests passing! What is happening here is instead of using the mongoose database module that we normally use, we are replacing it with our jest mock objects that exist in the tests/db.mock.js file. So when we call .sort() or .find() on the db module, instead of the "real" mongoose methods being called, we are using the ones we configured in db.mock.js. This allows us to produce stateless deterministic test results, and not worry about our delete or create tests modifying the database.orders.npm install @shelf/jest-mongodb --save-dev. This will install the Jest MongoDB package.jest.config.js file and uncomment or add the following:tests/orders.test.js. Because orders require products, we'll need to do a bit of setup first.npm run test you'll see it fail. That is expected! We've not created any orders in the databse, we're requesting orders, but nothing has been created. Let's remedy this with a quick tests to create an order:npm run test again and we should see a successful pass of all our tests!orders.test.js suite. This task will require you calling the get method, using the createdOrder._idorders.test.js suite. You'll want to create a change on the order, and call that change:editedOrder exists or is defined, and that the change which was made exists on the new editedOrder object.products.test.js suite. You'll want to create a mock response for the get method:products.test.js suite. You'll want to create a mock response for the delete method. Use the template provided above:Posted May 5, 2025
Developed a full-stack e-commerce application using Node.js, Express, and MongoDB for managing products and orders with a backend that supports CRUD operations
0
0