typescript icon indicating copy to clipboard operation
typescript copied to clipboard

Support typed configuration via an `ava.config.ts` file

Open treybrisbane opened this issue 6 years ago • 9 comments

It'd be useful if we could use TypeScript to define our Ava configurations via an ava.config.ts file.

Ideally, we'd be able to structure them like so:

// Importable `Configuration` type
import { Configuration } from 'ava';

// Typed configuration object
const configuration: Configuration = {
  // ... config goes here
};

export default configuration;

Webpack supports similar functionality with webpack.config.ts files (although I believe ts-node is required for those).

treybrisbane avatar Feb 15 '20 02:02 treybrisbane

Webpack supports similar functionality with webpack.config.ts files (although I believe ts-node is required for those).

Yea, this is the kicker.

AVA needs to first resolve the configuration, to then determine whether to load @ava/typescript.

I suppose we could resolve ava.config.ts files, then load @ava/typescript, and then somehow load the configuration.

But we'd have to compile the file which is another complication. I'm not sure it's worth it, as nice as it would be to have a type definition for the config.

novemberborn avatar Feb 16 '20 12:02 novemberborn

@novemberborn I believe that there are two separate ideas here. Firstly, whether ava should support ava.config.ts files for configuration. Secondly, should ava configurations have type definitions. I believe that the second idea is easier to implement than the first, and useful to a wider array of users.

binyamin avatar May 29 '22 16:05 binyamin

For the record, AVA 4 now lists @ava/typescript as an optional Peer Dependency. That should make this feature somewhat easier to implement.

binyamin avatar May 29 '22 16:05 binyamin

Secondly, should ava configurations have type definitions. I believe that the second idea is easier to implement than the first, and useful to a wider array of users.

How would this work in practice, in JS files?

novemberborn avatar May 30 '22 08:05 novemberborn

JSDoc comments. I use them very often.

/**
 * @type {import('ava').Config}
 */
const config = {};

module.exports = config;

binyamin avatar May 31 '22 23:05 binyamin

A little awkward, but it would then provide auto-completion and whatnot so that's cool!

PR welcome, even if it's an incomplete starting point. Note that we also support a config factory.

novemberborn avatar Jun 02 '22 12:06 novemberborn

That said, this needs to be added in https://github.com/avajs/ava.

novemberborn avatar Jun 02 '22 12:06 novemberborn

There's also another solution that I've seen a couple places (vite, nextjs). AVA would export a defineConfig function, which takes a config parameter and exports it directly. The trick is that you can add a type definition to the config parameter.

binyamin avatar Jun 02 '22 14:06 binyamin

Importing ava itself doesn't quite work (it expects to be imported in a test worker). We could have an ava/config export though.

novemberborn avatar Jun 02 '22 15:06 novemberborn