Axios is a JavaScript library for making HTTP requests through promise-based asynchronous APIs. Its async/await support makes it a practical choice for Node.js integration tests and service-to-service communication. section 9.7.4 section 11.4.6
Use Axios to:
- Send HTTP requests to a running microservice instead of calling its code directly or mocking dependencies. section 9.7.4
- Configure a request timeout so an unresponsive dependency fails quickly instead of waiting indefinitely. section 11.4.6
- Combine requests with retry logic when temporary failures make another attempt appropriate. section 11.4.6
Integration-test request
Require Axios, build a URL from a base address and route, and return axios.get(url). Start the real microservice before the test and close it afterward. Install Axios with npm install --save-dev axios when it is test-only; use --save when production code depends on it. Run fixture-loading tests only against a test database because the fixture setup deletes the database. section 9.7.4
Listing 9.9 Integration testing the metadata microservice with Jest
const axios = require("axios"); const mongodb = require("mongodb"); describe("metadata microservice", () => { const BASE_URL = "http://localhost:3000"; #1 const DBHOST = "mongodb://localhost:27017"; #2 const DBNAME = "testdb"; const { startMicroservice } = require("./index"); let microservice; beforeAll(async () => { microservice = await startMicroservice(DBHOST, DBNAME); #3 }); afterAll(async () => { await microservice.close(); #4 }); function httpGet(route) { const url = `${BASE_URL}${route}`; return axios.get(url); } async function loadDatabaseFixture(collectionName, #5 records) { #5 await microservice.db.dropDatabase(); #5 const collection = microservice.db.collection(collectionName); #5 await collection.insertMany(records); #5 } #5 test("/videos route retrieves data", async () => { #6 const id1 = new mongodb.ObjectId(); #7 const id2 = new mongodb.ObjectId(); const videoPath1 = "my-video-1.mp4"; const videoPath2 = "my-video-2.mp4"; const testVideos = [ { _id: id1, videoPath: videoPath1 }, { _id: id2, videoPath: videoPath2 }, ]; #7 await loadDatabaseFixture("videos", testVideos); #8 const response = await httpGet("/videos"); #9 expect(response.status).toEqual(200); const videos = response.data.videos; #10 expect(videos.length).toEqual(2); expect(videos[0]._id).toEqual(id1.toString()); expect(videos[0].videoPath).toEqual(videoPath1); expect(videos[1]._id).toEqual(id2.toString()); expect(videos[1].videoPath).toEqual(videoPath2); #10 }); --snip-- #11 }); #1 Sets the base URL for the microservice we’re testing #2 Sets the URL for our database, which we’re running locally #3 Starts the microservice, including the HTTP server and the database connection #4 Shuts down the microservice #5 The helper function that loads test data (a database fixture) into our database. We defined this function in listing 9.8. #6 Tests that a list of videos can be retrieved via an HTTP request to the /videos route #7 Creates test data to load into the database #8 Loads the database fixture into the videos collection of the database #9 Makes an HTTP request to the route we’re testing #10 Expects that the received data matches our test data #11 More tests go here!
Inspect a complete Jest integration-test listing showing Axios requests against a real metadata microservice. section 9.7.4
Listing 11.3 Setting a default timeout for HTTP requests with Axios
const axios = require("axios");
axios.defaults.timeout = 2500; #1
Inspect the Axios configuration that sets a 2500-millisecond default timeout for HTTP requests. section 11.4.6
