My submission for the third challenge of the compass scholarship program. The challenge was to build a simple api following this swagger documentation using typescript, express and jest for unit tests with at least 50% coverage
after you have cloned the repo follow the steps below
-
install dependencies
use npm to install all dependencies using the install command belownpm install
-
fill in env variables
before going ahead to run the variables the .env file in the root of your project looks something like thisMONGO_URI= PORT= SECRET= TEST_MONGO_URI=
if they are not provided appropriate values some of the test will not pass as some of them have to connect to a mongodb database using mongoose, the server will also not run as expected without connection to a mongodb database. MONGO_URI and TEST_MONGO_URI must be valid mongodb connection uri strings it could be a local db or an atlas cluster, it is advisable to use different databases for the MONGO_URI and TEST_MONGO_URI. PORT is for the port you want the backend to run on, and the SECRET is for creating jwt on successfull user sign in
-
run tests
you can now go ahead to run the tests using the command belownpm test
npm test runs the command jest --coverage --detectOpenHandles I suggest you use npm test because each time i try to run the tests without the --detectOpenHandles, one of the tests suites fails. and the waring jest returns is something along the line of memory leak or something like that and at this moment i do not have the experties to tackle that difficulty. also ensure that the tests that require connection to a database do connect or else the following tests after the database connection will fail
-
build and run server
after the test result has been displayed you can exit the interactive jest terminal them build the typescript server using the command belownpm run build
after that run the server using
npm run start
if everything was successfull you should see server is running on port [whatever port you specified] displayed on the console
for this project i used the following technologies
- Express (Node js framework)
- MongoDB/Atlas (Database)
- typescript
- jest
and these packages:
- JOI for input validation
- bcrypt for hashing
- JsonWebToken for jwt tokens
- express-async-errors
This challenge was challenging a whole lot😂. I had to learn typescript and testing. while I was learning typescript I fell in love with the use case for decorators. there was a part in the tutorial where the tutor automated the routing process, all you had to do was add a decorator and a new route was added or to add a middleware all that was needeed was to use a @use decorator. the use of decorators made the code preety readable and cool as well. I wanted to combine decorators with the repository-controller-service pattern that paulo suggested when he shared the link to an article with us the problem however was that the article was written in java and was not easy to understand as i am not properly grounded in java.
i had to search on github for typescript projects that followed the repository-controller-service pattern then i found this one so i went through their code to understand what the repository-controller-service pattern really meant. and following their folder structure i began working on my own.
at the end of the day i just wanted to combine decorators and the repository-controller-service pattern, since this was going to be the last challenge i just wanted to showcase all i had learnt in this one project.
also note that i had to copy some parts of the decorators implementation from the original tutor to save time as it wasn't a core test or server implementation but a dependency
testing at first was confusing, i did not know what to test or how to go about it but after research, chat with gpt and a friend i began to get the hang of it. i fell in love😂 with mocks and stubs and all the beautiful simulations you could run with them.
i learnt a lot while working on this challenge, but i'll give a summary
- typescript
i leart about type annotations, type inference, interfaces, generics, decorators, type decleration files - design patterns
i learnt about delegation, composition, inheritance, and i'm kinda familiar to an extent with the controller-service-repository pattern - testing
i learn about tests, the F.I.R.S.T principle of tests, setup and teardown, AAA principle, was familiarized with jest, mocks, stubs, e.t.c - thunderclient
as an extra i learnt how to use the thunderClient VS code extension, my workflow with it was smooth as I did not have to leave vs code while making use of it, and i also learn how to automate filling in the authorization header in thunderClient
i'll be looking forward to your review, what i should have done better, what i should do next time, what i should learn next, and whatever else you feel i should know, i really learnt a lot with this challenge and i am very greatfull for the opportunity. Thank you Compass🧡