SyntaxError:swiper-react.js 中的意外令牌“导出”

问题描述 投票:0回答:0

我在尝试使用 Jest 测试我的应用程序时遇到问题。我的应用程序是使用 create-react-app 创建的,作为依赖项,我使用 Swiper 和 TypeScript。不知何故,当我运行测试时,我的终端出现错误。

 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.

    Here's what you can do:
     • 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
     • 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/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    D:\Coding\Projects\Website portfolio\weather-app-react\node_modules\swiper\react\index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from './swiper-react.js';
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

      2 | import { Box, Stack, Typography, Button, useMediaQuery } from "@mui/material";
      3 | import { useTheme } from "@mui/material/styles";
    > 4 | import { Swiper, SwiperSlide } from "swiper/react";
        | ^
      5 | import { Mousewheel } from "swiper";
      6 | import { CurrentWeatherType, ButtonDaysPerViewEnum } from "../types";
      7 | import MobileView from "./MobileView";

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
      at Object.<anonymous> (src/pages/MainPage.tsx:4:1)

我的 package.json 看起来像这样:

{
  "name": "weather-app-react",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@emotion/react": "^11.10.6",
    "@emotion/styled": "^11.10.6",
    "@hookform/resolvers": "^3.0.1",
    "@material-ui/core": "^4.12.4",
    "@mui/icons-material": "^5.11.11",
    "@mui/material": "^5.11.15",
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "@types/jest": "^27.5.2",
    "@types/node": "^16.18.23",
    "@types/react": "^18.0.31",
    "@types/react-dom": "^18.0.11",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-hook-form": "^7.43.9",
    "react-responsive": "^9.0.2",
    "react-router-dom": "^6.10.0",
    "react-scripts": "5.0.1",
    "react-spinners": "^0.13.8",
    "swiper": "^9.2.0",
    "typescript": "^4.9.5",
    "web-vitals": "^2.1.4",
    "yup": "^1.0.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

我还添加了我的回购协议:https://github.com/orchidenver/weather-app-react/tree/final

jestjs create-react-app react-swiper
© www.soinside.com 2019 - 2024. All rights reserved.