mardi 18 décembre 2018

How to make Jest test client-side scripts?

I've got some client-side code that I'd like to test with Jest. Let's narrow it down to this reproducible bit:

// fetch-example.js
const fetchExample = () => fetch('http://example.org/');
module.exports = fetchExample

Since Jest runs tests via Node and Node doesn't support fetch, the following test script

// fetch-example.test.js
const fetchExample = require('./fetch-example')
test('fetch resolves to something', () => {
    expect.assertions(1);
    return fetchExample().then(() => expect(true).toBe(true));
});

fails with ReferenceError: fetch is not defined. No surprises, but... fetchExample works fine in a browser and I'd like Jest to take this into account and mark the test as passed.

Is there any way to make this work? Is there any parameter I can pass to Jest to include browser functions like fetch?

NB: Jest's browser configuration entry does not make it work.

Aucun commentaire:

Enregistrer un commentaire