Search code examples
javascriptunit-testingtestingjestjscheerio

how to test cheerio js


this is my code and i don't know how to write test for it:

html.js

const getHtml = async (url) => {
  const { data } = await axios.get(url);
  return data;
};

const cheerioInit = async (url) => cheerio.load(await getHtml(url));

module.exports = {
  cheerioInit,
  getHtml 
};

i think i should mock this, but don't know how to do that. i wrote this but getting error:

const htmlJs = require("./html");

describe("html", () => {
  it("initializes cheerio js", () => {
    const mock = jest.spyOn(htmlJs, "cheerioInit");
    expect(mock).toHaveBeenCalled();
  });
});

this is error:

enter image description here


Solution

  • You can use jest.spyOn(object, methodName) to mock axios.get() method and its resolved value and mock cheerio.load() method with a fake implementation. After executing the getHtml and cheerioInit functions, make assertion for above mocks to check if they have been called with specific arguments.

    E.g.

    html.js:

    const axios = require('axios');
    const cheerio = require('cheerio');
    
    const getHtml = async (url) => {
      const { data } = await axios.get(url);
      return data;
    };
    
    const cheerioInit = async (url) => cheerio.load(await getHtml(url));
    
    module.exports = {
      cheerioInit,
      getHtml,
    };
    

    html.test.js:

    const { getHtml, cheerioInit } = require('./html');
    const axios = require('axios');
    const cheerio = require('cheerio');
    
    describe('html', () => {
      afterEach(() => {
        jest.restoreAllMocks();
      });
      describe('getHtml', () => {
        it('should get html', async () => {
          const getSpy = jest.spyOn(axios, 'get').mockResolvedValueOnce({ data: '<div>teresa teng</div>' });
          const actual = await getHtml('http://localhost:3000');
          expect(actual).toEqual('<div>teresa teng</div>');
          expect(getSpy).toBeCalledWith('http://localhost:3000');
        });
      });
      describe('cheerioInit', () => {
        it('should initializes cheerio', async () => {
          const loadSpy = jest.spyOn(cheerio, 'load').mockImplementation();
          const getSpy = jest.spyOn(axios, 'get').mockResolvedValueOnce({ data: '<div>teresa teng</div>' });
          await cheerioInit('http://localhost:3000');
          expect(loadSpy).toHaveBeenCalledWith('<div>teresa teng</div>');
        });
      });
    });
    

    unit test result:

     PASS  examples/66304918/html.test.js
      html
        getHtml
          ✓ should get html (3 ms)
        cheerioInit
          ✓ should initializes cheerio
    
    ----------|---------|----------|---------|---------|-------------------
    File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
    ----------|---------|----------|---------|---------|-------------------
    All files |     100 |      100 |     100 |     100 |                   
     html.js  |     100 |      100 |     100 |     100 |                   
    ----------|---------|----------|---------|---------|-------------------
    Test Suites: 1 passed, 1 total
    Tests:       2 passed, 2 total
    Snapshots:   0 total
    Time:        4.088 s