摩卡在测试后没有退出

我从 Node 中的测试开始,使用 mocha、 chai 和 nock (拦截外部 HTTPapi 调用)。

我已经写了3个测试,他们都是一个通过,但是,当我添加第三个测试,摩卡停止退出后,运行的测试,没有错误或任何错误的迹象。

如果我注释第三个测试,摩卡就会退出。

这是导致“问题”的测试:

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

38392 次浏览

I know it is a bit late to answer this, but I was facing a similar problem and saw your post.

In mocha 4.0.0, they changed the behavior of tests on 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?

In your case, it seems like a call to createReadStream() was never closed.

So, you have 2 options:

Option A: Close the fs and other streams open (recommended)

Option B: Run mocha with the --exit option.