Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way of a programmatically skipping a test #1901

Closed
gajus opened this issue Sep 23, 2015 · 12 comments
Closed

Add a way of a programmatically skipping a test #1901

gajus opened this issue Sep 23, 2015 · 12 comments

Comments

@gajus
Copy link

gajus commented Sep 23, 2015

I have a code where certain tests will always fail in CI environment. I would like to disable them based on an environment condition.
How to programmatically skip a test in mocha during the runtime execution?

– http://stackoverflow.com/q/32723167/368691

@gajus
Copy link
Author

gajus commented Sep 23, 2015

There does not appear a way to conditionally skip tests from the program itself, e.g.

describe('foo', (d) => { 
    before(() => {
        d.skip();
    });

    it('foo', (i) => { 
        i.skip();
    });
});

@danielstjules
Copy link
Contributor

Hm, have you tried:

$ cat test.js
describe('foo', function() {
  before(function() {
    this.skip();
  });

  it('foo', function() {
    // will not run
    console.log('This will not be printed');
  });
});
$ mocha test.js


  foo
    - foo


  0 passing (9ms)
  1 pending

Async skip will hopefully land in the next minor release via #1618

@danielstjules
Copy link
Contributor

In your example above:

describe('foo', (d) => { 
    before(() => {
        d.skip();
    });
//...
});

d is undefined. Mocha doesn't pass anything to functions that wrap suites, only specs.

@gajus
Copy link
Author

gajus commented Sep 24, 2015

Where is this documented?

@lukealbao
Copy link

This is not documented as far as I can tell. I was looking to do the same thing and having trouble. The problem was an outdated version of Mocha. Updating to 2.3.3 allows me to do this:

describe('skipping', function () {
  it('should skip', function () {
    var u = false;
    this.skip();
    expect(u).to.equal(true); // <= Will not throw because it's skipped
  });
});

Of course, you can add conditional logic to skipping a test according to, say, environment variables.
You can see that this feature was added in 718c411.

@felixfbecker
Copy link

How about this?

describe('something', function () {
  (os === 'windows' ? it.skip : it)('should do something that doesnt work on windows', function () {

  })
})

@mislav
Copy link
Contributor

mislav commented Jan 10, 2016

Skipping tests is broken right now in 2 ways. See fixes in #1945 (comment).

  • It's broken when skip() is invoked within hooks such as before()
  • It's broken when skip() is called within individual test combined with HTML reporter.

@jdmarshall
Copy link

I think you telegraph the problem better if you tag the troublesome tests and use a mocha test filter in your build environment. In addition to tests failing in CI, it's not uncommon to have tests that fail only in Dev. note that I am not condoning this situation, only acknowledging that it exists.

I think conditional logic conceals problems that should be surfaced, and loudly. Nonfunctioning tests are legitimate concerns, and burying them in the spec file means fewer eyeballs on the problem. All else being equal, I'd prefer my test library didn't make it easier for my coworkers to sneakily disable tests to make the build green. I deal with enough of that sort of thing without mocha.js being an Enabler to that dysfunction.

@mislav
Copy link
Contributor

mislav commented Apr 2, 2016

Agreed that using skip too often in a test suite should be in general considered a code smell. However, for those rare cases where you need it (e.g. having a browser test suite that should run one test only in capable browsers, but that test needs to be skipped in PhantomJS due to lack of browser support) it would be great if skip just worked.

Right now Mocha offers this feature, but it wasn't working well historically (unsure of the state in latest stable version of Mocha). It would be great that if there's a test suite feature, it at least worked.

@jdmarshall
Copy link

And I'm going to reiterate that I believe tests that don't work in Phantom should be tagged as such and any build that runs on phantom should filter those tests out.

It is immediately clear from the build history that only part of the tests were run. This is IME no different than tagging tests as slow and running them in a separate build target or at the end of the test run.

@boneskull
Copy link
Contributor

The problems with this.skip() should be fixed in v3, if not master. Please reopen if master does not address the problem(s).

@BohdanKov
Copy link

https://mochajs.org/#arrow-functions

as docs says Passing arrow functions (“lambdas”) to Mocha is discouraged use function instead.
It helped me to use it in the following way

it('test case', async function() => {
   if (conditionToCheck) this.skip();

   // default logic to test
   ...
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants