Personal Library: App not working in test mode

This question is about Personal Library Project
if NODE_ENV=test,app returning 404 for all routes.If I comment out; in server.js,then everything works fine.
I am suspecting my code in server.js is not compatible with testrunner code.
Is there anyway I could resolve this issue?


'use strict';

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

const myDB = require('./db.js');
const apiRoutes         = require('./routes/api.js');
const fccTestingRoutes  = require('./routes/fcctesting.js');
const runner            = require('./test-runner');

const app = express();

app.use('/public', express.static(process.cwd() + '/public'));

app.use(cors({origin: '*'})); //USED FOR FCC TESTING PURPOSES ONLY!

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

myDB(async (client) => {
   const myDataBase = await client.db('myFirstDatabase');

  //Index page (static HTML)
    .get(function (req, res) {
      res.sendFile(process.cwd() + '/views/index.html');

  //For FCC testing purposes

  //Routing for API 

  //404 Not Found Middleware
  app.use(function(req, res, next) {
      .send('Not Found');

//Start our server and tests!
let PORT = process.env.PORT || 3000;
app.listen(PORT, function () {
  console.log("Listening on port " + PORT);
  if(process.env.NODE_ENV==='test') {
    console.log('Running Tests...');
    setTimeout(function () {
      try {;
      } catch(e) {
        let error = e;
          console.log('Tests are not valid:');
    }, 1500);

module.exports = app; //for unit/functional testing


const { MongoClient } = require('mongodb');

async function main(callback) {
    const URI = process.env.MONGO_URI; // Declare MONGO_URI in your .env file
    const client = new MongoClient(URI, { useNewUrlParser: true, useUnifiedTopology: true });

    try {
        // Connect to the MongoDB cluster
        await client.connect();

        // Make the appropriate DB calls
        await callback(client);
    } catch (e) {
        // Catch any errors
        throw new Error('Unable to Connect to Database')

module.exports = main;

I think you will have to post the link to your repl. Or post what you have in the tests.

I don’t actually know this and I didn’t really look, but I suspect any routes in server.js that are missing in 2_functional-tests.js will just not get hit when running in test mode.

NODE_ENV has been set to test.
My Repl

when test-runner delay 1500ms(default) I found that apiroutes is being called after running tests.

Listening on port 3000
Running Tests...

  Functional Tests
    1) #example Test GET /api/books

  0 passing (69ms)
  1 failing

  1) Functional Tests #example Test GET /api/books:

      Uncaught AssertionError: expected 404 to equal 200
      + expected - actual

      at tests/2_functional-tests.js:26:16
      at Test.Request.callback (node_modules/superagent/lib/node/index.js:716:12)
      at IncomingMessage.<anonymous> (node_modules/superagent/lib/node/index.js:916:18)
      at endReadableNT (_stream_readable.js:1241:12)
      at processTicksAndRejections (internal/process/task_queues.js:84:21)

db connected
apiRoutes is being called

So, I Increased the test-runner delay to 5000ms.

after increasing delay

> node server.js

Listening on port 3000
Running Tests...
db connected
apiRoutes is being called

  Functional Tests
    ✓ #example Test GET /api/books (409ms)

  1 passing (432ms)

Now,tests are passing but in both cases(before and after changing delay),while the server is still listening it shows # Hmmmm.... We Couldn't Reach Your Repl after running tests.

It’s working ,I think it’s a replit issue.