NodeJS Tests with microservices, Jenkins and Docker


Hi, this is my journey trying to add tests for a microservice architectured built with nodejs. I would need to use Jenkins and Docker.

This article is a dev log starting from nowhere (is not totally true, but I would like to make it sounds worst than in reality). I have an microservice architecture which connects its services through RabbitMQ, message broker and redis in place. So running integration tests, I need an environment capable of this.

I don’t know which is the right approach, but time will be my fellow.

I read few things trying to setup the testing environment:

  • https://dev.to/mrfrontend/how-to-mock-an-api-with-random-data-from-nodejs-dda
  • https://scotch.io/tutorials/nodejs-tests-mocking-http-requests
  • https://codersociety.com/blog/articles/contract-testing-pact
  • https://martinfowler.com/articles/microservice-testing/
  • https://hackernoon.com/set-up-jenkins-ci-in-docker-container-and-run-your-tests-inside-their-own-container-a-how-to-guide-7h8u32yi

The default test file, to check that mocha, chai, nuxt and axios works as expected is this:

import nock from 'nock'
import { describe, it } from 'mocha'
import { expect } from 'chai'
import axios from 'axios'

describe('Initial test', function () {
  it('should be equals with 2', function () {
    expect(1 + 1).equals(2)
  })
})

describe('Test Nock', function () {
  const baseUrl = 'https://example.com/'

  it('should use nock for request', async function () {
    const expectedResponse = { hello: 'world' }
    nock(baseUrl).get('/').reply(200, expectedResponse)

    const response = await axios(baseUrl, {
      method: 'GET',
      url: '/'
    })

    expect(response.status).to.be.equals(200)
    expect(response.data).to.be.deep.equals(expectedResponse)
  })
})

Newsletter


Related Posts

What I learned to never do in a presentation meeting

What I learned to never do in a presentation meeting. I had a very bad meeting experience with a gateway provider.

Startup graveyard is a series of killed projects that made millions in revenue per year

Startup graveyard is a series of killed projects that made millions in revenue per year. Just watch their graveyard and do it your own. What better validation do you expect.

How do you detect Credit card type based on the number?

How do you detect Credit card type based on the number using JavaScript? Simple solution using only code, no library required.

Download files in Javascript from Node.js server

How to download files in Javascript, either you use Vue.js, React, Angular, jQuery, or Vanilla JS. On the backend side, we run on Node.js using Express.js.

What books to read

The list of books I read. I decided one year ago to challenge myself and read as many books as I can this year. Take a look at this article and find all of them.

Startup tools for a better version

Are you a startup founder and are you looking for startup tools like invoice, planning or just making money with much automation? Check this article, from founder to founders.

How to use express validator?

How to use express validator through full examples. I found express validator very powerful, but having a poor documentation.

How to check DNS propagation

How to check DNS propagation? Did you changed the DNS records recently and now is not working? Stop crying and check the DNS propagation.

LibraPay library for Nodejs

LibraPay library for Nodejs - integrate Libra Bank online payment gateway using this library for NodeJs.

Opensource Object Storage with Minio

Opensource Object Storage with Minio using Docker. An alternative to AWS S3, Linode Storage, Google Storage, Azure Storage.