【问题标题】:Jest encountered an unexpected token and doesn't recongnise ES6 and JSXJest 遇到了意外的令牌,无法识别 ES6 和 JSX
【发布时间】:2020-11-12 04:01:32
【问题描述】:

Jest 无法识别 JSX 和 ES6。我也在使用 webpack 版本 3。我最近还将弃用的 babel 包更新到了​​最新版本。

[![在此处输入图片描述]

jest.config.js:

    module.exports = {
        verbose: true,
        transform: {
            "^.+\\.jsx?$": "babel-jest"
            },
        "transformIgnorePatterns": [
                "<rootDir>/node_modules/(?!@babel).+\\.js$"
              ],
    }

babel.config.js

module.exports = {
 "presets": [ "@babel/preset-env", "@babel/preset-react"],
 "plugins": [
    [   
      "@babel/plugin-proposal-class-properties",
      {
        "loose": true
      },
      '@babel/plugin-syntax-dynamic-import',
    ]
  ]
}

我的测试文件:

import React from 'react';
import ReactDom from 'react-dom';
import {render} from '@testing-library/react';
import AccessSeeker from './AccessSeeker';
// import Home from './Home'
// import BankStatementPage from './BankStatementPage';

test('renders', () => {
    render(<AccessSeeker/>)
})

【问题讨论】:

  • 你能把整个截图看一下错误指向的地方吗?
  • 已添加,请看上方

标签: reactjs jestjs


【解决方案1】:

问题是 jest 无法解析您的 css 文件,这在测试期间通常会被忽略。您可以通过添加以下配置来模拟它:

moduleNameMapper: {
  "^.+.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$": "jest-transform-stub"
}

// Keep in mind to install `npm i -D jest-transform-stub` before using

【讨论】:

  • 已修复!!!!谢谢,我的朋友 !!!你救了我的命
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-13
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 2019-03-22
相关资源
最近更新 更多