Search code examples
node.jsmongodbmongoosemocha.js

Why do I get error "Cannot overwrite model once compiled" in Mongoose when I run my test a second time?


I have read related post: Cannot overwrite model once compiled Mongoose

Problem is neither of these solution's helped me with my problem.

I get the error in the title and I have following setup:

Folder Structure:

enter image description here

My models look like this:

forums.js

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const topicGroupSchema = require("./topicGroups");

const forumSchema = new Schema({
    title: String,
    topicGroups: [topicGroupSchema]
})

const Forum = mongoose.model("forums", forumSchema);

module.exports = Forum;

topicGroups.js

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const topicGroupSchema = new Schema({
    title: String,
   // Todo: add topics
})

module.exports = topicGroupSchema;

My test_helper and saveForum_test.js files look like this:

saveForum_test.js

const assert = require("assert");
const Forum = require("../model/forums")

describe("Creating records", () => {
    it("can save a new forum", (done) => {
        const forum = new Forum({
            title: "CodeHUB"
        })
        forum.save()
            .then(() => {
                assert(forum.isNew)
                done();
            })
    })
})

test_helper.js

const mongoose = require("mongoose");
mongoose.Promise = global.Promise;

before(done => {
    mongoose.connect("mongodb://myuser:[email protected]:21339/clicker", { useNewUrlParser: true });
    mongoose.connection
        .once("open", () => {done()})
        .on("error", error => {
            console.warn("error", error)
        })
})

// FIXME: error when saved twice

beforeEach(done => {
    console.log(mongoose.connection.collections.forums)
    mongoose.connection.dropCollection("forums", () => {
        done();
    })
})

So when I run my test suite with mocha, everything works as expected. But when I change something, and run it again, I get the error

OverwriteModelError: Cannot overwrite forums model once compiled.

I use mlab with mongoose and not a local installed mongodb. Maybe it has something todo with that? I can't figure it out, I checked all the files and imports etc. 10 times and can't find a mistake, can you ?


Solution

  • I have solved the problem.

    Turns out the problem was how I was running my test suite. My npm run test command in package.json did the following:

    "test": "mocha --watch"

    Here was the error. I think --watch doesn't reinstantiate everything and is just like "Hot Module Replacement".

    I installed nodemon and changed my test script like this:

    "test": "nodemon --exec \"mocha -R min\""

    This makes sure to rerun the whole files and no error is showing up.

    Another related post that should work: mocha --watch and mongoose models