🚀
【エラー】Jest encountered an unexpected token
前提
- Jestインストール
- jest.config.tsファイルをプロジェクトのルートに追加
jest.config.ts
module.exports = {
"roots": [
"<rootDir>/src"
],
"testMatch": [
"**/__tests__/**/*.+(ts|tsx|js)",
"**/?(*.)+(spec|test).+(ts|tsx|js)"
],
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest"
},
}
エラー内容
● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
・・・
対応:ESModuleに変更
修正後
const config = {
"roots": [
"<rootDir>/src"
],
"testMatch": [
"**/__tests__/**/*.+(ts|tsx|js)",
"**/?(*.)+(spec|test).+(ts|tsx|js)"
],
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest"
},
};
export default config;
原因
クライアントサイドのテストをJest実施した際に出たエラーですが、CommonJSとESModuleについて調べた結果、恐らく、インストール導入時にブラウザを選択していたからなのかな~~~と思ったのですが、原因がはっきりしていないのが現状です。
別のアプリ開発をしていた際は、クライアントサイドのテストではCommonJSで実行できていたので、今はそれしか思い浮かばず・・。
Discussion