api-design-node-v3
api-design-node-v3 copied to clipboard
Unable to run tests
[email protected] test-routes npm run test -t router
[email protected] test cross-env NODE_ENV=testing jest --forceExit --detectOpenHandles --silent "router"
RUNS src/resources/item/tests/item.router.spec.js node:internal/process/promises:246 triggerUncaughtException(err, true /* fromPromise */); ^
[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "TypeError: Cannot read properties of undefined (reading 'dropDatabase')".] { code: 'ERR_UNHANDLED_REJECTION' }
I was able to get past this by editing test-db-setup.js and changing the afterEach() function to include a try/catch:
afterEach(async done => {
try {
await mongoose.connection.db.dropDatabase()
await mongoose.disconnect()
} catch (err) {
console.log(err)
}
await mongoose.disconnect()
return done()
})
afterAll(done => {
return done()
})
On windows, I edited the test-db-setup.js file and also the dev.js and testing.js files contained in the config folder.
I changed all the instances of mongodb://localhost:27017
to mongodb://0.0.0.0:27017
I got this from a (stackoverflow)[https://stackoverflow.com/questions/46523321/mongoerror-connect-econnrefused-127-0-0-127017] answer, specifically the second answer.