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

Tools for Analyzing Your Competition

Before knowing where your business stands, you should look at the market and your competition. You need to know what marketing tactics and strategies are working for your audience, what traffic the competition has, and how they position.

Free Online Photo Editor with Cool Effects | Edit Images Easily

Transform your images with our free online photo editor with cool effects. Add filters, adjust colors, and apply creative enhancements. Edit your photos easily and make them stand out.

How to find the size of a table in SQL?

Learn how to determine the size of a SQL table with this informative guide. Discover efficient methods for measuring table size.

MongoDB Query date by _id field

Query date by _id in mongodb. Straighforward guide to solve your issue right away.

Unleashing the Power of Free API for Scrape Reverse Geocoding

Learn how to harness the potential of reverse geocoding with our beginner's guide to scraping APIs for free. Unlock the power today.

Discover the Power of Whatsapp-web.js for Safe and Easy Automation

Automate WhatsApp with ease using Whatsapp-web.js, a NodeJS client library that connects through the official WhatsApp Web app, reducing ban risks. Perfect for user or business accounts.

Website tours js tools

Tools to create live tours for your web project using js libraries.

Unlocking SaaS Marketing Success: Essential Resources to Learn and Grow

Discover a comprehensive collection of valuable resources to master the art of SaaS marketing.

Where to launch your startup

Do you wanna launch grandiosely your startup? Get a list of platforms where people can discover your project and can push your project directly to the sky.

Email campaign improving my SaaS convertion - Manager Flota

Users subscribe to your SaaS, but don't let the money on your table. Here's how I do it!