Jest 遇到意外标记 - SyntaxError:意外标记“导出”

2024-07-03

我正在使用 jest 来测试 React TypeScript 应用程序。

这是我正在运行的测试:

import { render, screen } from '@testing-library/react'
import { toBeInTheDocument } from '@testing-library/jest-dom'

import ContextProvider from '../../context/ContextProvider'
import { BrowserRouter } from 'react-router-dom'
import BlogPage from './BlogPage'

describe('BlogPage', () => {

  test('Render blog page', () => {
    render(
      <ContextProvider>
        <BrowserRouter>
          <BlogPage/>
        </BrowserRouter>
      </ContextProvider>
    )

    expect(screen.getByText('In this page you can see some of the last articles I wrote.')).toBeInTheDocument()
  })

})

这是我收到的错误:

FAIL  src/components/blogPage/BlogPage.test.js
  ● Test suite failed to run

    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:

    /home/German/Desktop/ger/code/projects/my-website/node_modules/react-markdown/index.js:6
    export {uriTransformer} from './lib/uri-transformer.js'
    ^^^^^^

    SyntaxError: Unexpected token 'export'

    > 1 | import ReactMarkdown from 'react-markdown'
        | ^
      2 | import Accordion from 'react-bootstrap/Accordion'
      3 |
      4 | interface Props {

我明白这是因为我正在使用的库(react-markdown)没有预编译的源代码。问题是我遵循了文档(https://jestjs.io/docs/tutorial-react-native#transformignorepatterns-customization https://jestjs.io/docs/tutorial-react-native#transformignorepatterns-customization)并添加了反应降价文件夹到变换忽略模式配置,我仍然收到错误。

这是我的 jest.config.ts 文件:

import type { Config } from '@jest/types'

const config: Config.InitialOptions = {
  verbose: true,
  transform: {
    '^.+\\.ts?$': 'ts-jest'
  },
  transformIgnorePatterns: [
    'node_modules/(?!react-markdown/)'
  ]
}
export default config

我尝试添加<rootDir> like <rootDir>/node_modules/(?!react-markdown/)这并没有什么区别。

我还尝试直接从 package.json 而不是 jest.config 文件配置 jest,它也没有什么区别。

然后我发现了这个问题:开玩笑,transformIgnorePatterns 不起作用 https://stackoverflow.com/questions/50147915/jest-transformignorepatterns-not-working,其中提到您需要配置 Babel。

我使用 create-react-app 创建了我的应用程序,所以我的应用程序上没有 Babel。我安装了它并创建了一个 babel.config.js 文件,在其中放入:

module.exports = {
    "presets": [
        "@babel/preset-env"
    ]
};

但我仍然收到错误... 我已经没有主意了。我该如何解决这个问题有任何线索吗?

完整代码可以在这里找到:https://github.com/coccagerman/my-website https://github.com/coccagerman/my-website


react-markdown作为 js 提供,添加babel-jest作为笑话配置中的变压器

  transform: {
    '^.+\\.ts?$': 'ts-jest',
    "^.+\\.(js|jsx)$": "babel-jest"
  },

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Jest 遇到意外标记 - SyntaxError:意外标记“导出” 的相关文章

随机推荐

  • 在泽西岛为自己的服务编写代理/包装类

    我想访问运行基本 http 身份验证的完整休息服务 然而 当 a提供了错误的凭证 https stackoverflow com questions 86105 how can i supress the browsers authenti
  • Spring boot 1.5.9,访问 Docker 容器内的资源镜像时出现 404 错误

    在 Spring Boot 1 5 9 应用程序中 我想使用 Google Chrome 通过 HTTP 访问静态资源 在 Docker 中我有404 error 在嵌入式tomcat中运行良好 配置 这是Dockerfile FROM o
  • 将blob发送到pythonflask然后保存

    所以我正在尝试制作一个记录你的声音的网站 问题是当我将 blob 文件或 blob url 发送到 Flask 服务器时 我的 Flask python 代码说没有内容 而它是 我该如何发送blob 因此服务器可以将其保存为文件 media
  • 如何在 XCode 6 iOS 模拟器中运行/录制 iOS 应用程序?

    我可能会犯这个错误 但我正在尝试在 Xcode 6 iOS 模拟器中运行 iOS 应用程序 以便录制该应用程序的视频教程 我已经安装了 xCode 并且可以启动 iOS 模拟器 但似乎无法安装 app 文件 Library Develope
  • Maven增量构建

    我们目前有一个大型 Maven 2 项目 它是许多具有复杂依赖关系的单个独立项目的集合 除了一些用于构建的通用父 POM 之外 最后 我们总是必须将应用程序作为一个整体进行交付 因此我宁愿将其转换为一个或几个大项目 有谁有如何优化大型项目的
  • 我应该为 Spark 选择哪种集群类型? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我是 Apache Spark 的新手 刚刚了解到 Spark 支持三种类型的集群 独立 意味着 Spark 将管理自己的集群 YARN 使用
  • 具有 has_many 和 has_one 多态关联的工厂女孩

    我目前正在开发一个项目 我想使用 Factory Girl 创建测试 但我无法使其与多态 has many 关联一起工作 我已经尝试了其他文章中提到的许多不同的可能性 但它仍然不起作用 我的模型看起来像这样 class Restaurant
  • C# 合并两个集合的不同项

    我正在寻找一种高性能的方法来将第二个 ICollection 的不同项目添加到现有的 ICollection 中 我正在使用 NET 4 这应该可以做到 list1 Union list2 Distinct aCustomComparer
  • Spring:根据 XSD 架构验证 REST 控制器

    目前我有 RestController 和以下代码 package be smartask api import be smartask api model NumberValue import be smartask api model
  • 使用 pandas 数据框进行主成分分析

    如何根据 pandas 数据框中的数据计算主成分分析 Most sklearn http scikit learn org stable 对象一起工作pandas数据框很好 这样的东西对你有用吗 import pandas as pd im
  • 使用maven仅将资源打包为jar

    我只想打包目录下的资源src main resources放入maven发布的jar中 有没有办法做到这一点 尝试以下操作
  • 代码镜头切换快捷键?

    我非常喜欢 VS2013 中的新代码镜头 但我觉得它具有侵入性 并且希望能够使用键盘打开和关闭它 我想知道是否有人知道允许我打开和关闭它的快捷方式或扩展 我搜索了谷歌和一些论坛 但找不到任何东西 我假设这还不存在 因为我有 0 个结果 但我
  • 并发环境中的幂等 PUT

    Context 我有一个 REST API 多个客户端 应用程序 可以使用 PUT 更新资源的状态 例如 该资源是一盏可以打开的灯ON or OFF 当系统检测到发生停电时 该资源也会自动更新 导致灯坏了 BROKEN状态 我想区分一下BR
  • Neuroph 神经网络帮助

    在我的研究生研究中 我正在创建一个训练识别图像的神经网络 我要做的事情比像许多示例那样仅采用 RGB 值网格 下采样并将它们发送到网络的输入要复杂得多 实际上 我使用了 100 多个独立训练的神经网络来检测线条 阴影图案等特征 更像人眼 而
  • 创建一个指向端口非80的IP的域名

    我想使用域名来指向本地服务器IP地址上的网页 然而 问题是该页面链接到端口 8088 而不是 80 上设置的 IP 地址 因为后者已被另一个网页使用 域名公司告诉我他们不能这样做 因为域名只能指向在端口 80 上设置的 IP 地址 所以现在
  • 什么是 redzone_checker?在 GPU 上分析我的张量流应用程序

    我正在使用 NVIDIA 的命令行 Visual Profiler nvprof 分析一个张量流 GPU 应用程序 其中一个已启动且在分析中非常活跃的内核是名为 redzone checker 的东西 我一生都无法在互联网上的任何地方找到任
  • 将文件上传选择限制为特定类型

    无论如何 通过限制文件类型的选择
  • 如何使用ansible输入私钥密码

    我有流浪者virtual machine跑步 I can ssh进入它使用ssh email protected cdn cgi l email protection然后它会询问我私钥的密码 我可以输入该密码 然后它就会让我登录 但如果我使
  • 如何从 YouTube 获取播放列表视频

    我正在编写一个我想要的应用程序获取 YouTube 视频列表 我可以通过使用特定用户的帐户来做到这一点 如下链接所示 HttpUriRequest request new HttpGet http gdata youtube com fee
  • Jest 遇到意外标记 - SyntaxError:意外标记“导出”

    我正在使用 jest 来测试 React TypeScript 应用程序 这是我正在运行的测试 import render screen from testing library react import toBeInTheDocument