mocha chai 测试用例不会自动终止

mocha chai test case does not terminate automatically

我在 mocha-chai 上编写了以下测试用例,它运行良好并显示所有结果都成功通过。但它不会在成功通过所有 6 个测试用例后终止执行。不知道是什么原因导致我的测试用例没有自动终止,需要按CTRL+C来终止测试用例

我不知道是什么原因测试已经成功通过但没有终止。我还在测试用例的末尾写了 done() 但它仍然不起作用

我使用 PostgreSQL 作为数据库,使用 NodeJS 作为后端服务器。

test.js

const { colors } = require('mocha/lib/reporters/base');

colors.pass = 32;
const { describe, it } = require('mocha');
const { expect } = require('chai');
const request = require('superagent');
const should = require('should');
const fs = require('fs');
const path = require('path');
const moment = require('moment-timezone');

const envPath = path.join(__dirname, '.env');
require('dotenv').config({ path: envPath });

const Sequelize = require('sequelize');
const db = require('../db/models');

// Test that server is running or not
describe('Unit tests for add user', () => {
    // Gateway api test case and it's works fine
});

// Read the test cases stored from database and stored in an array
describe('Test Cases', () => {
    // Read the test cases from database
});

// Test case to write test data in csv and upload the csv and insert user information in csv

describe('Process data user csv', () => {    

    it('create user csv to write data in csv', done => {
        // Create user csv from test data and it's passed and successfully completed it
    });

    it('Upload user csv and insert the user in the database with accurate data', done => {
        // Upload user csv and insert user information from csv and it's passed and successfully completed it
    });

    it('verify user information from user table', done => {
        // This method verifies the user data stored user table, which has inserted in test case.
        // Sequlieze query which fetch the user information 

        TestService.executeQuery(
            dbQuery.qGetUserDetails,
            {
            email: arrTestCases[0].expected_result.email,
            },
            'select'
        )
        .then(data => {
            should(data[0].email).equal(arrTestCases[0].expected_result.email);
            done();
        });        
    });
  });
});

app.query.js

/**
 * @description default queries
 */
module.exports = {
  qGetUserDetails: `select * from "Users" where email = :email and is_active = true and is_deleted = false`,
};

Test.service.js

/**
 * @class TestService
 */
const Sequelize = require('sequelize');
const db = require('../db/models');

module.exports = class TestService {

  /**
   * @static execute query
   * @param {*} query
   * @param {*} replacements
   * @param {*} operation
   * @memberof TestService
   */
  static executeQuery(query, replacements, operation) {
    return new Promise((resolve, reject) => {
      let queryType;
      if (operation === 'insert') {
        queryType = Sequelize.QueryTypes.INSERT;
      } else if (operation === 'update') {
        queryType = Sequelize.QueryTypes.UPDATE;
      } else if (operation === 'select') {
        queryType = Sequelize.QueryTypes.SELECT;
      } else if (operation === 'delete') {
        queryType = Sequelize.QueryTypes.DELETE;
      } else {
        queryType = Sequelize.QueryTypes.SELECT;
      }
      return db.sequelize
        .query(query, { replacements, type: queryType, raw: true })
        .then(data => resolve(data))
        .catch(err => reject(err));
    });
  }
};

I don't know what is the reason test has been successfully passed but does not terminate.

当所有测试都已 运行 时,您没有关闭数据库句柄,这意味着 Node.js 不知道您已完成。

您可以添加根级 after 挂钩来关闭它:

after(() => {
  sequelize.close();
});