Skip to content

Non-mutating API for specifying macros? #2189

@bitjson

Description

@bitjson

Hi all, I love the consistency of AVA’s API (especially the lack of magically-present globals).

Would the project be open to re-evaluating how macros are defined? It’s the only thing I consistently refer back to the documentation to use rather than discovering the type via inference/hover/autocomplete. It would also be nice to avoid mutating a Macro object to define test titles (when using tslint-immutable or similar).

So rather than:

import test, { Macro } from 'ava';

const macro: Macro<[string, number]> = (t, input, expected) => {
	t.is(eval(input), expected);
}

// tslint:disable-next-line: no-object-mutation
macro.title = (providedTitle = '', input, expected) => `${providedTitle} ${input} = ${expected}`.trim();

test(macro, '2 + 2', 4);
test(macro, '2 * 3', 6);
test('providedTitle', macro, '3 * 3', 9);

Maybe something like:

import test, { Macro } from 'ava';

const macro: Macro<[string, number]> = {
    macro: (t, input, expected) => {
	t.is(eval(input), expected);
    },
    title: (providedTitle = '', input, expected) => `${providedTitle} ${input} = ${expected}`.trim();
} 

test(macro, '2 + 2', 4);
test(macro, '2 * 3', 6);
test('providedTitle', macro, '3 * 3', 9);

I think this might also make the macro API a little easier to discover by type inference – you'd start getting autocomplete assistance immediately after providing an object to the test method, e.g.:
test({ [autocomplete offers "macro" and "title" here] }).

Thoughts? If there’s interest, I can try to send a non-breaking PR (where the mutating API is still supported).

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions