前提
TypeScriptとJavaScriptを同時に利用しているプロジェクトでJestを使用してテストを書きたいと考えています。しかし、以下のエラーが発生しています。
発生している問題・エラーメッセージ
js
1$ docker-compose exec front yarn test 2yarn run v1.22.18 3$ jest 4 FAIL src/App.test.tsx 5 ● Test suite failed to run 6 7 Jest encountered an unexpected token 8 9 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. 10 11 Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration. 12 13 By default "node_modules" folder is ignored by transformers. 14 15 Here's what you can do: 16 • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it. 17 • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript 18 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config. 19 • If you need a custom transformation specify a "transform" option in your config. 20 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option. 21 22 You'll find more details and examples of these config options in the docs: 23 https://jestjs.io/docs/configuration 24 For information about custom transformations, see: 25 https://jestjs.io/docs/code-transformation 26 27 Details: 28 29 /usr/src/app/src/components/atoms/wavesurfer/wavesurfer.jsx:1 30 ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import WaveSurfer from "wavesurfer.js"; 31 ^^^^^^ 32 33 SyntaxError: Cannot use import statement outside a module 34 35 26 | import { LearningMaterial } from "../../interfaces/index" 36 27 | import { deleteLearningMaterials } from "../../apis/learning_material" 37 > 28 | import { WSF_stock } from "../atoms/wavesurfer/wavesurfer" 38 | ^ 39 29 | // import { Wavesurfer_react } from "../../components/atoms/wevesurfer/wavesurfer_react" 40 30 | import { AuthContext } from "../../App" 41 31 | import { Update } from "../pages/Update" 42 43 at Runtime.createScriptFromCode (node_modules/jest-cli/node_modules/jest-runtime/build/index.js:1796:14) 44 at Object.<anonymous> (src/components/organisms/LearningMaterialItem.tsx:28:1) 45 46Test Suites: 1 failed, 1 total 47Tests: 0 total 48Snapshots: 0 total 49Time: 16.065 s 50Ran all test suites. 51error Command failed with exit code 1. 52info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
試したことと自分の考察
こちらのサイトと公式ドキュメントを参考にして以下のことを試しました。
1 インストールを行う
$ yarn add --dev jest @types/jest ts-jest
2 package.jsonの修正
json
1{ 2 // ... 3 "scripts": { 4 // ... 5 "test": "jest" 6 }, 7 "jest": { 8 "preset": "ts-jest" 9 } 10} 11
3 テストを走らせる
ここでエラーが発生しました。エラーから考察するに、どうやらimportを処理出来ていないようです。エラーが発生しているコンポーネントではwavesurfer.jsというライブラリーとプラグインを使用しているのですが、この部分がtypescriptだとエラーが起きてしまうので、javascriptで記述しています。ここでエラーが出ていますが、テストした時にのみエラーが発生しており、画面上では特に問題なく動いています。ここで自分は恐らくJestのts-jestを使用する場合は、言語をTypeScript(しないならJavaScript)に統一しないと仕様できないのではないかという結論に至りました。そのため、ts-jestを使用するのではなく、babelでその都度typescriptを翻訳すれば行けるのではないかと考えて、公式ドキュメントにある通りbabelを使用することにしました、
しかし、以下のエラーが発生してしまいました。
js
1$ docker-compose exec front yarn test 2yarn run v1.22.18 3$ jest 4 FAIL src/App.test.tsx 5 ● Test suite failed to run 6 7 Jest encountered an unexpected token 8 9 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. 10 11 Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration. 12 13 By default "node_modules" folder is ignored by transformers. 14 15 Here's what you can do: 16 • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it. • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript 17 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config. 18 • If you need a custom transformation specify a "transform" option in your config. 19 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option. 20 21 You'll find more details and examples of these config options in the docs: 22 https://jestjs.io/docs/configuration 23 For information about custom transformations, see: 24 https://jestjs.io/docs/code-transformation 25 26 Details: 27 28 /usr/src/app/src/App.test.tsx:12 29 (0, _react2.render)(<_App.default />); 30 ^ 31 32 SyntaxError: Unexpected token '<' 33 34 at Runtime.createScriptFromCode (node_modules/jest-cli/node_modules/jest-runtime/build/index.js:1796:14) 35 36Test Suites: 1 failed, 1 total 37Tests: 0 total 38Snapshots: 0 total 39Time: 0.942 s 40Ran all test suites. 41error Command failed with exit code 1. 42info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
ここでおかしな現象が発生しています。私のテストファイルは以下の通りなので、(<_App.default />);というコードは記載していないにも関わらずエラーが発生しています。
ts
1import React from 'react'; 2import { render, screen } from '@testing-library/react'; 3import App from './App'; 4 5test('renders learn react link', () => { 6 render(<App />); 7 const linkElement = screen.getByText(/learn react/i); 8 expect(linkElement).toBeInTheDocument(); 9}); 10
ここで詰まってしまいました。初心者なので的外れな考察をしているかもしれませんが、何かしらアドバイスがあればよろしくお願いいたします。必要なファイルがあれば追記いたします。
追記
javascriptファイルを読み込まない場合はテストをパスできます。
ts
1 docker-compose exec front yarn test 2yarn run v1.22.18 3$ jest 4 PASS src/App.test.tsx (8.573 s) 5 ✓ renders learn react link (28 ms) 6 7Test Suites: 1 passed, 1 total 8Tests: 1 passed, 1 total 9Snapshots: 0 total 10Time: 9.377 s, estimated 10 s 11Ran all test suites. 12Done in 11.07s.
あなたの回答
tips
プレビュー