项目作者: rockchalkwushock

项目描述 :
Jest playground for issue with jest terminating.
高级语言: JavaScript
项目地址: git://github.com/rockchalkwushock/jest-playground.git
创建时间: 2017-05-18T15:48:10Z
项目社区:https://github.com/rockchalkwushock/jest-playground

开源协议:Other

下载


Jest-Playground :white_check_mark: Solved :white_check_mark:

This repository is for figuring out the issue with jest not terminating when the testing suite completes successfully while using supertest to mock the api endpoints.

Using this repository

  1. git clone https://github.com/rockchalkwushock/jest-playground.git
  2. cd jest-playground
  3. yarn install
  4. yarn test

My System & Package Information

Tech Version
node 7.9.0
npm 4.5.0
yarn 0.24.4
macOS Sierra 10.12.4
jest 20.0.3 (package.json)

The Problem

jest never terminates upon completion of tests.

Script executing tests:

  1. jest --config jest.config.json # nope not --watch'ing'

Attempted Solutions

  1. "testEnvironment": "node" has been set from the beginning.
  2. jest.unmock(module) as per #997.
  3. Added afterEach() & have tried the afterAll() hook for closing connection to server.

Initially I believed #3 that this was the issue. That the server connection was remaining open
and jest could not close because of it. Looking at the below screenshots I don’t believe that
is the case anymore.

Screenshots

Upon executing yarn test the following is seen in the console:

After-Tests

Upon running the command:

  1. ps | grep node

I see the following for the two scripts in the package.json:

yarn start

yarn start

yarn test

yarn test

The Solution

As per discussion with @cpojer in #3602 the issue was not enough was being done in the afterEach() hook. The connection to MongoDB was remaining open because I was only dropping connection to the collection not the entire database.

The StackO post has been answered similarly as well.

  1. afterEach(async () => {
  2. try {
  3. const { todos } = mongoose.connection.collections;
  4. // Collection is being dropped.
  5. await todos.drop()
  6. // Connection to Mongo killed.
  7. await mongoose.disconnect();
  8. // Server connection closed.
  9. await server.close();
  10. } catch (error) {
  11. console.log(`
  12. You did something wrong dummy!
  13. ${error}
  14. `);
  15. throw error;
  16. }
  17. });