How to programmatically skip a test in mocha?

前端 未结 14 1129
失恋的感觉
失恋的感觉 2020-12-07 17:31

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

相关标签:
14条回答
  • 2020-12-07 17:52

    I am not sure if this qualifies as „programmatic skipping“, but in order to selectively skip some specific tests for our CI environment, I use Mocha's tagging feature (https://github.com/mochajs/mocha/wiki/Tagging). In describe() or it() messages, you can add a tag like @no-ci. To exclude those tests, you could define a specific "ci target" in your package.json and use --grep and --invert parameters like:

    "scripts": {
      "test": "mocha",
      "test-ci" : "mocha --reporter mocha-junit-reporter --grep @no-ci --invert"
    }
    
    0 讨论(0)
  • 2020-12-07 17:54

    We can write a nice clean wrapper function to conditionally run tests as follows:

    function ifConditionIt(title, test) {
      // Define your condition here
      return condition ? it(title, test) : it.skip(title, test);
    }
    

    This can then be required and used in your tests as follows:

    ifConditionIt('Should be an awesome test', (done) => {
      // Test things
      done();
    });
    
    0 讨论(0)
提交回复
热议问题