## 事象 以下のようなエラーになる。 ```error SyntaxError: /Users/tadashi-aikawa/git/github.com/tadashi-aikawa/obsidian-various-complements-plugin/src/util/strings.test.ts: Cannot transform the imported binding "FuzzyResult" since it's also used in a type annotation. Please strip type annotations using @babel/preset-typescript or @babel/preset-flow. ``` エラー周辺のコード。もちろん[[TypeScript]]ファイルに型エラーはない。 ```ts describe.each<{ value: string; query: string; expected: FuzzyResult }>` value | query | expected ${"abcde"} | ${"ab"} | ${{ type: "concrete_match" }} ${"abcde"} | ${"bc"} | ${{ type: "concrete_match" }} ${"abcde"} | ${"ace"} | ${{ type: "fuzzy_match", score: 1.2 }} ${"abcde"} | ${"abcde"} | ${{ type: "concrete_match" }} ${"abcde"} | ${"abcdef"} | ${{ type: "none" }} ${"abcde"} | ${"bd"} | ${{ type: "fuzzy_match", score: 0.8 }} ${"abcde"} | ${"ba"} | ${{ type: "none" }} ${"fuzzy name match"} | ${"match"} | ${{ type: "fuzzy_match", score: 1.125 }} `("microFuzzy", ({ value, query, expected }) => { test(`microFuzzy(${value}, ${query}) = ${expected}`, () => { expect(microFuzzy(value, query)).toStrictEqual(expected); }); }); ``` 以前は動いていたが、そのときと比べてこのテストケースは何も変更していなかった。 ### 環境 | 対象 | バージョン | | ---------------- | ---------- | | [[macOS]] | 15.6.1 | | [[esbuild-jest]] | 0.5.0 | | [[Jest]] | 29.7.0 | | [[esbuild]] | 0.23.0 | | [[TypeScript]] | 5.8.3 | | [[pnpm]] | 10.1.0 | ## 原因 [[esbuild-jest]]の仕様で **`ock(` という文字列を含む**と、**モックのコードがあると推測し、内部的に[[babel-jest]]を呼び出す** ようになっている。しかし、[[babel-jest]]は[[TypeScript]]に未対応であるため、型importを解釈できずエラーになるから。 <div class="link-card-v2"> <div class="link-card-v2-site"> <img class="link-card-v2-site-icon" src="https://github.githubassets.com/favicons/favicon.svg" /> <span class="link-card-v2-site-name">GitHub</span> </div> <div class="link-card-v2-title"> Cannot transform the imported binding xxxxx since it's also used in a type annotation · Issue #57 · aelbore/esbuild-jest </div> <div class="link-card-v2-content"> I'm getting this error when trying to execute a Jest test: import React, { ReactElement } from 'react'; import T ... </div> <img class="link-card-v2-image" src="https://opengraph.githubassets.com/a46a2e7aad5d2f84dcec5642e9fe246948d2621d43442166426d4dad65c831ca/aelbore/esbuild-jest/issues/57" /> <a href="https://github.com/aelbore/esbuild-jest/issues/57"></a> </div> ## 解決方法 [[Codex CLI]]の回答をそのまま引用。 - Babel を最小限有効化(推奨) - 依存追加: @babel/preset-typescript(TS/TSX)、必要なら @babel/plugin-transform-react-jsx(古い JSX ランタイム等) - package.json 例: - "babel.presets": ["@babel/preset-typescript"] - "babel.plugins": ["@babel/plugin-transform-react-jsx"](必要な場合のみ) - フォーク版 - ユーザー提供の修正版: esbuild-jest-fixed(2025-05 時点のコメント情報)。試す価値あり。 ## 回避策 [[Codex CLI]]の回答をそのまま引用。 - React の ReferenceError 回避の一案 - import React from 'react' を import * as React from 'react' に変更、または上記 JSX プラグインを有効化。 - ファイル内容の回避策(応急) - 型注釈があるファイルで “ock(” を含めない(jest.mock のあるファイルを分離、Block( などの識別子をリネーム等)。ただし脆く推奨しない。 - 代替トランスフォーマ - @swc/jest や ts-jest へ移行(多数の報告で安定)。 このために新たな依存関係を増やしたくなかったので、今回問題となった関数名のインポートを別名インポートにすることで乗り切った。 ```ts import { type FuzzyResult, // Avoid for https://github.com/aelbore/esbuild-jest/issues/57 isEOTinCodeBlock as isEOTinCodeBlock_, ``` これにより `isEOTinCodeBlock(` が `isEOTinCodeBlock_(` になるので `ock(` には一致せず動作するようになる。 ```ts test(`isEOTinCodeBlock should return ${expected} for text ending ${expected ? "inside" : "outside"} code block`, () => { expect(isEOTinCodeBlock_(text)).toBe(expected); }); ```