Skip to content

Can't get TS esm working on async esm babel config and esm ava.config.mjs #47

@yuriy-yarosh

Description

@yuriy-yarosh

I'm using node 18 LTS and trying to get ESM working.

@novemberborn as per #5.
I don't think @ava/typescript identifies *.mts test files as ES modules properly, after compiling with babel-preset-typescript instead of tsc and "type": "module".

I'm importing shared esm babel and ava configs from separate packages - i.e. monorepo with separate config-testing workspace for ava and config-babel, for babel instrumentation respectively (yarn berry workspaces tbe).
Using experimental @babel/register/experimental-worker.js to be able to load .babel.mjs config.

Everything is "type": "module" in here.

.ava.config.mjs

import path from 'node:path';
import { fileURLToPath } from 'node:url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

export default {
  files: [
    'test/**/*.mts',
  ],
  typescript: {
    extensions: [ 'ts', 'mts', 'cts', 'tsx', 'js','jsx', 'cjs', 'mjs' ],
    rewritePaths: {
      'src/': 'dist/',
    },
    compile: false,
  },
  require: [
    `${__dirname}/Register.js`,
  ],
  failFast: true,
  failWithoutAssertions: false,
  verbose: true,
};

.babelrc.mjs

export default {
  sourceType: 'module',
  targets: {
    node: 'current',
    esmodules: true
  },
  env: {
    coverage: {
      plugins: ['istanbul']
    }
  },
  presets: [
    '@babel/preset-typescript',
    [
      '@babel/preset-env',
      {
        loose: true,
        useBuiltIns: false,
        modules: false,
        bugfixes: true,
        targets: {
            node: 'current'
        },
      }
    ]
  ]
};

I'm not sure if worker is able to identify that it's a module over here. I'm passing the js extension but I don't think I'm able to force using ES modules, by default, and fallback to CommonJS if the import had failed.

Maybe it's worth adding 'ts' 'mts' extensions explicitly over here ?

For some reason it doesn't pick .mts text files as ES Modules.

I'd like to keep babel instrumentation due to certain TSC compatibility issues with the existing coverage report tools, including Node internal coverage reporter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions