Jest 遇到了意外的令牌 - SyntaxError: Unexpected token 'export'

我正在使用 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', () => {

    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 for how to enable it.
     • If you are trying to use TypeScript, see
     • 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:
    For information about custom transformations, see:


    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)没有预编译的源代码。问题是我遵循了文档( )并将 react-markdown 文件夹添加到 transformIgnorePatterns 配置中,但仍然出现错误。

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

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

const config: Config.InitialOptions = {
  verbose: true,
  transform: {
    '^.+.ts?$': 'ts-jest'
  transformIgnorePatterns: [
export default config

我尝试像 <rootDir> 这样添加 <rootDir>/node_modules/(?!react-markdown/) 并没有什么不同。

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

然后我发现了这个问题: Jest transformIgnorePatterns not working ,里面提到需要配置Babel。

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

module.exports = {
    "presets": [



stack overflow Jest encountered an unexpected token - SyntaxError: Unexpected token 'export'
author avatar


react-markdown 作为JS发货,添加 babel-jest 作为Jest配置中的变压器

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