Not My Cat is a responsive single-paged web app providing a platform for cat lovers to track, collect and play with cats in the real world. Our users can create a cat, with name, picture, attributes and their location stored using the Google Maps API. They can then play against randomised cats, to increase their win total and win crowns. Finally they can see where other cats have been spotted and go out to spot these neighbourhood felines.
We created Not My Cat as part of our final project at Makers Academy, a 12 week web development bootcamp. We used the MERN stack (Mongo DB, Express, React, Node.js)
-
npm init -
npm install dependancies
-
dotenvfor accessing enviroment data needed to connect to Mongodb -
expressfor the router functions -
mongoosefor connecting and carrying out actions on Mongodb -
supertestfor testing requests to the router -
jestas a testing framework, coverage and more -
nodemonto rolling restart the server on saved changes -
eslintas a linter
-
-
updated scripts in the package.json
"scripts": {
"dev": "nodemon app.js",
"test": "jest",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage --colors"
}-
added dirs:
- models
- config
- routes/api
- test/helper
-
touch:
-
./app.js -
./test/app.test.js -
./test/users.test.js -
./config/db.js -
./.env -
./routes/api/users.js -
./models/user.js
-
-
added jest configuration to package.json
"jest": {
"testEnvironment": "node",
"testTimeout": 10000
}app.jsis updated to pull in express.js and a few of the other dependancies and the empty routes files, a test is written for a basic route and then the route added to pass.- A port is assigned to the server.
- Middleware is added to the
app.jsto ensure thatjsoncan be parsed for post requests. (This has to be declared before any of the routes otherwise it will lead to errors where no body is passed to the post routes.) - The db connection method is delcared for dev and production envs.
- Requires
app,testSetupandsupertestto be pulled in. - The tests are in a familiar format in
jesttojasmineandrspecet al. - The
requestconst, instantisation of supertest allows you to make requests to the server from the test suite. - Tests involving requests must be async or use the done callback.
- A
mongooseschema object that defines the user model in our case. the mongoose documentation helps with how we can define our attributes. - The function
userSchema.pre("save")allows us to do things to data before it is saved to the database, in this instance we are gonna encrypt the password withBcrypt. - The function
userSchema.methods.isValidPassword()is used to check the password for when the user wants to login or authenticate. It returns aboolean.
- Using
mongooseto create functions to connect to and disconnect from the database. - We use the env variable
MONGO_URIwhich we access usingdotenvto connect to the database and make sure that our credentials are not pushed to github.
- This helper file allows the tests to connect to the database use a
beforeAllandafterAllfunction. - Also there are
beforeEachandafterEachfunctions that create stand up test data and tear it down so that each of the tests can be carried out in a clean environment. - This file calls on
testData.jsonthat contains the data for the test database seeding.