samedi 30 janvier 2021

Async callback not invoked within the 5000ms timeout specified by jest.setTimeout

New to the testing world, I've been struggling to understand why I'm getting the following error while testing the following endpoint with Jest on this very simple API. Would be very grateful if you could give me a hint on the reason why I'm getting the error in detailed below, and how to fix it. Been looking around answers on this particular issue and I'm pretty sure it is related to some promisy thing but don't know how to implement it.

Thank you 🙏

// server.js

const express = require('express');
const app = express();

const carts = new Map();

app.get('/carts/:username/items', (req, res) => {
   const userCart = carts.get(req.params.username);
   userCart ? res.status(200).json(userCart) : res.status(404);
});

module.exports = { app: app.listen(3000) };
// server.test.js

const { app } = require('./server');
const fetch = require('isomorphic-fetch');

const apiRoot = 'http://localhost:3000';

const getItems = (username) => {
    return fetch(`${apiRoot}/carts/${username}/items`, {
        method: 'GET',
    });
};

afterAll(() => app.close());

test('Adds an item to a cart',  async() => {
   const getItemsResponse = await getItems('test_user');
   expect(getItemsResponse.status).toEqual(404);
});
Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.
Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout.Error: 
    at new Spec (/Users/alexandrecoin/Desktop/testing_javascript/node_modules/jest-jasmine2/build/jasmine/Spec.js:116:22)
    at new Spec (/Users/alexandrecoin/Desktop/testing_javascript/node_modules/jest-jasmine2/build/setup_jest_globals.js:78:9)
    at new JBPatchedSpec (/Applications/WebStorm.app/Contents/plugins/JavaScriptLanguage/helpers/jest-intellij/lib/jest-intellij-jasmine-reporter.js:94:7)
    at specFactory (/Users/alexandrecoin/Desktop/testing_javascript/node_modules/jest-jasmine2/build/jasmine/Env.js:523:24)
    at Env.it (/Users/alexandrecoin/Desktop/testing_javascript/node_modules/jest-jasmine2/build/jasmine/Env.js:592:24)
    at Env.it (/Users/alexandrecoin/Desktop/testing_javascript/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:134:23)
    at it (/Users/alexandrecoin/Desktop/testing_javascript/node_modules/jest-jasmine2/build/jasmine/jasmineLight.js:100:21)
    at Object.<anonymous> (/Users/alexandrecoin/Desktop/testing_javascript/server.test.js:14:1)
    at Runtime._execModule (/Users/alexandrecoin/Desktop/testing_javascript/node_modules/jest-runtime/build/index.js:1299:24)
    at Runtime._loadModule (/Users/alexandrecoin/Desktop/testing_javascript/node_modules/jest-runtime/build/index.js:898:12)
    at Runtime.requireModule (/Users/alexandrecoin/Desktop/testing_javascript/node_modules/jest-runtime/build/index.js:746:10)
    at jasmine2 (/Users/alexandrecoin/Desktop/testing_javascript/node_modules/jest-jasmine2/build/index.js:230:13)
    at runTestInternal (/Users/alexandrecoin/Desktop/testing_javascript/node_modules/jest-runner/build/runTest.js:380:22)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at runTest (/Users/alexandrecoin/Desktop/testing_javascript/node_modules/jest-runner/build/runTest.js:472:34)

Aucun commentaire:

Enregistrer un commentaire