简体   繁体   中英

Async test in Jest failing due to unresolved promise

I am learning Jest and running into an issue trying to mock an async function that returns a fetch promise.

In their docs, there is this: "If a promise doesn't resolve at all, this error might be thrown: - Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL. Most commonly this is being caused by conflicting Promise implementations. Consider replacing the global promise implementation with your own, for example global.Promise = require.requireActual('promise'); and/or consolidate the used Promise libraries to a single one."

This likely is the issue, but it's unclear to me how to fix my issue or determine where the Promise is breaking.

Maybe my understanding of mocks in Jest is wrong? I tried using their example and it worked perfectly of course. ( https://facebook.github.io/jest/docs/tutorial-async.html#content )

This is what I get in the console:

● endpoint can receive form data › can parse and fulfill a promise

    Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

Here are my modules and test:

// myModule/index.js
import { OK } from 'http-status-codes';
import request from './request';
import parseForm from '../../../../modules/parseMultiPart';

export default (req, res, next) => parseForm(req)
    .then(body => request(body, req)
    .then(result => result.text())
    .then(result => res.status(OK).send(result)))
.catch(next);


// myModule/request.js
import fetch from 'node-fetch';

export default (body, req) => fetch('http://url', {
    method: 'POST',
    body: JSON.stringify(body.fields),
    headers: {
        'Content-Type': 'application/json',
        'X-Request-Id': req.id
    }
});


// myModule/__tests__/myModule.test.js
import MockReq from 'mock-req';
import MockRes from 'mock-res';
import myModule from '../';

jest.mock('../request');

describe('endpoint can receive form data', () => {
    const response = new MockRes();
    const request = new MockReq({
        method: 'POST',
        url: '/webhook',
        headers: {
            'Content-Disposition': 'form-data; name="file"; filename="plain.txt"',
            'Content-Type': 'multipart/form-data; custom=stuff; boundary=----TLV0SrKD4z1TRxRhAPUvZ',
            'Content-Length': 213
        }
    });

    it('can parse and fulfill a promise', () => myModule(request, response)
        .then(text => expect(text).toEqual('API Event Received'))
    );
});


// myModule/__mocks__/request.js
export default function request() {
    return new Promise((resolve) => {
        process.nextTick(
            () => resolve('API Event Received')
        );
    });
}

I was mocking the wrong thing. Needed to be mocking the functions, not the data in this case:

jest.mock('../parseMultiPart');
jest.mock('../request');

My request wasn't correct and the Promise was never returning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM