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

skip doesn't work for nested describes #2683

Closed
kharandziuk opened this issue Jan 23, 2017 · 10 comments
Closed

skip doesn't work for nested describes #2683

kharandziuk opened this issue Jan 23, 2017 · 10 comments
Labels
type: question support question

Comments

@kharandziuk
Copy link

copied from my question on SO

I have some code below:

const assert = require('assert')

describe('server', function() {
  before(function() {
    // HACK: skip the tests in staging environment until we find to provide db in it
    if(process.env.NODE_ENV === 'staging') {
      this.skip();
    }
  });

  it('list request', function() {
    assert.fail('fails wo db')
  })
  describe('detail requests', function() {
    it('some arguments', function() {
      assert.fail('fails wo db')
    })
  })
})

When I run NODE_ENV='staging' npm test:

> @ test /Users/kharandz/Projects/mocha-bug
> mocha



  server
    - list request
    detail requests
      1) some arguments


  0 passing (10ms)
  1 pending
  1 failing

  1) server detail requests some arguments:
     AssertionError: 'fails wo db' undefined undefined
      at Context.<anonymous> (test/sample.spec.js:16:14)

But I expect that all the tests are skipped. So, the question:

  • How to achieve the expected behavior without copying before-code in every describe?
  • Is there any reason why it works like this?
@Munter
Copy link
Contributor

Munter commented Jan 28, 2017

this is a different suite for each describe.

I think it would make more sense for you to pull your condition out of the mocha internals altogether:

if(process.env.NODE_ENV !== 'staging') {
  describe('...', function () {
    describe('...', function () {
    });
  });
}

Does that solve your problem?

@Munter Munter added status: waiting for author waiting on response from OP - more information needed type: question support question labels Jan 28, 2017
@kharandziuk
Copy link
Author

kharandziuk commented Jan 30, 2017

Thanks for the answer. I actually do it like you propose, but technically its a hack. I want to have the output:
5 pending (or any other number).
With the current solution we are omitting the tests instead of skipping them

@Munter
Copy link
Contributor

Munter commented Jan 30, 2017

If I wanted to display pending tests I would probably do something like

var envDescribe = process.NODE_ENV === 'staging' ? describe.skip : describe;

envDescribe('...', function () {
  envDescribe('...', function () {
  });
});

@kharandziuk
Copy link
Author

it solves the issue, thx. If you want a few points on SO, feel free to copypaste you answer and I'll receive

@Munter
Copy link
Contributor

Munter commented Jan 30, 2017

I'm fine without the points. Happy testing :)

@Munter Munter closed this as completed Jan 30, 2017
@Munter Munter removed the status: waiting for author waiting on response from OP - more information needed label Jan 30, 2017
@kharandziuk
Copy link
Author

@Munter I found a problem with your solution. If somebody write something like envDescribe.skip, we will have a problem and it will appear only under some circumstances which makes it's really flaky

@ScottFreeCode
Copy link
Contributor

When that does appear, though, it should throw a hard error and block the test suite from even running (thus making the problem impossible to overlook), if I'm not mistaken?

A possible alternative may be to var envDescribe = process.NODE_ENV === 'staging' ? describe.skip : function() { return describe.apply(null, arguments) };, which, if I've written it correctly (not sure off the top of my head if null in the apply call would need to be this instead), would make envDescribe.skip(...) always be an error. If I'm also right that the error's obvious, perhaps that would do?

@timruffles
Copy link

timruffles commented May 3, 2017

This definitely is surprising behaviour, given that most other behaviour is nested by context (e.g describe.skip certainly is). Worth re-opening?

@alexcanessa
Copy link

Definitely worth re-opening as I'm trying to skip in an async function (and I don't think that wrapping everything in the callback is nice TBH).

@yuriiveremiienko
Copy link

for me worked setting pending prop to true

before(function () {
	this.test.parent.pending = true;
	this.skip();
});

-- skips current and all nested describes

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

No branches or pull requests

6 participants