摩卡测试后未退出

Fed*_* E. 19 mocha.js node.js chai

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

我已经编写了3个测试,所有这些都通过了,但是,当我添加了第3个测试时,mocha在运行测试后停止退出,没有错误或任何错误的迹象。

如果我对第三项测试发表评论,那么摩卡咖啡就可以退出。

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

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);

    });

});
Run Code Online (Sandbox Code Playgroud)

再次,那个测试是通过,这使我感到困惑。

这是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);
                }
            });

        });

    }
Run Code Online (Sandbox Code Playgroud)

在其他2个测试中还调用了Tokenize(value)和Detokenize(value)函数,在运行时,mocha退出就可以了。

知道是什么原因造成的吗?

摩卡版本:5.1.1

Ale*_*aut 38

我知道现在回答这个问题有点晚了,但是我遇到了类似的问题,看到了您的帖子。

在摩卡4.0.0,他们改变了对finalization.From测试的行为在这里

如果在您的测试似乎“完成”之后,mocha进程仍然有效,则说明您的测试已安排了一些事情(异步),并且没有对其进行适当的清理。你没有打开插座吗?

在您的情况下,似乎createReadStream()从未终止对的致电。

因此,您有2个选择:

选项A:关闭fs,并打开其他流(推荐)

选项B:使用该--exit选项运行摩卡咖啡。

  • 这种行为确实很有帮助,我不建议强制您的测试退出。通常,当 mocha 挂起并且无法退出时,您的代码中有一些内容需要清理。最好找到问题所在。我通过这种方式发现了很多潜在的泄漏。 (5认同)
  • 不要强行关闭或阻止此行为。就我而言,MongoDB 驱动程序保持打开状态,最终使测试保持运行,就像 @reads0520 提到的那样。 (4认同)
  • 我要说的是,测试中的--exit选项就是这样! (3认同)
  • `--exit`选项对我有用!谢谢 (3认同)
  • 我使用 https://www.npmjs.com/package/why-is-node-running 来找出我的节点应用程序仍在运行的原因。 (3认同)