Boss Machine Project: run test error with ideaId PUT and POST routes

4 fails on the run test and I do not understand why it’s sending a 400 bad request when I specified the status for each route error handling.

TERMINAL:

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

module.exports = app;

const PORT = process.env.PORT || 4001;

app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

const {
  createMeeting,
  getAllFromDatabase,
  getFromDatabaseById,
  addToDatabase,
  updateInstanceInDatabase,
  deleteFromDatabasebyId,
  deleteAllFromDatabase,
} = require('./server/db');

const checkMillionDollarIdea = require('./server/checkMillionDollarIdea');

// Middleware to validate ID parameter
const validateIdParam = (paramName) => {
  return (req, res, next) => {
    const id = req.params[paramName];
    if (!id || isNaN(id)) {
      return res.status(404).send();
    }
    next();
  };
};

// *** MINION ROUTES ***
app.get('/api/minions', (req, res) => {
  const minions = getAllFromDatabase('minions');
  res.send(minions);
});

app.post('/api/minions', (req, res) => {
  const newMinion = addToDatabase('minions', req.body);
  res.status(201).send(newMinion);
});

app.get('/api/minions/:minionId', validateIdParam('minionId'), (req, res) => {
  const minion = getFromDatabaseById('minions', req.params.minionId);
  if (!minion) {
    return res.status(404).send();
  }
  res.send(minion);
});

app.put('/api/minions/:minionId', validateIdParam('minionId'), (req, res) => {
  const updatedMinion = updateInstanceInDatabase('minions', req.body);
  if (!updatedMinion) {
    return res.status(404).send();
  }
  res.send(updatedMinion);
});

app.delete('/api/minions/:minionId', validateIdParam('minionId'), (req, res) => {
  const deleted = deleteFromDatabasebyId('minions', req.params.minionId);
  if (!deleted) {
    return res.status(404).send();
  }
  res.status(204).send();
});

// *** IDEA ROUTES ***
app.get('/api/ideas', (req, res) => {
  res.send(getAllFromDatabase('ideas'));
});

app.get('/api/ideas/:ideaId', validateIdParam('ideaId'), (req, res) => {
  const idea = getFromDatabaseById('ideas', req.params.ideaId);
  if (!idea) {
    return res.status(404).send();
  }
  res.send(idea);
});

app.post('/api/ideas', checkMillionDollarIdea, (req, res) => {
  const newIdea = addToDatabase('ideas', req.body);
  res.status(201).send(newIdea);
});

app.put('/api/ideas/:ideaId', validateIdParam('ideaId'), checkMillionDollarIdea, (req, res) => {
  const updatedIdea = updateInstanceInDatabase('ideas', req.body);
  if (!updatedIdea) {
    return res.status(404).send();
  }
  res.send(updatedIdea);
});

app.delete('/api/ideas/:ideaId', validateIdParam('ideaId'), (req, res) => {
  const deleted = deleteFromDatabasebyId('ideas', req.params.ideaId);
  if (!deleted) {
    return res.status(404).send();
  }
  res.status(204).send();
});

// *** MEETING ROUTES ***
app.get('/api/meetings', (req, res) => {
  res.send(getAllFromDatabase('meetings'));
});

app.post('/api/meetings', (req, res) => {
  const newMeeting = createMeeting();
  addToDatabase('meetings', newMeeting);
  res.status(201).send(newMeeting);
});

app.delete('/api/meetings', (req, res) => {
  deleteAllFromDatabase('meetings');
  res.status(204).send();
});

if (!module.parent) { 
  app.listen(PORT, () => {
    console.log(`Server is running on port: ${PORT}`);
  });
}

SERVER.JS FILE

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

module.exports = app;

const PORT = process.env.PORT || 4001;

app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

const {
  createMeeting,
  getAllFromDatabase,
  getFromDatabaseById,
  addToDatabase,
  updateInstanceInDatabase,
  deleteFromDatabasebyId,
  deleteAllFromDatabase,
} = require('./server/db');

const checkMillionDollarIdea = require('./server/checkMillionDollarIdea');

// Middleware to validate ID parameter
const validateIdParam = (paramName) => {
  return (req, res, next) => {
    const id = req.params[paramName];
    if (!id || isNaN(id)) {
      return res.status(404).send();
    }
    next();
  };
};

// *** MINION ROUTES ***
app.get('/api/minions', (req, res) => {
  const minions = getAllFromDatabase('minions');
  res.send(minions);
});

app.post('/api/minions', (req, res) => {
  const newMinion = addToDatabase('minions', req.body);
  res.status(201).send(newMinion);
});

app.get('/api/minions/:minionId', validateIdParam('minionId'), (req, res) => {
  const minion = getFromDatabaseById('minions', req.params.minionId);
  if (!minion) {
    return res.status(404).send();
  }
  res.send(minion);
});

app.put('/api/minions/:minionId', validateIdParam('minionId'), (req, res) => {
  const updatedMinion = updateInstanceInDatabase('minions', req.body);
  if (!updatedMinion) {
    return res.status(404).send();
  }
  res.send(updatedMinion);
});

app.delete('/api/minions/:minionId', validateIdParam('minionId'), (req, res) => {
  const deleted = deleteFromDatabasebyId('minions', req.params.minionId);
  if (!deleted) {
    return res.status(404).send();
  }
  res.status(204).send();
});

// *** IDEA ROUTES ***
app.get('/api/ideas', (req, res) => {
  res.send(getAllFromDatabase('ideas'));
});

app.get('/api/ideas/:ideaId', validateIdParam('ideaId'), (req, res) => {
  const idea = getFromDatabaseById('ideas', req.params.ideaId);
  if (!idea) {
    return res.status(404).send();
  }
  res.send(idea);
});

app.post('/api/ideas', checkMillionDollarIdea, (req, res) => {
  const newIdea = addToDatabase('ideas', req.body);
  res.status(201).send(newIdea);
});

app.put('/api/ideas/:ideaId', validateIdParam('ideaId'), checkMillionDollarIdea, (req, res) => {
  const updatedIdea = updateInstanceInDatabase('ideas', req.body);
  if (!updatedIdea) {
    return res.status(404).send();
  }
  res.send(updatedIdea);
});

app.delete('/api/ideas/:ideaId', validateIdParam('ideaId'), (req, res) => {
  const deleted = deleteFromDatabasebyId('ideas', req.params.ideaId);
  if (!deleted) {
    return res.status(404).send();
  }
  res.status(204).send();
});

// *** MEETING ROUTES ***
app.get('/api/meetings', (req, res) => {
  res.send(getAllFromDatabase('meetings'));
});

app.post('/api/meetings', (req, res) => {
  const newMeeting = createMeeting();
  addToDatabase('meetings', newMeeting);
  res.status(201).send(newMeeting);
});

app.delete('/api/meetings', (req, res) => {
  deleteAllFromDatabase('meetings');
  res.status(204).send();
});

if (!module.parent) { 
  app.listen(PORT, () => {
    console.log(`Server is running on port: ${PORT}`);
  });
}

I was wondering if the checkMillionDollarIdea.js had something to do with the failed test? Code for the PUT & POST routes for minionId and ideaId are identical except for the middleware function:

const checkMillionDollarIdea = (req, res, next) => 
    {
        const {numWeeks, weeklyRevenue} = req.body;

        if (!numWeeks || !weeklyRevenue) {
            res.status(400).send('Missing required properties');
            return;
        }

        const week = Number(numWeeks);
        const revenue = Number(weeklyRevenue);

        if (isNaN(week) || isNaN(revenue)) {
            res.status(400).send('Invalid numWeeks or weeklyRevenue');
            return;
        }

        const totalValue = week + revenue;

        if (totalValue >= 1000000) {
            next();
        } else {
            res.status(400).send('Must be a million dollar idea');
        }
    };

// Leave this exports assignment so that the function can be used elsewhere
module.exports = checkMillionDollarIdea;

Can’t wrap my head around this. Any help or tips would be very much appreciated, thank you!

I apologize, I did not double check my code here is the terminal output:

39 passing (462ms)
  18 pending
  4 failing

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

      AssertionError: expected {} to deeply equal { id: '1', name: 'Test', …(3) }
      + expected - actual

      -{}
      +{
      +  "description": "The name says it all!!!"
      +  "id": "1"
      +  "name": "Test"
      +  "numWeeks": 89
      +  "weeklyRevenue": 114368
      +}

      at C:\Users\emman\Desktop\project-4-boss-machine-start\project-4-boss-machine-start\test\test.js:344:44
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

  2) /api/ideas routes
       PUT /api/ideas/:ideaId
         updates the correct idea and persists to the database:

      AssertionError: expected 'Codecademy but for infomediaries' to equal 'Persistence Test'
      + expected - actual

      -Codecademy but for infomediaries
      +Persistence Test

      at C:\Users\emman\Desktop\project-4-boss-machine-start\project-4-boss-machine-start\test\test.js:368:44
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

  3) /api/ideas routes
       PUT /api/ideas/:ideaId
         called with an invalid ID returns a 404 error:
     Error: expected 404 "Not Found", got 400 "Bad Request"
      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 Object.onceWrapper (node:events:633:28)
      at Server.emit (node:events:519:28)
      at emitCloseNT (node:net:2340:8)
      at process.processTicksAndRejections (node:internal/process/task_queues:81:21)

  4) /api/ideas routes

  4) /api/ideas routes
       POST /api/ideas
         should add a new idea if all supplied information is correct:
     Error: expected 201 "Created", got 400 "Bad Request"
       POST /api/ideas
         should add a new idea if all supplied information is correct:
     Error: expected 201 "Created", got 400 "Bad Request"
      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 Object.onceWrapper (node:events:633:28)