Search code examples
javascriptnode.jsaxiosmocha.jssinon

Stub Axios Get Request using HTML fixture


I have tried various attempts. This is my latest. I'm just trying to stub the Axios request and return the fixture.

const { expect } = require('chai');
const sinon = require('sinon');
const { readFile } = require('fs');
const axios = require('axios');

let htmlFixture;

const htmlFixtureData = readFile(
  './test/fixtures/htmlFixture.html',
  (err, file) => {
    if (err) throw err;
    htmlFixture(file);
  }
);

Copying some of the Axios response object.

htmlFixture = (data) => ({
  status: 200,
  statusText: 'OK',
  headers: {},
  data
});

const { getTextBody } = require('../src/app');

describe('Get text body', () => {
  let sandbox = sinon.createSandbox();
  beforeEach(() => (sandbox = sinon.createSandbox()));
  afterEach(() => (sandbox = sandbox.restore()));

  it('should return the text body from the html website', () => {
    sandbox.stub(axios, 'get').resolves(htmlFixtureData);
    console.log(htmlFixture, '< --- fixture here');
    expect(
      getTextBody('http://www.fake-website.com/').to.equal(htmlFixtureData)
    );
  });
});

I'm out of ideas of how to make this work now. I am open to trying Jest instead if this will make things work easier.


Solution

  • Here is the solution:

    app.js:

    const axios = require("axios");
    
    function getTextBody(url) {
      return axios.get(url);
    }
    
    module.exports = { getTextBody };
    

    app.test.js:

    const { expect } = require("chai");
    const sinon = require("sinon");
    const { readFileSync } = require("fs");
    const path = require("path");
    const axios = require("axios");
    
    const htmlFixtureData = readFileSync(path.resolve(__dirname, "./test/fixtures/htmlFixture.html")).toString();
    
    const { getTextBody } = require("./app");
    
    describe("Get text body", () => {
      let sandbox = sinon.createSandbox();
      beforeEach(() => (sandbox = sinon.createSandbox()));
      afterEach(() => (sandbox = sandbox.restore()));
    
      it("should return the text body from the html website", async () => {
        sandbox.stub(axios, "get").resolves(htmlFixtureData);
        const actual = await getTextBody("http://www.fake-website.com/");
        expect(actual).to.be.equal(htmlFixtureData);
        sandbox.assert.calledWith(axios.get, "http://www.fake-website.com/");
      });
    });
    

    Unit test result with coverage report:

     Get text body
        ✓ should return the text body from the html website
    
    
      1 passing (9ms)
    
    -------------|----------|----------|----------|----------|-------------------|
    File         |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
    -------------|----------|----------|----------|----------|-------------------|
    All files    |      100 |      100 |      100 |      100 |                   |
     app.js      |      100 |      100 |      100 |      100 |                   |
     app.test.js |      100 |      100 |      100 |      100 |                   |
    -------------|----------|----------|----------|----------|-------------------|
    

    Source code: https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/59418138