giltayar/babel-register-esm

Is there any way to use this with mocha .ts/.tsx test files?

Closed this issue · 2 comments

I have a test/index.tsx file which is one of my mocha specs, I've been testing in CJS land with require('@babel/register')({ extensions: ['.js', '.ts', '.tsx'] }).

Now I'm trying to also run tests in ESM mode. The imports within my test file use .js extensions, but since the file itself is .tsx, it doesn't load:

mocha -n experimental-default-type=module --loader=babel-register-esm test/index.tsx

TypeError: Unknown file extension ".tsx" for /private/var/folders/ml/12gw8z2146l86_zb3_5nn08c0000gp/T/toolchains/react-view-slider-ts/test/index.tsx
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:203:36)
    at defaultLoad (node:internal/modules/esm/load:141:22)
    at ModuleLoader.load (node:internal/modules/esm/loader:409:7)
    at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:291:45)
    at link (node:internal/modules/esm/module_job:76:21)

Do you know any workaround for this?

Thanks a bunch for this package, it does work great for testing pure JS projects in ESM mode!

After digging through the code I discovered I can make the load hook read .tsx? files and return the transformed source with shortCircuit: true.

Hmmm, actually it seems like the problem was I had the wrong version of @babel/core installed, it predated support for .babelrc.cjs.