【问题标题】:Mocha not exiting after test摩卡在测试后没有退出
【发布时间】:2018-10-26 15:00:24
【问题描述】:

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

我写了 3 个测试,都通过了,但是,当我添加第 3 个测试时,运行测试后 mocha 停止退出,没有错误或任何错误指示。

如果我评论第三个测试,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);

    });

});

再次,该测试是通过,这让我感到困惑。

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),运行时,mocha 退出就好了。

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

摩卡版本:5.1.1

【问题讨论】:

    标签: node.js mocha.js chai


    【解决方案1】:

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

    在 mocha 4.0.0 中,他们改变了关于终结的测试行为。来自here

    如果在您的测试看起来“完成”之后 mocha 进程仍然存在,那么您的测试已经安排了一些事情发生(异步)并且没有正确清理自己。你有没有打开一个套接字?

    在您的情况下,对createReadStream() 的调用似乎从未关闭。

    所以,你有两个选择:

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

    选项 B: 使用 --exit 选项运行 mocha。

    【讨论】:

    • 我正要说,你的测试中的 --exit 选项就是这样!
    • 这种行为真的很有帮助,我不建议强制你的测试退出。通常当 mocha 挂起并且不会退出时,您的代码中有一些东西需要清理。最好能找到问题。我通过这种方式发现了很多潜在的漏洞。
    • 这救了我的命!
    • 我使用npmjs.com/package/why-is-node-running 找出我的节点应用程序仍在运行的原因。
    • 不要强制关闭或阻止此行为。就我而言,MongoDB 驱动程序保持打开状态,最终保持测试运行,就像提到的 @reads0520 一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多