質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Jest

Jestは、JavaScriptのテストフレームワークです。設定が不要で、高速且つ安全にテストを開始できます。コードカバレッジを生成できる他、テストスコープ外のオブジェクトを容易にモック化できるなど、豊富な機能によりテストの導入を簡単にします。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

Q&A

0回答

769閲覧

TypeScriptとJavaScriptを同時に利用しているプロジェクトでJestを使用することは可能か知りたい。可能ならばimportエラーを解決したい。

senseIY

総合スコア281

Jest

Jestは、JavaScriptのテストフレームワークです。設定が不要で、高速且つ安全にテストを開始できます。コードカバレッジを生成できる他、テストスコープ外のオブジェクトを容易にモック化できるなど、豊富な機能によりテストの導入を簡単にします。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

0グッド

1クリップ

投稿2022/08/17 03:35

編集2022/08/17 05:10

前提

TypeScriptとJavaScriptを同時に利用しているプロジェクトでJestを使用してテストを書きたいと考えています。しかし、以下のエラーが発生しています。

発生している問題・エラーメッセージ

js

1$ docker-compose exec front yarn test 2yarn run v1.22.18 3$ jest 4 FAIL src/App.test.tsx 5Test 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: 16If 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 18To 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 5Test 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: 16If 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 17To 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.

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問