avajs / eslint-plugin-ava

ESLint rules for AVA
https://avajs.dev
MIT License
230 stars 49 forks source link

Library wrapping Ava #268

Open ehmicky opened 5 years ago

ehmicky commented 5 years ago

Some libraries might be wrapping Ava, for example:

const test = require('custom-ava')

or:

const test = require('custom-ava')(require('ava'))

Also the test() function (and its variants .skip(...), etc.) might be wrapped as well and provide slightly different arguments:

const test = require('ava-test-each')
test(
  ['red', 'green', 'blue'], 
  'example test', 
  (t, color) => {
    t.is(typeof color, 'string')
  }
)

I think this does not work with eslint-plugin-ava because this plugin might rely on (correct me if I'm wrong):

I am considering developing a small wrapper around Ava but I am realizing using that wrapper might not be compatible with eslint-plugin-ava. Would it be possible to add support for such wrappers?

I understand that might add lots of complexity, so feel free to close this issue if you don't think it's a good idea :)

sindresorhus commented 5 years ago

If they wrapped instance might provide other arguments, there are lots of rule assumptions that go out the window.

sindresorhus commented 5 years ago

I feel like instead of wrapping AVA, AVA should provide hooks that enable new functionality. Can you describe the problems you’re trying to solve by wrapping AVA?

ehmicky commented 5 years ago

I tend to iterate tests over different inputs. I love Ava but I personally find the syntax for macros not very intuitive and would prefer a syntax closer to Jest's test.each() and Array#forEach().

Besides this difference of syntax, there are additional features I would like:

I have created a library test-each that does just that:

const test = require('ava')
const { each } = require('test-each')

// The code we are testing
const multiply = require('./multiply.js')

// Run this test 4 times using every possible combination of inputs
each([0.5, 10], [2.5, 5], ({ title }, first, second) => {
  test(`should mix integers and floats | ${title}`, t => {
    t.is(typeof multiply(first, second), 'number')
  })
})

The library is designed to work with any test runner. However I want to create an adapter for Ava to make the syntax even simpler:

const ava = require('ava')
const avaTestEach = require('ava-test-each')
const test = avaTestEach(ava)

test([0.5, 10], [2.5, 5], 'should mix integers and floats', (t, first, second) => {
  t.is(typeof multiply(first, second), 'number')
})

My two concerns are: 1) Losing linting with eslint-plugin-ava 2) Monkey patching or wrapping test() (and related test.skip(), etc.) might be brittle. Also test() is quite polymorphic (title can be omitted for macros, callback must be omitted with test.todo(), extra arguments might be passed with macros) which makes wrapping harder.