Skip to content

[Idea]: test macros #695

Closed
Closed
@jamestalmage

Description

@jamestalmage

From: #78 (comment)

Occasionally it's useful to create a function that calls test for you:

// fictional module that analyzes math expressions in strings:
import calculator from 'calculator-parser';
import ava from 'ava';

function test(input, expected, only) {
  const method = only ? ava.only : ava;
  method(input + ' === ' + expected, t => {
    const actual = calculator.calculate(input);
    t.is(actual, expected);
  });
}

test('3 + 4', 7);
test('5 - 2', 3);
test('6 * 3', 18, true); // causes this to be interpreted as an exclusive test
// ...

Unfortunately, such helpers create a situation where it becomes basically impossible to do static analysis on the code (static analysis may be necessary to solve #78).

I think we could solve some of this with a macro command:

import test from `ava`;

const myMacro = test.macro(
  (t, input, expected) => { // additional arguments to the macro are passed in. 
    const actual = calculator.calculate(input);
    t.is(actual, expected);
  },
  (input, expected) => input + ' === ' + expected // optional function to compute the test tile from the input.
);

myMacro('3 + 4', 7);

// macros get some of the same modifiers as a normal test function:
myMacro.skip('3 * 6', 18);
myMacro.only('9 / 3', 3);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions