lundi 2 juillet 2018

Jest not closing because of Mongoose.model

I am trying to create my first test with jest.

user_model_test.js

const mongoose = require('mongoose')
const User = require('../user_model')


describe('user model tests', () => {
  beforeAll( async () => {
    await mongoose.connect('mongodb://localhost/supertest21')
  })
  afterAll( async () => { 
    await mongoose.connection.close()
  })

  it("has a module", () => {
    expect(User).toBeDefined()
  })
})

user_model.js

const mongoose = require('mongoose')
const Schema = mongoose.Schema

const userSchema = new Schema({
  username: {
    type: String,
    required: true
  },
  password: {
    type: String,
    required: true
  },
  email: {
    type: String,
    required: true
  }
})

const User = mongoose.model('User', userSchema, 'user')

module.exports = User

When I run my test, running with --detectOpenHandles I get this error:

Jest has detected the following 1 open handle potentially keeping Jest from exiting:

  ●  PROMISE

      17 | })
      18 |
    > 19 | const User = mongoose.model('User', userSchema, 'user')
         |                       ^
      20 |
      21 | module.exports = User

      at Function.init (node_modules/mongoose/lib/model.js:970:16)
      at Mongoose.Object.<anonymous>.Mongoose.model (node_modules/mongoose/lib/index.js:396:11)
      at Object.<anonymous> (libs/user/user_model.js:19:23)
      at Object.<anonymous> (libs/user/__tests__/user_model_test.js:3:14)

I know that there is something to do with the mongoose.model initialization.When I pass the 4th parameter to mongoose.model, to skip initialization, the promise error don't show up but the test never closes and don't show any more errors. Any ideas?

Aucun commentaire:

Enregistrer un commentaire