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

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

Open
treybrisbane opened this issue Feb 15, 2020 · 9 comments
Open

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

treybrisbane opened this issue Feb 15, 2020 · 9 comments
Labels
question Further information is requested

Comments

@treybrisbane
Copy link

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 treybrisbane added the question Further information is requested label Feb 15, 2020
@novemberborn
Copy link
Member

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

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

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

@novemberborn
Copy link
Member

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

JSDoc comments. I use them very often.

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

module.exports = config;

@novemberborn
Copy link
Member

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

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

@binyamin
Copy link

binyamin commented Jun 2, 2022

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

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

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

No branches or pull requests

3 participants