Boss Machine - Check my code!

Hey there everyone! I’m trying really hard on solving this challenge, I don’t want to peak at the solution. It seems like my major issue when running tests is that I’m getting an internal server error 500, instead of the statuses I’m sending back. Also, my objects are being sent empty, I don’t understand why. Despite that, I know I’m trying quite a DRYed code, but I’d like to know if it’s possible and if I’m on a good path.
Have you got any tips on how to solve it? Here’s my code for the api.js!!

const express = require(‘express’);

const api = express.Router();

const apiRouter = express.Router();

const checkMillionDollarIdea = require(’./checkMillionDollarIdea’);

const {

createMeeting,

getAllFromDatabase,

getFromDatabaseById,

addToDatabase,

updateInstanceInDatabase,

deleteFromDatabasebyId,

deleteAllFromDatabase,

} = require(’./db’);

apiRouter.use([’/minions’, ‘/minions/minionId’], (req, res, next) => {

req.body.type = 'minions';

next();

})

apiRouter.use([’/ideas’, ‘/ideas/ideasId’], (req, res, next) => {

req.body.type = 'ideas';

next();

})

apiRouter.param([‘minionId’, ‘ideaId’, ‘meetingId’], (req, res, next, id) => {

const api = getFromDatabaseById(type, id);

if (apiThrough) {

  req.api = api;

  next();

} else {

  res.status(404).send();

}

});

apiRouter.use(’/ideas/ideaId’, checkMillionDollarIdea);

apiRouter.use(’/meetings’, (req, res, next) => {

req.body.type = 'meetings';

next();

})

apiRouter.get ([’/minions’, ‘/ideas’, ‘/meetings’], (req, res, next) => {

const type = req.body.type;

const api = getAllFromDatabase(type);

res.status(200).send(api);

})

apiRouter.post ([’/minions’, ‘/ideas’], (req, res, next) => {

const newApi = req.body;

const type = req.body.type;

addToDatabase(type, newApi);

res.status(201).send(newApi);

})

apiRouter.get ([’/minions/minionId’, ‘/ideas/ideaId’], (req, res, next) => {

const apiId = Number(req.body.id);

const type = req.body.type;

const apiFound = database.getFromDatabaseById(type, apiId)

if (apiFound.id !== -1 && Number.isInteger(apiId)){

res.status(200).send(apiFound);

} else {

    res.status(404).send();

}

})

apiRouter.put ([’/minions/minionId’, ‘/ideas/ideaId’], (req, res, next) => {

const apiId = Number(req.body.id);

const type = req.body.type;

const newApi = req.body;

const api = getFromDatabaseById(type, apiId);

if (api.id !== -1 && Number.isInteger(apiId)){

updateInstanceInDatabase(type, newApi);

res.status(200).send(newApi);

} else {

    res.status(404).send("Not Found");

}

})

apiRouter.delete ([’/minions/minionId’, ‘/ideas/ideaId’], (req, res, next) => {

const apiId = Number(req.body.id);

const type = req.body.type;

const api = getFromDatabaseById(type, apiId);

if (api.id !== -1 && Number.isInteger(apiId)){

deleteFromDatabasebyId(type, apiId);

res.status(204).send();

} else {

    res.status(404).send("Not Found");

}

})

apiRouter.post(’/meetings’, (req, res, next) => {

const newMeeting = createMeeting();

res.status(201).send(newMeeting);

})

apiRouter.delete(’/meetings’, (req, res, next) => {

deleteAllFromDatabase('meetings');

res.status(204).send();

})

module.exports = apiRouter;

1 Like

This is the test!

 12 passing (957ms)
  18 pending
  31 failing

  1) /api/minions routes
       GET /minions/:minionId
         returns a single minion object:
     Error: expected 200 "OK", got 500 "Internal Server Error"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  2) /api/minions routes
       GET /minions/:minionId
         returns a full minion object:
     Error: expected 200 "OK", got 500 "Internal Server Error"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  3) /api/minions routes
       GET /minions/:minionId
         returned minion has the correct id:
     Error: expected 200 "OK", got 500 "Internal Server Error"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  4) /api/minions routes
       GET /minions/:minionId
         called with a non-numeric ID returns a 404 error:
     Error: expected 404 "Not Found", got 500 "Internal Server Error"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  5) /api/minions routes
       GET /minions/:minionId
         called with an invalid ID returns a 404 error:
     Error: expected 404 "Not Found", got 500 "Internal Server Error"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  6) /api/minions routes
       PUT /api/minions/:minionId
         updates the correct minion and returns it:

      AssertionError: expected {} to deeply equal { name: 'Test' }
      + expected - actual

      -{}
      +{
      +  "name": "Test"
      +}

      at D:\Projetos\Code\BossMachine\test\test.js:107:44
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)

  7) /api/minions routes
       PUT /api/minions/:minionId
         updates the correct minion and persists to the database:
     AssertionError: expected undefined to equal 'Persistence Test'
      at D:\Projetos\Code\BossMachine\test\test.js:131:46
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)

  8) /api/minions routes
       PUT /api/minions/:minionId
         called with a non-numeric ID returns a 404 error:
     Error: expected 404 "Not Found", got 500 "Internal Server Error"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  9) /api/minions routes
       PUT /api/minions/:minionId
         called with an invalid ID returns a 404 error:
     Error: expected 404 "Not Found", got 500 "Internal Server Error"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  10) /api/minions routes
       POST /api/minions
         should add a new minion if all supplied information is correct:

      AssertionError: expected { Object (name, title, ...) } to deeply equal { Object (name, title, ...) }
      + expected - actual

         "id": "11"
         "name": "Test"
         "salary": 0
         "title": ""
      +  "type": "minions"
         "weaknesses": ""
       }

      at D:\Projetos\Code\BossMachine\test\test.js:194:46
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)

  11) /api/minions routes
       DELETE /api/minions
         deletes the correct minion by id:
     Error: expected 204 "No Content", got 500 "Internal Server Error"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  12) /api/minions routes
       DELETE /api/minions
         called with a non-numeric ID returns a 404 error:
     Error: expected 404 "Not Found", got 500 "Internal Server Error"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  13) /api/minions routes
       DELETE /api/minions
         called with an invalid ID returns a 404 error:
     Error: expected 404 "Not Found", got 500 "Internal Server Error"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  14) /api/ideas routes
       GET /ideas/:ideaId
         returns a single idea object:
     Error: expected 200 "OK", got 500 "Internal Server Error"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  15) /api/ideas routes
       GET /ideas/:ideaId
         returns a full idea object with correct properties:
     Error: expected 200 "OK", got 500 "Internal Server Error"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  16) /api/ideas routes
       GET /ideas/:ideaId
         returned idea has the correct id:
     Error: expected 200 "OK", got 500 "Internal Server Error"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  17) /api/ideas routes
       GET /ideas/:ideaId
         called with a non-numeric ID returns a 404 error:
     Error: expected 404 "Not Found", got 500 "Internal Server Error"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  18) /api/ideas routes
       GET /ideas/:ideaId
         called with an invalid ID returns a 404 error:
     Error: expected 404 "Not Found", got 500 "Internal Server Error"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  19) /api/ideas routes
       PUT /api/ideas/:ideaId
         updates the correct idea and returns it:

      AssertionError: expected {} to deeply equal { name: 'Test' }
      + expected - actual

      -{}
      +{
      +  "name": "Test"
      +}

      at D:\Projetos\Code\BossMachine\test\test.js:344:44
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)

  20) /api/ideas routes
       PUT /api/ideas/:ideaId
         updates the correct idea and persists to the database:
     AssertionError: expected undefined to equal 'Persistence Test'
      at D:\Projetos\Code\BossMachine\test\test.js:368:44
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)

  21) /api/ideas routes
       PUT /api/ideas/:ideaId
         called with a non-numeric ID returns a 404 error:
     Error: expected 404 "Not Found", got 500 "Internal Server Error"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  22) /api/ideas routes
       PUT /api/ideas/:ideaId
         called with an invalid ID returns a 404 error:
     Error: expected 404 "Not Found", got 500 "Internal Server Error"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  23) /api/ideas routes
       POST /api/ideas
         should add a new idea if all supplied information is correct:

      AssertionError: expected { Object (name, description, ...) } to deeply equal { Object (name, description, ...) }
      + expected - actual

         "description": ""
         "id": "11"
         "name": "Test"
         "numWeeks": 10
      +  "type": "ideas"
         "weeklyRevenue": 200000
       }

      at D:\Projetos\Code\BossMachine\test\test.js:432:44
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)

  24) /api/ideas routes
       DELETE /api/ideas
         deletes the correct minion by id:
     Error: expected 204 "No Content", got 500 "Internal Server Error"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  25) /api/ideas routes
       DELETE /api/ideas
         called with a non-numeric ID returns a 404 error:
     Error: expected 404 "Not Found", got 500 "Internal Server Error"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  26) /api/ideas routes
       DELETE /api/ideas
         called with an invalid ID returns a 404 error:
     Error: expected 404 "Not Found", got 500 "Internal Server Error"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  27) checkMillionDollarIdea middleware
       sends a 400 error if the total yield is less than one million dollars:
     AssertionError: expected null to equal 400
      at Context.<anonymous> (test\test.js:529:23)
      at processImmediate (internal/timers.js:456:21)

  28) checkMillionDollarIdea middleware
       sends a 400 error if numWeeks or weeklyRevenue is not supplied:
     AssertionError: expected null to equal 400
      at Context.<anonymous> (test\test.js:543:23)
      at processImmediate (internal/timers.js:456:21)

  29) checkMillionDollarIdea middleware
       sends a 400 error if numWeeks or weeklyRevenue is an invalid string:

      AssertionError: expected true to be false
      + expected - actual

      -true
      +false

      at Context.<anonymous> (test\test.js:553:29)
      at processImmediate (internal/timers.js:456:21)

  30) checkMillionDollarIdea middleware
       is used in a POST /api/ideas route to reject insufficiently profitable ideas:
     Error: expected 400 "Bad Request", got 201 "Created"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  31) /api/meetings routes
       POST /api/meetings
         should persist the created meeting to the database:

      AssertionError: expected 3 to equal 4
      + expected - actual

      -3
      +4

      at D:\Projetos\Code\BossMachine\test\test.js:649:46
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)

By changing every /minions/:minionId and /ideas/:ideasId to /minions/minionId and /ideas/ideaId I guess because I’m not supposed to access parameters, despite getting less error I know that its not a good way of solving things.

> cross-env PORT=8000 mocha --watch




  /api/minions routes
    GET /api/minions
      √ returns an array
      √ returns an array of all minions
    GET /minions/:minionId
      1) returns a single minion object
      2) returns a full minion object
      3) returned minion has the correct id
      √ called with a non-numeric ID returns a 404 error
      √ called with an invalid ID returns a 404 error
    PUT /api/minions/:minionId
      4) updates the correct minion and returns it
      5) updates the correct minion and persists to the database
      √ called with a non-numeric ID returns a 404 error
      √ called with an invalid ID returns a 404 error
      √ called with an invalid ID does not change the database array
    POST /api/minions
      6) should add a new minion if all supplied information is correct
    DELETE /api/minions
      7) deletes the correct minion by id
      √ called with a non-numeric ID returns a 404 error
      √ called with an invalid ID returns a 404 error

  /api/ideas routes
    GET /api/ideas
      √ returns an array
      √ returns an array of all ideas
    GET /ideas/:ideaId
      8) returns a single idea object
      9) returns a full idea object with correct properties
      10) returned idea has the correct id
      √ called with a non-numeric ID returns a 404 error
      √ called with an invalid ID returns a 404 error
    PUT /api/ideas/:ideaId
      11) updates the correct idea and returns it
      12) updates the correct idea and persists to the database
      √ called with a non-numeric ID returns a 404 error
      √ called with an invalid ID returns a 404 error
      √ called with an invalid ID does not change the database array
    POST /api/ideas
      13) should add a new idea if all supplied information is correct
    DELETE /api/ideas
      14) deletes the correct minion by id
      √ called with a non-numeric ID returns a 404 error
      √ called with an invalid ID returns a 404 error

  checkMillionDollarIdea middleware
    √ is a function takes three arguments
    15) sends a 400 error if the total yield is less than one million dollars
    √ calls next for ideas that will yield at least one million dollars
    16) sends a 400 error if numWeeks or weeklyRevenue is not supplied
    17) sends a 400 error if numWeeks or weeklyRevenue is an invalid string
    18) is used in a POST /api/ideas route to reject insufficiently profitable ideas

  /api/meetings routes
    GET /api/meetings
      √ returns an array
      √ returns an array of all meetings
    POST /api/meetings
      √ should create a new meetings and return it
      19) should persist the created meeting to the database
    DELETE /api/meetings route
      √ deletes all meetings

  BONUS: /api/minions/:minionId/work routes
    GET /api/minions/:minionId/work
      - returns an array
      - returns an array of all all work for the specified minion
      - called with a non-numeric minion ID returns a 404 error
      - called with an invalid ID minion returns a 404 error
    PUT /api/minions/:minionId/work/:workId
      - updates the correct work and returns it
      - updates the correct work item and persists to the database
      - called with a non-numeric minion ID returns a 404 error
      - called with an invalid minion ID returns a 404 error
      - called with a non-numeric work ID returns a 404 error
      - called with an invalid work ID returns a 404 error
      - called with an invalid ID does not change the database array
      - returns a 400 if a work ID with the wrong :minionId is requested
    POST /api/minions/:minionId/work
      - should add a new work item if all supplied information is correct
    DELETE /api/minions/:minionId/work/:workId
      - deletes the correct work by id
      - called with a non-numeric minion ID returns a 404 error
      - called with an invalid minion ID returns a 404 error
      - called with a non-numeric work ID returns a 404 error
      - called with an invalid work ID returns a 404 error


  24 passing (337ms)
  18 pending
  19 failing

  1) /api/minions routes
       GET /minions/:minionId
         returns a single minion object:
     Error: expected 200 "OK", got 404 "Not Found"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  2) /api/minions routes
       GET /minions/:minionId
         returns a full minion object:
     Error: expected 200 "OK", got 404 "Not Found"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  3) /api/minions routes
       GET /minions/:minionId
         returned minion has the correct id:
     Error: expected 200 "OK", got 404 "Not Found"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  4) /api/minions routes
       PUT /api/minions/:minionId
         updates the correct minion and returns it:

      AssertionError: expected {} to deeply equal { name: 'Test' }
      + expected - actual

      -{}
      +{
      +  "name": "Test"
      +}

      at D:\Projetos\Code\BossMachine\test\test.js:107:44
      at processTicksAndRejections (internal/process/task_queues.js:97:5)

  5) /api/minions routes
       PUT /api/minions/:minionId
         updates the correct minion and persists to the database:
     AssertionError: expected undefined to equal 'Persistence Test'
      at D:\Projetos\Code\BossMachine\test\test.js:131:46
      at processTicksAndRejections (internal/process/task_queues.js:97:5)

  6) /api/minions routes
       POST /api/minions
         should add a new minion if all supplied information is correct:

      AssertionError: expected { Object (name, title, ...) } to deeply equal { Object (name, title, ...) }
      + expected - actual

         "id": "11"
         "name": "Test"
         "salary": 0
         "title": ""
      +  "type": "minions"
         "weaknesses": ""
       }

      at D:\Projetos\Code\BossMachine\test\test.js:194:46
      at processTicksAndRejections (internal/process/task_queues.js:97:5)

  7) /api/minions routes
       DELETE /api/minions
         deletes the correct minion by id:
     Error: expected 204 "No Content", got 404 "Not Found"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  8) /api/ideas routes
       GET /ideas/:ideaId
         returns a single idea object:
     Error: expected 200 "OK", got 404 "Not Found"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  9) /api/ideas routes
       GET /ideas/:ideaId
         returns a full idea object with correct properties:
     Error: expected 200 "OK", got 404 "Not Found"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  10) /api/ideas routes
       GET /ideas/:ideaId
         returned idea has the correct id:
     Error: expected 200 "OK", got 404 "Not Found"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  11) /api/ideas routes
       PUT /api/ideas/:ideaId
         updates the correct idea and returns it:

      AssertionError: expected {} to deeply equal { name: 'Test' }
      + expected - actual

      -{}
      +{
      +  "name": "Test"
      +}

      at D:\Projetos\Code\BossMachine\test\test.js:344:44
      at processTicksAndRejections (internal/process/task_queues.js:97:5)

  12) /api/ideas routes
       PUT /api/ideas/:ideaId
         updates the correct idea and persists to the database:
     AssertionError: expected undefined to equal 'Persistence Test'
      at D:\Projetos\Code\BossMachine\test\test.js:368:44
      at processTicksAndRejections (internal/process/task_queues.js:97:5)

  13) /api/ideas routes
       POST /api/ideas
         should add a new idea if all supplied information is correct:

      AssertionError: expected { Object (name, description, ...) } to deeply equal { Object (name, description, ...) }
      + expected - actual

         "description": ""
         "id": "11"
         "name": "Test"
         "numWeeks": 10
      +  "type": "ideas"
         "weeklyRevenue": 200000
       }

      at D:\Projetos\Code\BossMachine\test\test.js:432:44
      at processTicksAndRejections (internal/process/task_queues.js:97:5)

  14) /api/ideas routes
       DELETE /api/ideas
         deletes the correct minion by id:
     Error: expected 204 "No Content", got 404 "Not Found"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  15) checkMillionDollarIdea middleware
       sends a 400 error if the total yield is less than one million dollars:
     AssertionError: expected null to equal 400
      at Context.<anonymous> (test\test.js:529:23)
      at processImmediate (internal/timers.js:456:21)

  16) checkMillionDollarIdea middleware
       sends a 400 error if numWeeks or weeklyRevenue is not supplied:
     AssertionError: expected null to equal 400
      at Context.<anonymous> (test\test.js:543:23)
      at processImmediate (internal/timers.js:456:21)

  17) checkMillionDollarIdea middleware
       sends a 400 error if numWeeks or weeklyRevenue is an invalid string:

      AssertionError: expected true to be false
      + expected - actual

      -true
      +false

      at Context.<anonymous> (test\test.js:553:29)
      at processImmediate (internal/timers.js:456:21)

  18) checkMillionDollarIdea middleware
       is used in a POST /api/ideas route to reject insufficiently profitable ideas:
     Error: expected 400 "Bad Request", got 201 "Created"
      at Test._assertStatus (node_modules\supertest\lib\test.js:268:12)
      at Test._assertFunction (node_modules\supertest\lib\test.js:283:11)
      at Test.assert (node_modules\supertest\lib\test.js:173:18)
      at Server.localAssert (node_modules\supertest\lib\test.js:131:12)
      at emitCloseNT (net.js:1657:8)
      at processTicksAndRejections (internal/process/task_queues.js:83:21)

  19) /api/meetings routes
       POST /api/meetings
         should persist the created meeting to the database:

      AssertionError: expected 3 to equal 4
      + expected - actual

      -3
      +4

      at D:\Projetos\Code\BossMachine\test\test.js:649:46
      at processTicksAndRejections (internal/process/task_queues.js:97:5)




I’m not the best at testing but I believe you may need to prepend all routes with /api/.

Example:
GET /minions/:minionID should be updated to /api/minions/:Id

In this project, it is already mounted elsewhere! That’s why it’s unecessary to mount in the api.js

I’ve just started working on the minions routes. The way I did it for the get request which returns an array of all the minions, was to create a new router and mount it at the /minions path.

This is what my minionsRouter.js file looks like:



// Create an instance of minionsRouter 
const minionsRouter = require('express').Router();

//export minionsRouter 
module.exports = minionsRouter; 

const {
    getAllFromDatabase
  } = require('./db'); 


// Get an array of all the minions 
minionsRouter.get('/', (req, res, next) => {
    res.send(getAllFromDatabase('minions'));
}); 

and my api.js file looks like:

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

// Tell our server to mount the router at these paths 
const minionsRouter = require('./minionsRouter'); 
apiRouter.use('/minions', minionsRouter); 


module.exports = apiRouter;

I hope that helps!

Thanks!! Taking a look at your code seems quite neat!! I’ll go back to the project and compare with mine :smiley:

1 Like