Giter Site home page Giter Site logo

edignot / node-express-mongodb-mongoose-jest-supertest-nock Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 2.0 83 KB

๐Ÿ’พ Simple Node Express HTTP API microservice connected to MongoDB.

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

JavaScript 100.00%
mongodb mongoose express node nock jest supertest node-fetch

node-express-mongodb-mongoose-jest-supertest-nock's Introduction

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

node-express-mongodb-mongoose-jest-supertest-nock's People

Contributors

edignot avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  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.