現在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" } }
あなたの回答
tips
プレビュー