avajs / ava

Node.js test runner that lets you develop with confidence 🚀
MIT License
20.74k stars 1.41k forks source link

Macro #use() feature request #1456

Open skeggse opened 7 years ago

skeggse commented 7 years ago

Description

I'd like to see a slightly simpler means to use the same macro for a large number of tests. Instead of specifying the macro at every call-site, it might be nice to have a test.use(macro) function that produces a new test function that will apply the macro to every invocation.

Test Source

import test from 'ava';

function macro(t, input, expected) {
    t.is(eval(input), expected);
}

const check = test.use(macro);

check('2 + 2 = 4', '2 + 2', 4);
check('2 * 3 = 6', '2 * 3', 6);

const title = (providedTitle, input, expected) => `${providedTitle} ${input} = ${expected}`.trim();

const checkIt = test.use(macro, title);

checkIt('2 + 2', 4);
checkIt('2 * 3', 6);

If interested, I might find the time to write a PR for this feature.

novemberborn commented 7 years ago

I like it!

IMO the title function should remain on the macro (so no test.use(macro, title)).

What would check.use(otherMacro) do? Perhaps throw an exception?

@avajs/core?

sindresorhus commented 7 years ago

I like it too, but I think it should be named test.macro() for clarity instead of test.use().

novemberborn commented 7 years ago

The test API should be available on these produced functions, e.g. check.skip(), check.serial(), check.cb().

Though perhaps not check.before(), check.after() etc.

novemberborn commented 4 years ago

I think this could be test.curry(). Will investigate as part of #2435.