摩卡测试后未退出 [英] Mocha not exiting after test

查看:71
本文介绍了摩卡测试后未退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm starting with tests in Node. Using mocha, chai and nock (to intercept external HTTP api calls).

我已经编写了3个测试,但是所有测试都是通过的,但是当我添加第3个测试时测试中,摩卡咖啡在运行测试后停止退出,没有任何错误或任何错误提示。

I have written 3 tests, all of them are a pass, however, when I added the 3rd test, mocha stopped exiting after running the tests, with no error or indication of anything wrong.

如果我对第3个测试发表评论,摩卡咖啡就可以退出。

If I comment the 3rd test, mocha exits just fine.

这是导致问题的测试:

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

    });

});

同样,该测试是通过的,这让我感到困惑。

Again, that test is a pass, which baffles me.

这是tokenizer.processFile的代码:

Here's the code of 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)也是

Functions Tokenize(value) and Detokenize(value) are also called in the other 2 tests, which when run, mocha exits just fine.

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

Any idea what's causing this?

Mocha版本:5.1.1

Mocha version: 5.1.1

推荐答案

我知道回答这个问题有点晚了,但是我面临着一个类似的问题问题,并看到了您的帖子。

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

在mocha 4.0.0中,他们更改了完成测试的行为。来自此处

In mocha 4.0.0, they changed the behavior of tests on finalization.From here:


如果mocha进程为在您的测试似乎完成之后仍然存在,那么您的测试已安排了一些事情(异步),并且没有对自己进行适当的清理。您是否将套接字保持打开状态?

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()<的调用/ code>从未关闭。

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

因此,您有2个选择:

选项A:关闭fs,打开其他流(建议)

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

选项B:使用<$ c $运行摩卡c>-exit 选项。

这篇关于摩卡测试后未退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆