Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Library wrapping Ava #268

Open
ehmicky opened this issue Jul 10, 2019 · 3 comments
Open

Library wrapping Ava #268

ehmicky opened this issue Jul 10, 2019 · 3 comments

Comments

@ehmicky
Copy link

ehmicky commented Jul 10, 2019

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):

  • the library exporting test() being called "ava"
  • the test() signature not being modified
  • the main test function being called test() (that one should not be an issue though)

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
Copy link
Member

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

@sindresorhus
Copy link
Member

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
Copy link
Author

ehmicky commented Jul 10, 2019

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:

  • iterating over a cartesian product of inputs. This can be achieved with Ava by using several Array#forEach() but it's a little verbose.
  • using the stringified inputs as title instead of specifying a test title for each iteration. The generated title should:
    • work with any JavaScript type. For example Object.create(null) should be serialized differently from {}
    • be always unique (by appending a duplication counter)
    • be truncated when too long

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants