简体   繁体   English

摩卡柴测试用例不会自动终止

[英]mocha chai test case does not terminate automatically

I have written following test cases on mocha-chai and it works fine and shows the all results passed successfully. 我在mocha-chai上编写了以下测试用例,并且工作正常并显示所有结果成功通过。 But it does not terminate the execution after successfully passed all 6 test cases. 但是在成功通过所有6个测试用例后,它不会终止执行。 I don't know what is the reason that why my test case does not terminate automatically, I need to click CTRL+C to terminate the test case. 我不知道为什么我的测试用例没有自动终止的原因,我需要单击CTRL + C来终止测试用例。

I don't know what is the reason test has been successfully passed but does not terminate. 我不知道测试成功通过的原因是什么,但没有终止。 I have also written done() at the end of the test case but still it doesn't work 我也在测试用例结束时编写了done(),但它仍然不起作用

I have used PostgreSQL as database and NodeJS as backend server. 我使用PostgreSQL作为数据库,NodeJS作为后端服务器。

test.js 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 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 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. 我不知道测试成功通过的原因是什么,但没有终止。

You're not closing your database handle when all tests have been run, which means that Node.js doesn't know that you're done. 在运行所有测试时,您没有关闭数据库句柄,这意味着Node.js不知道您已完成。

You can add a root-level after hook to close it: 您可以after挂钩after添加根级别来关闭它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM