mocha icon indicating copy to clipboard operation
mocha copied to clipboard

Parallel tests inside suite (was: in addition to it, will)

Open nopeless opened this issue 5 years ago • 1 comments

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

this will allow better aesthetics

// resolve after t ms; reject after t ms
const sleep = (t) => new Promise(r => setTimeout(r, t));
const sleepError = (t) => new Promise((_, r) => setTimeout(() => r(new Error(`random reason`)), t));

describe(`batch of asyncs`, function() {
  // and this lint error should be suppressed as well
  /* eslint-disable mocha/no-setup-in-describe */

  // set them to reject by default to prevent tests from being resolved when no task was assigned in the first place
  const e = Promise.reject(new Error("test not specified"));  
  let task1 = e, task2 = e, task3 = e;
  before(function () {
    task1 = sleep(1000);
    task2 = sleepError(2000);
    task3 = sleep(3000);
    // wait for these to resolve
    return Promise.allSettled([task1, task2, task3]);
  });
  it(`task1: should be accepted`, function() {
    return task1;
  });
  it(`task2: should be rejected`, function() {
    return task2;
  });
  it(`task3: should be accepted`, function() {
    return task3;
  });
});

into

// resolve after t ms; reject after t ms
const sleep = (t) => new Promise(r => setTimeout(r, t));
const sleepError = (t) => new Promise((_, r) => setTimeout(() => r(new Error(`random reason`)), t));

describe(`batch of asyncs`, function() {
  will(`be accepted`, function() {
    return sleep(1000)
  });
  will(`be rejected`, function() {
    return sleepError(1000)
  });
  will(`be accepted`, function() {
    return sleep(3000)
  });
});

fixes:

  • task1, 2, 3 will have proper timing reports
  • the logs will be in sequential order of success

breaks:

  • this means that tests using "will" will not be in order in code(or, delay the output of other tests to ensure that the print order is preserved).

optional:

  • slow error (5012ms) -> slow error (suite -> 5012ms) to indicate that the test was not done in serial

Describe alternatives you've considered The before hook is just too verbose(and goes against a mocha eslint rule), and using parallel tests seems overkill

Additional context I'm willing to make a fork myself

Additional features if possible describeAsync an asynchronous suite. Lists all output to a single array then prints when the suite is complete. If all other tests are done, behave like describe and print reports normally. mocha/no-root-describeAsync disables an asynchronous describe on root level, which will cause major tests to shift order

nopeless avatar Feb 25 '21 19:02 nopeless

This will require a rewrite of a good section of the codebase though I'll think about it after #4792 gets resolved

nopeless avatar Dec 03 '21 12:12 nopeless

describeAsync an asynchronous suite

Tracked by #2116.

was ... will ... This will require a rewrite of a good section of the codebase though

Per #5027, we're not planning on adding in any major changes. Feel free to write this as a set of functions that eventually call to Mocha APIs. Cheers!

Note that #1457 might make it easier to do that.

JoshuaKGoldberg avatar Dec 28 '23 00:12 JoshuaKGoldberg