avajs/ava

Monorepo with babel aliases support

yuriy-yarosh opened this issue · 1 comments

I'm using ava with babel-preset-typescript and a set of path aliases for monorepo, e.g. @org/testing-common/src/Common.ts to testing-common/Common.ts aliasing.

ava.config.cjs

module.exports = {
  files: [
    './test/**/*Test.ts',
  ],
  require: [
    '@org/config-testing/Register.js',
  ],
  snapshotDir: 'test/snapshots',
  concurrency: 64,
  failFast: true,
  failWithoutAssertions: false,
  verbose: true,
  nodeArguments: [
    '--trace-deprecation',
  ],
  nodeArguments: [
    '--experimental-modules'
  ],
  extensions: ['ts', 'tsx', 'mts']
};

Both @ava/typescript and plain babel register results with the same error.

Register.js

const babelRC = require('./.babelrc.js');

require('@babel/register')({
  extensions: ['.es6', '.es', '.jsx', '.js', '.mjs', '.ts', 'tsx', '.mts'],
  cache: false,
  ...babelRC,
});

.babelrc with a custom expo preset and an alias resolver.

const { production } = require('@org/config-babel-preset-expo/Resolver.js')

module.exports = {
  "env": {
    "coverage": {
      "plugins": ["istanbul"]
    }
  },
  "presets": [
    "@babel/preset-typescript",
    "@org/config-babel-preset-expo",
    ["@babel/preset-env", {
      "forceAllTransforms": false,
      "modules": "auto",
      "targets": production
    }],
    ["@babel/preset-react", {
      "runtime": "automatic",
    }],
  ],
  "plugins": [
    "babel-plugin-transform-typescript-metadata",
    "macros",
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    ["@babel/plugin-proposal-class-properties", { "loose": true  }]
  ]
}

With aliased imports for tests

import { hasDir, isFile } from 'config-testing-common/Common';

I'm always getting

 export * from 'config-testing-common/Mocked';
  ^^^^^^

  SyntaxError: Unexpected token 'export'

But relative imports for my ava test files work just fine

import { hasDir, isFile } from '../src/Common';

Everything else everywhere is aliased.

My ava config doesn't work without a custom @babel/register - using @ava/typescript config only gives

SyntaxError: Cannot use import statement outside a module

At least I've noticed that aliased imports are working correctly only for the current package.
I can't import anything across yarn workspace without prior compilation with babel and the respective path rewriting, which is puzzling.

Maybe ava.js has something similar to get-package-type somewhere, similarly to tape, so the respective rewrites module aliases could be forced with a custom config there (e.g. to specify explicitly that an alias is a module).