Whenever a Mongoose model tries to load after it is already loaded, an error occurs, for example:
error: uncaughtException: Unable to compile Account model after compilation. date = Fri Feb 26 2016 10:13:40 GMT-0700 (MST), pid = 19231, uid = 502, gid = 20, cwd = / Users / me / PhpstormProjects / project, execPath = / usr / local / Basement / node / 0.12.4 / bin / node, version = v5.2.0, argv = [/usr/local/Cellar/node/0.12.4/bin/node,/usr/local/ Basement/node/0.12.4/bin / lab], rss = 73306112, heapTotal = 62168096, heapUsed = 29534752, loadavg = [1.6005859375, 1.84716796875, 1.8701171875], uptime = 648559 OverwriteModelError: Unable to overwrite Account model after compilation.
I feel good, but now that I am writing unit tests for my models, I have a problem.
Just basic file structure information ...
I have all the Mongoose models in separate files located inside the src/models/ folder, and to download these models you just need a folder that passes the Mongoose object to it and the src/models/index.js load all the models and return the model object. The index.js file can be seen here (And not that it is relevant, but the model names are mostly file names without .js)
Now module tests for models are also split into separate files. Theres one test file for each model. Although each unit test file focuses on a specific model, some of them also use other models (for tasks before and after).
Initial task
I just created the 2nd unit test file, and when I execute them myself, they work fine. But when I execute them all, I get the above error stating that I am trying to load models more than once. Which, since I require ./models in each case unit test, I am loading them more than once.
First resolution attempt
I thought that maybe I could clear all loaded models through after() in each individual unit test file, for example:
after(function(done) { mongoose.connection.close(function() { mongoose.connection.models = {} done() }) })
Which does not work at all (no new errors, but the same thing. It is impossible to overwrite the Account model after the compiled errors have been saved)
Second Permission Attempt (semi-successful)
Instead of models throwing an error in the last line when she tried to return Mongoose.model() , I insert some logic at the top of the model to check if the model is loaded, and if so, return which :
const thisFile = path.basename( __filename ).match( /(.*)\.js$/ )[ 1 ] const modelName = _.chain( thisFile ).toLower().upperFirst().value() module.exports = Mongoose => {
I am trying to do this in my models right now, and everything seems to be in order (well, I do not understand the above errors, saying that I am loading several models)
New problem
Now, when the module is being tested, I get an error message, the error is displayed once for the model, but with the same error:
$ lab .................................................. ... Test script errors: Cannot set property '0' of undefined at emitOne (events.js:83:20) at EventEmitter.emit (events.js:170:7) at EventEmitter.g (events.js:261:16) at emitNone (events.js:68:13) at EventEmitter.emit (events.js:167:7) Cannot set property '0' of undefined at emitOne (events.js:83:20) at EventEmitter.emit (events.js:170:7) at EventEmitter.g (events.js:261:16) at emitNone (events.js:68:13) at EventEmitter.emit (events.js:167:7) Cannot set property '0' of undefined at emitOne (events.js:83:20) at EventEmitter.emit (events.js:170:7) at EventEmitter.g (events.js:261:16) at emitNone (events.js:68:13) at EventEmitter.emit (events.js:167:7) There were 3 test script error(s). 53 tests complete Test duration: 1028 ms No global variable leaks detected
There are not many details in this stack trace.
I am not sure if this is caused by the code that I added to each model, if it was already loaded, if it was, it would appear when performing one unit test, or it would only show that it is impossible to set property “0” from undefined twice (once for successfully loading the initial model, and then twice for the next two ... I would think)
If anyone has any data, I would really appreciate it! Thanks
Update
I tried running lab --debug to get additional information, and while it does not show stack traces around errors that occur, it doubles them ... which is strange. So, if there was 2 when doing only lab , lab --debug shows 4
I also use Winston to do my logging. If I change the debug log level, which shows the lot of debug entries in the console, it does not show any entries about these errors ... Therefore, it makes me think that this cannot be caused by my scripts, but rather something in the dependencies testing modules?
Errors say that they come from the error.js file, but they no longer say. I tried to find error.js via find . -name 'events.js' find . -name 'events.js' , with no results .. Odd