avajs / cooperate

Plugin to enable cooperation between AVA test files
7 stars 5 forks source link

Please add commonjs support #9

Closed dolsem closed 2 years ago

dolsem commented 2 years ago

I run AVA on test files that are precompiled with TypeScript to a commonjs format. I am unable to use this library, getting the following error:

Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/@ava/cooperate/dist/source/index.js from .../build/test/spec/e2e/helpers/use-auth.js not supported

I tried loading using the esm package but it doesn't seem to work either.

Can you please include commonjs files in your package?

AVA version: 4.1.0. AVA config:

{
    "failFast": true,
    "files": [
      "build/test/**/*.spec.js"
    ],
    "verbose": true
}
novemberborn commented 2 years ago

You'll have to use await import('@ava/cooperate') instead. You can do that from an async test.before() hook. It's not ideal but very doable.

dolsem commented 2 years ago

@novemberborn unfortunately that doesn't solve the issue because TypeScript transpiles all imports to require statements. If I change module from "commonjs" to "esnext" in tsconfig, running tests starts failing with "Cannot use import statement outside a module".

I've also tried all methods listing in the AVA docs of running TS tests, such as using @ava/typescript or ts-node, none of them work for some reason, the only thing I found to work is my current setup with the commonjs module type.

novemberborn commented 2 years ago

In our setup at work the dynamic import() function isn't transpiled.

dolsem commented 2 years ago

@novemberborn what's the value of the module flag in your tsconfig / how do you do transpilation?

novemberborn commented 2 years ago

I think these may be the relevant properties:

    "module": "CommonJS",
    "target": "ES2019",
    "esModuleInterop": true,

That's just from one service where I know we do dynamic imports.

novemberborn commented 2 years ago

(I'm closing this issue for housekeeping purposes, but let's keep the conversation going.)

dolsem commented 2 years ago

@novemberborn I tried it with this configuration, making sure to include "esModuleInterop": true, but it fails with the same error.

novemberborn commented 2 years ago

Yea you're right, sorry.

With the upcoming TypeScript 4.7 release you get a node12 module option. It also lets you treat TypeScript files explicitly as CJS or ESM. That way, you can use CJS (but explicitly, through require()) and still do dynamic import().

Shared workers are a new AVA feature and so we've chosen to provide an ESM implementation only.