avajs / ava

Node.js test runner that lets you develop with confidence 🚀
MIT License
20.74k stars 1.41k forks source link

Handle compilation/bundle step #2589

Closed fregante closed 1 year ago

fregante commented 4 years ago

Recently I found myself having to bundle the tests to overcome all the hurdles caused by combinations of AVA, TypeScript and type: "module" (https://github.com/vadimdemedes/dom-chef/pull/68 and https://github.com/sindresorhus/refined-github/pull/3206), so having AVA support some kind of build step might be helpful.

{
  "ava": {
    "build": "rollup --config --out tests tests/_source/*"
  }
}

Advantages over rollup && ava


Additionally, it could automatically handle the creation of a temporary build folder

{
  "ava": {
    // Notice there's no "source" folder anymore because AVA would treat $TEMP as the test folder
    "build": "rollup --config --out $TEMP tests/*"
  }
}

Additional advantages

novemberborn commented 4 years ago

The infrastructure we use for @ava/babel and @ava/typescript could work for this.

How would you select a test file from inside the bundle though?

If you precompile AVA can load ESM test files though. https://github.com/avajs/ava/issues/2347#issuecomment-698499005 shows how you could do it on the fly with ts-node as well (I've been meaning to get that added to the docs).

FWIW in my TypeScript projects, running a separate tsc in watch mode works well enough with AVA's watch mode and @ava/typescript.

fregante commented 4 years ago

How would you select a test file from inside the bundle though?

I never tried. I suppose if the bundle is set up to have multiple outputs AVA would still see each test file.

#2347 (comment) shows how you could do it on the fly with ts-node as well

I'll try replacing https://github.com/sindresorhus/refined-github/pull/3206 with that solution perhaps, but with all those experimental flags it looks like a time bomb :(

in my TypeScript projects, running a separate tsc in watch mode

Can you point me to one such example? I'd like to see how you're configuring the built folder

novemberborn commented 4 years ago

How would you select a test file from inside the bundle though?

I never tried. I suppose if the bundle is set up to have multiple outputs AVA would still see each test file.

Test file selection is driven by the file system. What the TypeScript provider does is provide a way to map that to a different file at runtime. Presumably we could select a bundle entrypoint or something like that.

with all those experimental flags it looks like a time bomb :(

I think from AVA's side it'll be OK — you're really dependent on the behavior in Node.js.

in my TypeScript projects, running a separate tsc in watch mode

Can you point me to one such example? I'd like to see how you're configuring the built folder

Have a look at https://github.com/avajs/cooperate.

fregante commented 1 year ago

Closing because those issues have since been resolved (https://github.com/refined-github/refined-github/pull/4002 and https://github.com/vadimdemedes/dom-chef/pull/86) and I started using Vitest where I want to avoid ESM issues