测试后摩卡不退出
Mocha not exiting after test
我开始在 Node.js 中进行测试。使用 mocha、chai 和 nock(拦截外部 HTTP api 调用)。
我写了 3 个测试,它们都通过了,但是,当我添加第 3 个测试时,mocha 在 运行 测试后停止退出,没有错误或任何错误的迹象。
如果我评论第 3 个测试,mocha 退出就好了。
这是导致 'issue' 的测试:
describe('tokenizer.processFile(req, \'tokenize\')', () => {
it('should tokenize a file', async () => {
req = {
file: {
originalname: 'randomcards.txt',
buffer: cardsFile_buffer
},
user: {
displayName: user
}
};
expect(Buffer.from(await tokenizer.processFile(req, 'tokenize'))).to.deep.equal(tokensFile_buffer);
});
});
同样,那个测试是通过的,这让我很困惑。
这是tokenizer.processFile的代码:
processFile: function(req, whatTo){
combinedLogger.info(`Request to ${whatTo} ${req.file.originalname} received. Made by: ${req.user.displayName}`);
return new Promise(function(resolve, reject){
const lines = [], responses = [];
const lineReader = require('readline').createInterface({
input: require('streamifier').createReadStream(req.file.buffer)
});
lineReader.on('line', line => {
lines.push(line);
});
lineReader.on('close', async () => {
//process every line sequentially
try {
//ensure DB connected to mass insert
await db_instance.get_pool();
for(const line of lines) {
var response;
req.current_value = line;
if (whatTo == 'tokenize'){
response = await Tokenize(line);
db_instance.insertAction(req, 'tokenize', response);
}
else if (whatTo == 'detokenize'){
combinedLogger.info(`Request to detokenize ${line} received. Made by: ${req.user.displayName}`);
response = await Detokenize(line);
db_instance.insertAction(req, 'detokenize', line);
}
responses.push(response);
}
resolve(responses.join("\r\n"));
}
catch(error){
reject(error);
}
});
});
}
函数 Tokenize(value) 和 Detokenize(value) 在其他 2 个测试中也被调用,当 运行 时,mocha 退出就好了。
知道是什么原因造成的吗?
摩卡版本:5.1.1
我知道回答这个问题有点晚了,但我遇到了类似的问题并且看到了你的 post。
在 mocha 4.0.0 中,他们更改了 finalization.From here:
上的测试行为
If the mocha process is still alive after your tests seem "done", then your tests have scheduled something to happen (asynchronously) and haven't cleaned up after themselves properly. Did you leave a socket open?
在您的情况下,似乎从未关闭对 createReadStream()
的调用。
那么,你有两个选择:
选项A:关闭fs并打开其他流(推荐)
选项 B: 运行 mocha 与 --exit
选项。
我开始在 Node.js 中进行测试。使用 mocha、chai 和 nock(拦截外部 HTTP api 调用)。
我写了 3 个测试,它们都通过了,但是,当我添加第 3 个测试时,mocha 在 运行 测试后停止退出,没有错误或任何错误的迹象。
如果我评论第 3 个测试,mocha 退出就好了。
这是导致 'issue' 的测试:
describe('tokenizer.processFile(req, \'tokenize\')', () => {
it('should tokenize a file', async () => {
req = {
file: {
originalname: 'randomcards.txt',
buffer: cardsFile_buffer
},
user: {
displayName: user
}
};
expect(Buffer.from(await tokenizer.processFile(req, 'tokenize'))).to.deep.equal(tokensFile_buffer);
});
});
同样,那个测试是通过的,这让我很困惑。
这是tokenizer.processFile的代码:
processFile: function(req, whatTo){
combinedLogger.info(`Request to ${whatTo} ${req.file.originalname} received. Made by: ${req.user.displayName}`);
return new Promise(function(resolve, reject){
const lines = [], responses = [];
const lineReader = require('readline').createInterface({
input: require('streamifier').createReadStream(req.file.buffer)
});
lineReader.on('line', line => {
lines.push(line);
});
lineReader.on('close', async () => {
//process every line sequentially
try {
//ensure DB connected to mass insert
await db_instance.get_pool();
for(const line of lines) {
var response;
req.current_value = line;
if (whatTo == 'tokenize'){
response = await Tokenize(line);
db_instance.insertAction(req, 'tokenize', response);
}
else if (whatTo == 'detokenize'){
combinedLogger.info(`Request to detokenize ${line} received. Made by: ${req.user.displayName}`);
response = await Detokenize(line);
db_instance.insertAction(req, 'detokenize', line);
}
responses.push(response);
}
resolve(responses.join("\r\n"));
}
catch(error){
reject(error);
}
});
});
}
函数 Tokenize(value) 和 Detokenize(value) 在其他 2 个测试中也被调用,当 运行 时,mocha 退出就好了。
知道是什么原因造成的吗?
摩卡版本:5.1.1
我知道回答这个问题有点晚了,但我遇到了类似的问题并且看到了你的 post。
在 mocha 4.0.0 中,他们更改了 finalization.From here:
上的测试行为If the mocha process is still alive after your tests seem "done", then your tests have scheduled something to happen (asynchronously) and haven't cleaned up after themselves properly. Did you leave a socket open?
在您的情况下,似乎从未关闭对 createReadStream()
的调用。
那么,你有两个选择:
选项A:关闭fs并打开其他流(推荐)
选项 B: 运行 mocha 与 --exit
选项。