Coder Social home page Coder Social logo

compass-challenge-3's Introduction

compass-challenge-3

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

Installation

after you have cloned the repo follow the steps below

  • install dependencies
    use npm to install all dependencies using the install command below

     npm install
  • fill in env variables
    before going ahead to run the variables the .env file in the root of your project looks something like this

      MONGO_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 below

        npm 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 below

        npm 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

technologies used

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

walkthrough

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.

What I learnt

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

Conclusion

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🧡

compass-challenge-3's People

Contributors

ovify avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.