Skip to content

Instantly share code, notes, and snippets.

@jy95
Last active January 23, 2018 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jy95/25f9ee58f07672a3eb5e98ecf20497fa to your computer and use it in GitHub Desktop.
Save jy95/25f9ee58f07672a3eb5e98ecf20497fa to your computer and use it in GitHub Desktop.
Revision : helpers not compiled in custom test folder

Common Pitfalls

Translations: Français

ESLint plugin

If you use ESLint, you can install eslint-plugin-ava. It will help you use AVA correctly and avoid some common pitfalls.

AVA in Docker

If you run AVA in Docker as part of your CI, you need to fix the appropriate environment variables. Specifically, adding -e CI=true in the docker exec command. See #751.

AVA uses is-ci to decide if it's in a CI environment or not using these variables.

AVA and connected client limits

You may be using a service that only allows a limited number of concurrent connections. For example, many database-as-a-service businesses offer a free plan with a limit on how many clients can be using it at the same time. AVA can hit those limits as it runs multiple processes, but well-written services should emit an error or throttle in those cases. If the one you're using doesn't, the tests will hang.

By default, AVA will use as many processes as there are logical cores on your machine. This is capped at two in a CI environment.

Use the concurrency flag to limit the number of processes ran. For example, if your service plan allows 5 clients, you should run AVA with concurrency=5 or less.

Asynchronous operations

You may be running an asynchronous operation inside a test and wondering why it's not finishing. If your asynchronous operation uses promises, you should return the promise:

test(t => {
	return fetch().then(data => {
		t.is(data, 'foo');
	});
});

Better yet, use async / await:

test(async t => {
	const data = await fetch();
	t.is(data, 'foo');
});

If you're using callbacks, use test.cb:

test.cb(t => {
	fetch((err, data) => {
		t.is(data, 'foo');
		t.end();
	});
});

Alternatively, promisify the callback function using something like pify:

test(async t => {
	const data = await pify(fetch)();
	t.is(data, 'foo');
});

Attributing uncaught exceptions to tests

AVA can't trace uncaught exceptions back to the test that triggered them. Callback-taking functions may lead to uncaught exceptions that can then be hard to debug. Consider promisifying and using async/await, as in the above example. This should allow AVA to catch the exception and attribute it to the correct test.

Why are the enhanced assertion messages not shown?

Ensure that the first parameter passed into your test is named t. This is a requirement of power-assert, the library that provides the enhanced messages.

test(t => {
	t.is(1, 1);
});

Helpers not compiled in custom test folder

That is a known issue. The solution is to rename your custom-named-folder to test. The cause is due to the defaultHelperPatterns set in the codebase of Ava. It should be fixed in the 1.0 release.


Is your problem not listed here? Submit a pull request or comment on this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment