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 feature to allow user to define a function for next Suite selection #4591

Closed
RossVertizan opened this issue Mar 1, 2021 · 1 comment
Closed
Labels
status: waiting for author waiting on response from OP - more information needed type: feature enhancement proposal

Comments

@RossVertizan
Copy link

RossVertizan commented Mar 1, 2021

Is your feature request related to a problem or a nice-to-have?? Please describe.
This feature request is a nice-to-have.

Currently, the runner simply iterates through the Suites using:

var curr = suite.suites[i++]

because this is the standard usage model for Mocha. However, there may be a number of applications where users would like to be able to specify a function for how to select the next suite to run, maybe based on the outcome of the previous suite. For example, one can imagine building logic to skip Suites if a previous suite had failed. In my case I am wanting to integrate our Vitaq tool with Webdriverio and Mocha for use for web and app testing. Mocha is invoked from within a plugin maintained by Webdriverio. The Vitaq tool selects the next action (Suite) to run using AI and functional coverage targets to achieve the user journeys specified by the user. In order to re-use the Mocha framework, it would be necessary to have a function to select the next action.

Describe the solution you'd like
I have developed a prototype solution to demonstrate the idea. In the file runner.js, in the method runSuite I have replaced the line 883:

var curr = suite.suites[i++]

with:

if (typeof opts.nextSuiteSelectFunction === 'undefined') {
      curr = suite.suites[i++];
}
else {
    try {
        curr = eval(opts.nextSuiteSelectFunction);
    } 
    catch (err) {
        debug(err);
        console.error(err);
    }
}

I have assumed that the nextSuiteSelectFunction be passed as an option, and is a function that has a signature of (suite, curr), thereby allowing the user function to determine the state of the previous suite (passing/failing) and make selections about which suite to run next. The user function would be responsible for handling hierarchical suites if they were to be used. This solution requires the options to be passed through to the runner object and into the runSuite method. I don't like the fact that I have used the eval function, but as a proof of concept I think this is OK for now. The function itself is made available on the global namespace. I accept there are a number of things that could be tidied up here and I'm sure those more familiar with Mocha may have better suggestions regarding implementation details.

Describe alternatives you've considered
This is the cleanest way I could see to implement this functionality. I looked at hooks, but as far as I can see they are only for use within the Suite itself.

Additional context
No additional context

@RossVertizan RossVertizan added the type: feature enhancement proposal label Mar 1, 2021
RossVertizan added a commit to RossVertizan/vitaq-mocha that referenced this issue Mar 9, 2021
@outsideris
Copy link
Contributor

How about you use this.skip() like:

describe('suite', function() {
  let shouldRun = true;
  it('test case', function() {
    try {
      throw new Error();
    } catch(e) {
      shouldRun = false;
    }
  });

  it('test case 2', function() {
    if (!shouldRun) {
      this.skip();
    }
  });
});

@outsideris outsideris added the status: waiting for author waiting on response from OP - more information needed label Mar 15, 2021
@outsideris outsideris added the stale this has been inactive for a while... label Mar 27, 2021
@github-actions github-actions bot removed the stale this has been inactive for a while... label Mar 28, 2021
@juergba juergba closed this as completed Jun 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting for author waiting on response from OP - more information needed type: feature enhancement proposal
Projects
None yet
Development

No branches or pull requests

3 participants