Mini test runner with describe, it, only, and skip
While debugging, developers narrow a big test file down with .only (“run just this”) and park broken tests with .skip. The runner then has to work out which tests to run, and report every test by its full name so the output makes sense on its own: checkout > coupons > rejects expired codes.
Implement createSpecRunner(). Each call returns a new, independent runner:
const { describe, it, run } = createSpecRunner();
describe(name, callback) // declares a suite
describe.only(name, callback) // a focused suite
describe.skip(name, callback) // a skipped suite
it(name, fn) // declares a test
it.only(name, fn) // a focused test
it.skip(name, fn) // a skipped test
run() // => { passed, failed, skipped }
describe calls callback right away (synchronously), so the tests inside it are declared. Suites nest to any depth. it outside any suite declares a root-level test.run(), which is called once, after everything is declared.it.skip, or any suite around it was declared with describe.skip.it.only, or any suite around it was declared with describe.only. Every test inside a focused suite is focused, including plain it tests.it.only or describe.only was declared anywhere in the runner, only focused tests run; every other test is skipped.run() never throws.' > '. A root-level test’s full name is just its own name.run() returns { passed, failed, skipped }, each in declaration order:
passed: full names of the tests that passed.failed: { name, error } objects, where error is the thrown value’s message if it is an Error (including subclasses such as TypeError), otherwise String(value).skipped: full names of the tests that did not run.const { describe, it, run } = createSpecRunner();
it('loads config', () => {});
describe('checkout', () => {
it('shows the total', () => {});
describe.only('coupons', () => {
it('applies 10% off', () => {});
it('rejects expired codes', () => {
throw new Error('expected an error banner');
});
it.skip('stacks two coupons', () => {});
});
});
run();
// => {
// passed: ['checkout > coupons > applies 10% off'],
// failed: [
// { name: 'checkout > coupons > rejects expired codes', error: 'expected an error banner' },
// ],
// skipped: [
// 'loads config',
// 'checkout > shows the total',
// 'checkout > coupons > stacks two coupons',
// ],
// }