-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot transform the imported binding xxxxx since it's also used in a type annotation #57
Comments
I have the same problem, have you solved it? |
@bvanjoi a colleague solved it by adding "babel": {
"presets": [
"@babel/preset-typescript"
]
}, One other workaround we had to do to get past the error Hope that helps someone.... |
thanks @davisford for coming back and posting your solution. I ran into the same issues you did ( It's a bit of a bummer to have to add a babel configuration to my project when there are no explicit references to babel anywhere in my project. If not for a comment linking back to this issue, it would seem like my babel config was doing nothing. |
@khalliday7 I don't recall 100% if it occurred just on files that do |
Locally, I found that the If you add "plugins": [
"@babel/transform-react-jsx"
] in your Babel configuration, it fixed the problem for me without needing to change import styles to Since only a subset of the files go through the Babel transformation and therefore only some of the files will have |
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 aelbore#57
So the simplest workaround is to run the babel-typescript transform but of course that defeats the point of this plugin which is to use esbuild to transform the typescript. Another workaround is to do both the following:
|
I think the workarounds are not that appealing. I think the code that looks for the mock calls should probably not do a string search, which feels a bit brittle to me. |
I tried the workaround (install the plugin For us, I can reproduce the problem with a minimal repro, which gives us a // bar.ts
// =====
export type Bar = {
a: string;
};
// foo.ts
// =====
import { Bar } from "./bar"; // <-- important to import a type
export const returnBar = (): Bar => ({ a: "foo" }); // use as annotation
// ock( <-- just put in this bogus text to trigger the transform
// foo.test.ts
// ========
import { add } from "./foo";
// no type annotations
it("does something", () => {
expect(add(1, 2)).toEqual(3);
}); I'm not very familiar with esbuild, I was mostly seeing if I could use it to speed up my Jest test runs, but wouldn't passing the code through a babel transform counteract any speed gains we'd find by using esbuild? Or does it still make a difference? If it does make a difference, any ideas why the workaround isn't working on my end? |
I'm encoutering the same issues as well
This plugin worked great in another repo but it seems it has real trouble when files use jest.Mock or Gonna try using the swc plugin to see if it does any better. |
We ended up going with |
this worked for me and i had a good laugh when searching my code and finding this text fragment
my jest tests ran again after renaming |
Any updates on this issues? We'd like to use |
Thanks, was going crazy just to realize that one of React components was named To repeat, you can't have |
esbuild-jest fails when test files have type annotations and jest.mock() calls (see aelbore/esbuild-jest#57)
Due to esbuild looking for `ock(` string, code containing something like `Clock()` requires a Babel plugin to run, which is odd. Refs: aelbore/esbuild-jest#57
LOL, that is bizarre! Who wrote this thing?! |
We also had to move to swc. Using Babel to pre-transform, which seems foundational concept here, seems defeating the purpose of esbuild. |
Fun fact, esbuild-jest cannot compile/transform tests that have the characters `ock(` anywhere in the code! This makes it very hard to mock anything with `jest.mock()`! See aelbore/esbuild-jest#57
Fun fact, `esbuild-jest`cannot compile/transform tests that have the characters `ock(` anywhere in the code! This makes it very hard to mock anything with `jest.mock()`! See aelbore/esbuild-jest#57
Fun fact, `esbuild-jest`cannot compile/transform tests that have the characters `ock(` anywhere in the code! This makes it very hard to mock anything with `jest.mock()`! See aelbore/esbuild-jest#57
I'm getting this error when trying to execute a Jest test:
I thought esbuild will strip out all type annotations, so I'm not sure what is wrong here?
Here is my jest.config.js transform:
The text was updated successfully, but these errors were encountered: