aelbore / esbuild-jest

A Jest transformer using esbuild
519 stars 51 forks source link

fix Babel transform to handle TS type annotations #64

Open petebacondarwin opened 2 years ago

petebacondarwin commented 2 years ago

The @babel/plugin-transform-modules-commonjs transform runs before the esbuild step, which is where TS type annotations are removed.

This can cause files that contain typings to fail with errors like:

SyntaxError: xxx.ts: Cannot transform the imported binding "XXX" since it's also used in a type annotation. Please strip type annotations using @babel/preset-typescript or @babel/preset-flow.

This commit ensures that files that are transformed via this Babel plugin have their type annotations removed via the @babel/preset-typescript preset beforehand.

Fixes #57

thebanjomatic commented 2 years ago

Rather than use @babel/preset-typescript to strip the type annotations, it seems like maybe it would be better to do the esbuild transform first and then pass the results of that into babel-jest to do the jest.mock hoisting? With a little extra work you would also probably be able to hook up the inputSourceMap as well.

vertic4l commented 2 years ago

Can somebody release it in the way @thebanjomatic mentions it, please ?

dforsl commented 2 years ago

Any progress here? If this solution works - despite there being more ideal one - I think anything right now that lets us use type annotations in tests would be a huge improvement :pray:

peteratdivvy commented 2 years ago

Any word on this? This is preventing us from using esbuild-jest.

Crafoord commented 2 years ago

Is repo abandoned by @aelbore ?

anttiviljami commented 1 year ago

This change fixed our problem. 🙏 I've published @petebacondarwin's branch under @epilot/esbuild-jest in case anyone needs it.