Skip to content

edignot/node-express-mongoDB-mongoose-jest-supertest-nock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

16 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Express Microservice

About

πŸ’Ύ This is a simple express microservice. Client can get all quotes, one quote by id, delete and update quote by id. Also clinet can post a new quote and if required fields are not provided - random quote is fetched from a downstream API

Learning goals:

  • Implement an HTTP API web server microservice.
  • Interact with downstream APIs.
  • Connect server to NoSQL database .
  • Test API endpoints and mock downstreams API requests.

Technologies Used:

  • Node | Express
  • MongoDB | Mongoose | MongoDB Atlas | MongoDB Compass
  • Jest | Supertest | Nock
  • Node Fetch

File structure

REST API Endpoints:

Local Server

'http://localhost:3000/api/quotes'

  • GET
  • POST

'http://localhost:3000/api/quotes/:id'

  • GET
  • PATCH
  • DELETE

Production

https://apiquotesapi.herokuapp.com/api/quotes

  • GET
  • POST

https://apiquotesapi.herokuapp.com/api/quotes/:id

  • GET
  • PATCH
  • DELETE

Sample response

  • GET http://localhost:3000/api/quotes
[
    {
        "_id": "5fa1cab353459e9f3d03361b",
        "quote": "If you don't go after what you want, you'll never have it. If you don't ask, the answer is always no. If you don't step forward, you're always in the same place.",
        "author": "Nora Roberts",
        "createdAt": "2020-11-03T21:25:07.226Z",
        "updatedAt": "2020-11-03T21:25:07.226Z",
        "__v": 0
    },
    {
        "_id": "5fa1cab553459e9f3d03361c",
        "quote": "A goal is a dream with a deadline.",
        "author": "Napoleon Hill",
        "createdAt": "2020-11-03T21:25:09.678Z",
        "updatedAt": "2020-11-03T21:25:09.678Z",
        "__v": 0
    }
]

Testing

  • Utilized hooks to connect to local MongoDB before all tests run and disconnect after all tests finish running. Also seed testing database with testing data before each test and delete data after each test.
beforeAll(async () => {
beforeAll(async () => {
    await mongoose.connect('mongodb://127.0.0.1/testing', {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    })
  })

  beforeEach(async () => {
    for (const testQuote of testQuotes) {
      const newQuote = new Quote({
        quote: testQuote.quote,
        author: testQuote.author,
      })
      await newQuote.save()
    }
  })

  afterEach(async () => {
    await Quote.deleteMany()
  })

  afterAll(async () => {
    await Quote.drop()
    await mongoose.connection.close()
  })
  • Utilized Nock npm package and mocked downstream API response
  test('POST | save quote to database with external api data', async (done) => {
    const mockedResponse = [{ text: 'mock quote', author: 'mock author' }]
    nock('https://type.fit').get('/api/quotes').reply(200, mockedResponse)
    ...
  })
 PASS  api/tests/quotes.test.js
  Quote API endpoints
    βœ“ GET | get all quotes from database (42 ms)
    βœ“ POST | save quote to database with user data (28 ms)
    βœ“ POST | save quote to database with external api data (20 ms)
    βœ“ GET | get quote from database by id (11 ms)
    βœ“ GET | returns message if quote id doesn't exist (10 ms)
    βœ“ PATCH | update quote by id (44 ms)
    βœ“ PATCH | returns message if quote id doesn't exist (12 ms)
    βœ“ DELETE | delete quote by id (11 ms)
    βœ“ DELETE | returns message if quote id doesn't exist (7 ms)
    βœ“ GET | Default response if endpoint doesn't exist (7 ms)
    βœ“ POST | Default response if endpoint doesn't exist (7 ms)
    βœ“ PATCH | Default response if endpoint doesn't exist (10 ms)
    βœ“ DELETE | Default response if endpoint doesn't exist (7 ms)

Test Suites: 1 passed, 1 total
Tests:       13 passed, 13 total
Snapshots:   0 total
Time:        3.807 s

Releases

No releases published

Packages

No packages published