avajs / typescript

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

Issue with usage of ESM repository #41

Closed Amachua closed 2 years ago

Amachua commented 2 years ago

I am trying to switch my repository from CommonJS to ESM, as part of this migration I encounter some issue with the AVA configuration since when I launch the test I get the following issue:

  Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: \ava-repro\src\multiply.test.ts
  require() of ES modules is not supported.
  require() of ava-repro\src\multiply.test.ts from \ava-repro\node_modules\ava\lib\worker\base.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
  Instead change the requiring code to use import(), or remove "type": "module" from \ava-repro\package.json.

In order to help you found the issue in the configuration, I have reproduced the migration issue within this reproduction repository: https://github.com/Amachua/ava-repro (and you can see the issue directly in the pipeline build here).

Did I miss something in the configuration to make it properly work with ESM?

novemberborn commented 2 years ago

You're not using AVA's TypeScript support, instead you're using ts-node/register.

This may work, but it's also up to whether ts-node/register is registering ESM hooks.

export default {
  files: ['src/*.test.ts'],
  extensions: {
    ts: 'module'
  },
  require: [
    'ts-node/register'
  ],
  timeout: '20s'
};
Amachua commented 2 years ago

Got some time to see your feedback, thank you. I finally managed to found a solution on this issue :)