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

Experimental test interface #2435

Closed
wants to merge 13 commits into from
Closed

Conversation

novemberborn
Copy link
Member

@novemberborn novemberborn commented Mar 29, 2020

This kicks off some experimental changes to AVA's test interface. That is, the const test = require('ava') stuff. Eventually this will be come AVA 4, but initially it'll be available through importing ava/experimental. You'll also have to enable the experimentalTestInterfaces experiment.

Summary of (planned) changes:

  • AVA is now just a CJS module with a single export: the test function. If you're transpiling ESM, you'll no longer be able to import { serial } from 'ava'. This never worked anyway with proper ESM.
  • Test implementations can no longer have title functions.
  • You'll no longer be able to pass an array of macros (or implementations), when declaring tests or with t.try().
  • Speaking of macros, if you want them to have a title function you'll need to use test.macro({ exec (t) { … }, title () { return 'title' }).
  • If you're writing tests using TypeScript, the Macro type has changed.
  • But, on the plus side, test.macro() automatically uses the t.context type from test.

I tend to write service integration tests using AVA, which involve stubbing several APIs, managing database access and launching child processes. Crafting t.context for these tests is rather complicated.

I'm tinkering with a "forkable test interface". You'd start off calling test.make(). This gives you an entirely new test chain, say test2. You can define hooks and whatnot, and then call test2.fork() to create yet another test chain, which will use the hooks defined on test2 before forking.

You'll be able to pass some options to make() and fork() to construct t.context, along the lines of what you'd do using test.before() and test.beforeEach(). But, for TypeScript users, the context type will be inferable.

We could also use this to create a test chain that always executes serially, or limits concurrency, or runs in isolation from other test chains in the same test file, or executes conditionally.

More to come!

@papb
Copy link
Contributor

papb commented Sep 14, 2020

But, for TypeScript users, the context type will be inferable.

Hi @novemberborn, currently to get accurate type checking for t.context, I do the following:

import test, { TestInterface } from 'ava';

function wrapTest(test: TestInterface): asserts test is TestInterface<MyContextType> {
	test.beforeEach(t => {
		t.context = getMyContext();
	});
}

wrapTest(test);

test('foo', /* ... */);

And it works very well. Just in case it can help you have some ideas :)

@novemberborn
Copy link
Member Author

@papb that's a nice trick! Would you like to add it to our TypeScript recipe?

@papb
Copy link
Contributor

papb commented Dec 17, 2020

Hi @novemberborn, sorry for taking long to reply, I would but I don't have the time, if you like it please feel free to add it, no need to credit or anything :)

Base automatically changed from master to main February 7, 2021 15:32
@novemberborn novemberborn added this to the 4.0 milestone Feb 7, 2021
@novemberborn
Copy link
Member Author

I've added this to the AVA 4 milestone… I don't expect to land everything but would like to get at least the breaking changes in.

@novemberborn
Copy link
Member Author

Other than the "forkable" stuff, this is now in #2785 instead.

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

Successfully merging this pull request may close these issues.

Ava test() expression is not callable / has no call signatures
2 participants