samedi 2 novembre 2019

Test self-executing function wit jest

I understand reaching 100% unit test coverage shouldn't be a goal. I have actually excluded index.js from the coverage tests, I'm just interested to know how it would be done.

How can the self-executing function in index.js be tested? I'd like to test that it calls scrape.

index.js:

import scrape from './scrape';

const main = (async () => {
  await scrape();
})();

export default main;

What I have tried:

import main from './index';

const scrape = jest.fn();

describe('On the index file', () => {
  it('scrape executes automatically', async () => {
    await main();
    expect(scrape).toHaveBeenCalled();
  });
});

This errors with TypeError: (0 , _index.default) is not a function

I see that main isn't a function but I haven't managed to make it work.

Aucun commentaire:

Enregistrer un commentaire