Boss Machine project: all I get is Error: Timeout of 2000 ms exceeded when running tests

I’m working on the Boss Machine.

So far this is what I have:
server.js

const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();

module.exports = app;

/* Do not change the following line! It is required for testing and allowing
*  the frontend application to interact as planned with the api server
*/
const PORT = process.env.PORT || 4001;

// Add middleware for handling CORS requests from index.html
app.use(cors());

// Add middware for parsing request bodies here:
app.use(bodyParser.json);

// Mount your existing apiRouter below at the '/api' path.
const apiRouter = require('./server/api');
app.use('/api', apiRouter);

// This conditional is here for testing purposes:
if (!module.parent) { 
  // Add your code to start the server listening at PORT below:
  app.listen(PORT, () => {
    console.log(`Server is listening on port ${PORT}`);
  });
}

server/api.js

const express = require('express');
const apiRouter = express.Router();

const minions = require('./minions')
apiRouter.use('/minions',minions);

module.exports = apiRouter;

server/minions.js

const express = require('express');
const db = require('./db');
const minionsRouter = express.Router();

//get all minions
minionsRouter.get('/', (req,res) => {
    const allMinions = db.getAllFromDatabase('minions');
    res.status(200).send(allMinions);
});

//create a minion
minionsRouter.post('/', (req,res) => {
    const newMinion = db.addToDatabase('minions', req.body);
    res.status(201).send(newMinion);
});

//get one minion

//update a minion (put)

//delete a minion

module.exports = minionsRouter;

When I want to check wether anything is actually working, I type npm run test in the root directory of the project. This is what I get:

npm run test

build-apis-boss-machine@1.0.0 test C:\Users\username\documents\projects\codeacademy\project-4-boss-machine-start
cross-env PORT=8000 mocha --watch

/api/minions routes
GET /api/minions
1) returns an array
2) returns an array of all minions
GET /minions/:minionId
3) returns a single minion object
4) returns a full minion object
5) returned minion has the correct id
6) called with a non-numeric ID returns a 404 error
7) called with an invalid ID returns a 404 error
PUT /api/minions/:minionId
8) updates the correct minion and returns it
9) updates the correct minion and persists to the database
10) called with a non-numeric ID returns a 404 error
11) called with an invalid ID returns a 404 error
12) called with an invalid ID does not change the database array
POST /api/minions
13) should add a new minion if all supplied information is correct
DELETE /api/minions
14) deletes the correct minion by id
15) called with a non-numeric ID returns a 404 error
16) called with an invalid ID returns a 404 error
(etc. this describes what is being tested)

0 passing (1m)
18 pending
43 failing

  1. /api/minions routes
    GET /api/minions
    returns an array:
    Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves. (C:\Users\username\documents\projects\codeacademy\project-4-boss-machine-start\test\test.js)
    at listOnTimeout (internal/timers.js:549:17)
    at processTimers (internal/timers.js:492:7)

  2. /api/minions routes
    GET /api/minions
    returns an array of all minions:
    Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves. (C:\Users\username\documents\projects\codeacademy\project-4-boss-machine-start\test\test.js)
    at listOnTimeout (internal/timers.js:549:17)
    at processTimers (internal/timers.js:492:7)

  3. /api/minions routes
    GET /minions/:minionId
    returns a single minion object:
    Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves. (C:\Users\username\documents\projects\codeacademy\project-4-boss-machine-start\test\test.js)
    at listOnTimeout (internal/timers.js:549:17)
    at processTimers (internal/timers.js:492:7)
    (etc)

I get the same Error for all the tests: a timeout. What is happening here and where can I fix it?

I had the same timeout error.
I looked it up and it mentioned this on stackoverflow:

but I’m not sure how to apply it.

Hey silvith,

I just came across the same error today and with the help of a more experienced friend, found the solution.

You need to call middleware, including external or third-party middleware, as a function.

That means adding round brackets to your call to bodyParser:

app.use(bodyParser.json())

Otherwise the middleware is missing a next() call leading to the request to neither being resolved nor rejected.

Hope this is still helpful! :slight_smile:

I want to redo this project soon, and this looks like it will solve the problem. Thanks! If it works, I’ll get back to this topic and vote this as solution.

1 Like