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

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

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

Next.jsは、Reactを用いたサーバサイドレンダリングなどを行う軽量なフレームワークです。Zeit社が開発しており、nextコマンドでプロジェクトを作成することにより、開発環境整備が整った環境が即時に作成できます。

TypeScript

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

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Q&A

0回答

1509閲覧

Next.js , TypeScript , React環境でjestでコンポーネントのテストがsyntaxErroeで通らない

amachan1234

総合スコア7

Next.js

Next.jsは、Reactを用いたサーバサイドレンダリングなどを行う軽量なフレームワークです。Zeit社が開発しており、nextコマンドでプロジェクトを作成することにより、開発環境整備が整った環境が即時に作成できます。

TypeScript

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

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

0グッド

0クリップ

投稿2020/04/17 03:22

現在Next.js , TypeScript , Reactという環境で、
jestによるテストを導入しようと思っています。
が、コンポーネントのsnapshotのテストを行おうとするとsyntaxEllorが出ます。

jestの導入手順

1.npm install enzyme enzyme-adapter-react-16 2.setupTest.jsに記述 import Enzyme from "enzyme"; import Adapter from "enzyme-adapter-react-16"; Enzyme.configure({ adapter: new Adapter() }); 3.npm install --save-dev enzyme-to-json 4.npm install --save @types/jest

下記のようなテストは通ります。

//__tests__/index.test.js it("adds 1 + 2 to equal 3", () => { expect(sum).toBe(3); });

しかし、コンポーネントを含んだ下記のテストはエラーが出ます。

//__tests__/index.test.js import React from "react"; import SideBar from "../components/cms/SideBar"; import { shallow } from "enzyme"; import toJson from "enzyme-to-json"; describe("<SideBar />", () => { it("<SideBar>", () => { const wrapper = shallow(<Aaaa />); expect(toJson(wrapper)).toMatchSnapshot(); }); });

エラー内容

Jest encountered an unexpected token This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript. By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules". Here's what you can do: • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config. • If you need a custom transformation specify a "transform" option in your config. • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option. You'll find more details and examples of these config options in the docs: https://jestjs.io/docs/en/configuration.html Details: SyntaxError: /Users/hoge/Desktop/React-tutorial/developer-training/hoge/gameAPP/__tests__/sidebar.test.js: Unexpected token (14:28) 12 | }); 13 | it("<SideBar>", () => { > 14 | const wrapper = shallow(<Aaaa />); | ^ 15 | expect(toJson(wrapper)).toMatchSnapshot(); 16 | }); 17 | }); at Parser._raise (node_modules/@babel/parser/src/parser/location.js:241:45) at Parser.raiseWithData (node_modules/@babel/parser/src/parser/location.js:236:17) at Parser.raise (node_modules/@babel/parser/src/parser/location.js:220:17) at Parser.unexpected (node_modules/@babel/parser/src/parser/util.js:149:16) at Parser.parseExprAtom (node_modules/@babel/parser/src/parser/expression.js:1144:20) at Parser.parseExprSubscripts (node_modules/@babel/parser/src/parser/expression.js:539:23) at Parser.parseMaybeUnary (node_modules/@babel/parser/src/parser/expression.js:519:21) at Parser.parseExprOps (node_modules/@babel/parser/src/parser/expression.js:311:23) at Parser.parseMaybeConditional (node_modules/@babel/parser/src/parser/expression.js:263:23) at Parser.parseMaybeAssign (node_modules/@babel/parser/src/parser/expression.js:211:21)

何か知見がございましたら
ご教授お願いいたします。

環境

jest 24.9.0

//package.json { "name": "cms", "version": "0.1.0", "private": true, "dependencies": { "@emotion/core": "^10.0.28", "@emotion/styled": "^10.0.27", "@testing-library/jest-dom": "^4.2.4", "@testing-library/react": "^9.3.2", "@testing-library/user-event": "^7.1.2", "@types/jest": "^25.2.1", "axios": "^0.19.2", "emotion": "^10.0.27", "enzyme": "^3.11.0", "enzyme-adapter-react-16": "^1.15.2", "isomorphic-unfetch": "^3.0.0", "next": "^9.3.4", "react": "^16.13.1", "react-dom": "^16.13.1", "react-router-dom": "^5.1.2", "react-scripts": "3.4.1" }, "scripts": { "dev": "next", "start": "react-scripts start", "build": "react-scripts build", "test": "jest", "eject": "react-scripts eject" }, "eslintConfig": { "extends": "react-app" }, "browserslist": { "production": [ ">0.2%", "not dead", "not op_mini all" ], "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] }, "devDependencies": { "@types/node": "^13.11.1", "@types/react": "^16.9.34", "enzyme-to-json": "^3.4.4", "typescript": "^3.8.3" } }

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問