我的测试正在通过,但我必须按Ctrl + C结束测试。有什么想法吗?
spec.js
var app = require('./app');
var request = require('supertest');
var chai = require('chai').expect;
describe('contracts', function () {
it('should get all contracts', function () {
request(app)
.get('/Contracts')
.set('Accept', 'application/json')
.end(function (err, res) {
console.log(JSON.parse(res.text)[0].FirstName);
chai(res.body).to.be.an('array');
chai(res.status).equals(200);
});
});
});
contractService.js
var mysql = require('mysql');
const config = require('./config')
var con = mysql.createConnection({
host: config.HOST,
user: config.USER,
password: config.PASSWORD,
database: config.DATABASE
});
exports.get = function (req, res) {
con.connect(function (err) {
if (err) {
res.send(err)
};
con.query("SELECT * FROM " + config.DATABASE + ".Contracts", function (err, result, fields) {
if (err) {
res.send(err)
};
//console.log(result);
res.send(result)
});
});
}
app.js
const express = require('express');
const app = express();
app.use(express.static('client'));
var GetContracts = require('./contractsService');
app.get('/contracts', GetContracts.get);
module.exports = app;
答案 0 :(得分:0)
如果我在res.send之前放置一个con.end(),它将优雅地退出测试。
<强> contractsService.js 强> var mysql = require(&#39; mysql&#39;); const config = require(&#39; ./ config&#39;)
var con = mysql.createConnection({
host: config.HOST,
user: config.USER,
password: config.PASSWORD,
database: config.DATABASE
});
exports.get = function (req, res) {
con.connect(function (err) {
if (err) {
res.send(err)
};
con.query("SELECT * FROM " + config.DATABASE + ".Contracts", function (err, result, fields) {
if (err) {
res.send(err)
};
//console.log(result);
con.end(); //////////////////////right here
res.send(result)
});
});
}