4

I got two problems with this jest test:

  1. Is it possible to define the Content collection only once instead of doing that inside of the test?
  2. I do get this error:

    Jest did not exit one second after the test run has completed. This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with --detectOpenHandles to troubleshoot this issue.

I don't see why my async code weren't stopped...

import resolvers from 'resolvers/'
import Db from 'lib/db'
const db = new Db()

describe('Resolver', () => {
  let token

  beforeAll(async () => {
    await db.connect()
  })
  beforeEach(async () => {
    token = 'string'
    await db.dropDB()
  })
  afterAll(async () => {
    await db.connection.close()
  })

  describe('articleGetContent()', () => {
    test('should return dataset', async () => {
      // SETUP
      const Content = db.connection.collection('content')
      const docs = [{
        // some content...
      }]
      await Content.insertMany(docs)
      // EXECUTE
      const result = await resolvers.Query.articleGetContent({}, {
        id: '123,
        language: 'en'
      }, {
        token
      })
      // VERIFY
      expect.assertions(1)
      expect(result).toBeDefined()
    })
  })
})

resolver

import { articleGetContent } from '../models/article'

export default {
  Query: {
    articleGetContent: async (obj, { id }, { token }) => articleGetContent(id, token)
  }
}

This is how my db class looks like

db.js

export default class Db {
  constructor (uri, callback) {
    const mongo = process.env.MONGO || 'mongodb://localhost:27017'
    this.mongodb = process.env.MONGO_DB || 'testing'
    this.gfs = null
    this.connection = MongoClient.connect(mongo, { useNewUrlParser: true })
    this.connected = false
    return this
  }

  async connect (msg) {
    if (!this.connected) {
      try {
        this.connection = await this.connection
        this.connection = this.connection.db(this.mongodb)
        this.gfs = new mongo.GridFSBucket(this.connection)
        this.connected = true
      } catch (err) {
        console.error('mongo connection error', err)
      }
    }
    return this
  }

  async disconnect () {
    if (this.connected) {
      try {
        this.connection = await this.connection.close()
        this.connected = false
      } catch (err) {
        console.error('mongo disconnection error', err)
      }
    }
  }

  async dropDB () {
    const Content = this.connection.collection('content')
    await Content.deleteMany({})
  }
}
user3142695
  • 15,844
  • 47
  • 176
  • 332

1 Answers1

0

Related to the second question I hope you've found some issues on github about it. In general, the issue is described in the debug log. Jest works with promises, as a result, you shouldn't leave any async operations in any status except resolved.

In your case, you have your DB connection opened so you need to implement another method disconnect for your DB class, this link to docs will help you, but I guess you have it already as it's not the full db.js file ( I see some custom method dropDB. Main idea here is to have it in afterAll hook:

afterAll(() => db.disconnect());

Great example at the bottom of the page


What about the first question, it really depends on what you are doing in your method dropDB. If you're running method for dropping collection, you could store the reference to this collection somewhere outside and use it as it will automatically create the new one, but it would be great to see this method.


Additionally, your async test was created in a wrong way, you could read more here for example in my Update. You need to run this function in the beginning of the test: expect.assertions(number)

expect.assertions(number) verifies that a certain number of assertions are called during a test. This is often useful when testing asynchronous code, in order to make sure that assertions in a callback actually got called.

Yevhenii Herasymchuk
  • 2,047
  • 15
  • 20
  • Unfortunately my DB class seems to be wrong. I updated the code and tried to add the disconnect method, but this is currently failing. I added a `afterAll()` to the test, but still get the same bad result. – user3142695 Jan 06 '19 at 22:31
  • what does it mean `db.connection.close()` ? you've opened the connection to DB before all tests and you need to close connection to DB afterAll – Yevhenii Herasymchuk Jan 07 '19 at 09:02
  • I've sent you a link in post: https://mongodb.github.io/node-mongodb-native/api-generated/db.html#close – Yevhenii Herasymchuk Jan 07 '19 at 09:04