jeudi 19 décembre 2019

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.

Aucun commentaire:

Enregistrer un commentaire