Jest遇到了一个意外的令牌 - SyntaxError:意外的令牌'export'。

31

我正在使用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),并将 react-markdown 文件夹添加到 transformIgnorePatterns 配置中,但我仍然收到错误信息。

这是我的 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>,类似于<rootDir>/node_modules/(?!react-markdown/),但是没有任何变化。

我还尝试了直接从package.json配置jest,而不是使用jest.config文件,但也没有任何变化。

后来我发现了这个问题:Jest transformIgnorePatterns not working,其中提到需要配置Babel。

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

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

但我仍然遇到错误... 我已经没有更多的想法了。有什么线索可以解决这个问题吗?

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

1个回答

15

react-markdown 是以 JavaScript 形式提供的,需要在 Jest 配置中添加 babel-jest 作为转换器。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接