Listen to the 'reloading' event of watch (e.g. to close db connection) in mocha test

62 views Asked by At

I have got this error:

Could not connect to MongoDB!

Error: Trying to open unclosed connection.

OverwriteModelError: Cannot overwrite Article model once compiled.

when I use the --watch to run my mocha test. It is because the test runner is trying to reload the application (server.js) and it is trying to recompile models.

I find several related questions on SO but they do not specifically apply to the --watch scenario.

1

There are 1 answers

0
Archer On

I've solved that by modifying statement:

var People = mongoose.model('people', mongose.Schema({...});

to read

var People = global.People = global.People || mongoose.model('people', mongose.Schema({...});