diff --git a/test/model.test.js b/test/model.test.js index be431564144..49007fd537a 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -3,7 +3,7 @@ /** * Test dependencies. */ - +const sinon = require('sinon'); const start = require('./common'); const assert = require('assert'); @@ -6756,6 +6756,29 @@ describe('Model', function() { ); }); + it('creates indexes only when they do not exist on the mongodb server (gh-12250)', async() => { + const userSchema = new Schema({ + name: { type: String } + }, { autoIndex: false }); + + userSchema.index({ name: 1 }); + + const User = db.model('User', userSchema); + + const createIndexSpy = sinon.spy(User.collection, 'createIndex'); + const listIndexesSpy = sinon.spy(User.collection, 'listIndexes'); + + // Act + await User.syncIndexes(); + assert.equal(createIndexSpy.callCount, 1); + assert.equal(listIndexesSpy.callCount, 1); + + await User.syncIndexes(); + + // Assert + assert.equal(listIndexesSpy.callCount, 2); + assert.equal(createIndexSpy.callCount, 1); + }); }); it('using `new db.model()()` (gh-6698)', function() {