gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:10:40 25 4
gpt4 key购买 nike

我在 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 钩子(Hook)来关闭它:

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

关于javascript - mocha chai 测试用例不会自动终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53866577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com