avajs / typescript

Test TypeScript projects using AVA.
MIT License
73 stars 16 forks source link

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

Open treybrisbane opened 4 years ago

treybrisbane commented 4 years ago

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

novemberborn commented 4 years ago

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.

binyamin commented 2 years ago

@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 commented 2 years ago

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

novemberborn commented 2 years ago

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?

binyamin commented 2 years ago

JSDoc comments. I use them very often.

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

module.exports = config;
novemberborn commented 2 years ago

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 commented 2 years ago

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

binyamin commented 2 years ago

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.

novemberborn commented 2 years ago

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