The purpose of this guided demo is to get you to set up your environment for building out your CRUD API with documentation, tests and of course, your endpoints.
- Initialize your project with the
yarn init
command (This will create apackage.json
file). - You can then run
yarn add
commands to in order to save yournode_modules
. - The packages that we need for this project are:
- express
- body-parser
- mongoose
- mocha
- chai
- chai-http
- morgan --> this is new, we'll chat about it.
- sinon
- Create the following files in your project.
- server.js
- app.js
- documentation.md
- .gitignore
- routes.test.js
- models.test.js
-
In your
package.json
file, underscripts
add the test command:"scripts": { "test": "mocha *.test.js" },
-
This command will allow you to run your tests. If you would like, you can add
"start": "nodemon app.js"
to thescripts
. Keep in mind that whichever file you point the "start" script to is where you'll invoke the Express listen() method (your "entry point"). -
Next, head over to the
server.js
file that you created, and build out the boilerplate code for your Node server.
-
Now we're going to set up our testing environment. Head over to your
routes.test.js
file, and requiremongoose
,chai
, andchai-http
. -
Also, pass
chaiHTTP
into yourchai
asmiddlware
.chai.use(chaiHTTP);
-
And of course, you'll want to require your server as it will be used to mock calls to your api.
-
Finally, call
mongoose.connect
to link up with a testingmongo
instance.mongoose.connect('mongodb://localhost/test');
- This should be all you need to get started on this project!
- This mini-project will actually be the starter pack for the Server-Testing lab.