如何使用Mocha成功忽略规格? [英] How to successfully ignore specs with mocha?

查看:88
本文介绍了如何使用Mocha成功忽略规格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用摩卡咖啡,并且使用跳过"和仅"来运行特定的规格和测试.

I use mocha, and I use "skip" and "only" to run specific specs and tests.

但是似乎每次摩卡咖啡仅在测试中应用这些.

But it seems that each time mocha applies these only on the tests.

所以,如果我有此代码:

so if I have this code:

var expect = require('expect.js');
var logger = require('log4js').getLogger('TestDemo');

describe('test', function(){
    logger.info('this will appear');
    it('should not appear', function(){
        expect(1+1).to.be(5);
    });
});


describe.only('should run', function(){
    logger.info('this will appear to');
    it('should appear', function(){
        expect(5).to.be(5);
    })
});

输出为:

[2014-12-12 13:38:37.276] [INFO] TestDemo - this will appear
[2014-12-12 13:38:37.278] [INFO] TestDemo - this will appear to

但是-第一个是意外的,因为我使用的是describe.only,我不希望看到任何其他规格的印刷品.

However - the first one is unexpected as I use describe.only I don't expect to see prints from any other spec.

阅读他们的文档,它应该可以按我期望的方式工作,但事实并非如此.

Reading their documentation, it should work as I expect it, but it doesn't.

 by appending .skip() you may tell Mocha to simply ignore these suite(s) and test-case(s)

但是,mocha似乎并没有忽略套件,而只是忽略了测试用例.

However it seems mocha does not ignore the suite, only the test-case.

我该如何实现?

推荐答案

获得的信息与Mocha如何发现测试有关.摩卡咖啡基本上是这样做的:

What you get has to do with how Mocha discovers your test. Basically Mocha does this:

  1. 读取所有测试文件并执行它们.传递给describe的回调将立即执行.传递给it和钩子(beforebeforeEach等)的回调记录为以便以后执行.

  1. Read all your test files and execute them. The callbacks passed to describe are executed right away. The callbacks passed to it and to the hooks (before, beforeEach, etc.) are recorded for later execution.

Mocha将执行已记录的内容以供以后执行(根据一些明智的顺序,在此并不重要).

Mocha executes what it has recorded for later execution (according to some sensible order which is not important here).

当您在第二个describe上指定.only时,会发生以下情况:Mocha不会 知道您只想执行此describe直到执行它.因此,当它遇到第一个describe时,没有理由跳过它.因此,它执行传递给它的回调.

What happens when you specify .only on your second describe is that Mocha does not know that you want to execute only this describe block until it executes it. So when it runs into the first describe it has no reason yet to skip it. So it executes the callback passed to it.

在一天结束时,尽管Mocha完全在执行您告诉它执行的测试. (当我在此处运行代码时,Mocha运行以1 passing结尾,这意味着恰好执行了一个测试并通过了.)第一个describe中的 test 被忽略,而第二个describe中的测试一个被执行.如果您希望这样做,则仅当要执行describe块中的一个或多个测试时,才执行对logger.info的调用 ,然后将它们放在before挂钩中:

At the end of the day, though Mocha is executing exactly the tests you told it to execute. (When I run your code here, the Mocha run ends with 1 passing meaning exactly one test was executed and it passed.) The test in the first describe is ignored and the test in the second one is executed. If you want to make it so that your calls to logger.info will be executed only if one or more test in your describe block is going to be executed, then put them in before hooks:

var expect = require('expect.js');
var logger = require('log4js').getLogger('TestDemo');

describe('test', function(){
    before(function () {
        logger.info('this will appear');
    });
    it('should not appear', function(){
        expect(1+1).to.be(5);
    });
});


describe.only('should run', function(){
    before(function () {
        logger.info('this will appear too');
    });
    it('should appear', function(){
        expect(5).to.be(5);
    })
});

这篇关于如何使用Mocha成功忽略规格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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