玩笑语法错误:意外的令牌 <

Dav*_*yan 5 typescript reactjs jestjs enzyme

我想使用带有打字稿的 jest 酶测试 react 应用程序中的一个组件,我按照jest 中的说明进行操作, 但由于某种原因我得到: SyntaxError: Unexpected token <

我可以测试测试一些功能没有问题,但是当我使用组件时出现错误,

这是开玩笑的配置 package.json

"jest": {
    "transform": {
      "^.+\\.tsx?$": "ts-jest"
    },
    "testMatch": ["**/__tests__/*.(ts|tsx)"],
    "snapshotSerializers": ["enzyme-to-json/serializer"],
    "setupFilesAfterEnv": ["<rootDir>/src/setupEnzyme.ts"],
    "unmockedModulePathPatterns": ["node_modules/react/", "node_modules/enzyme/"]
  }
Run Code Online (Sandbox Code Playgroud)

酶配置

import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });
Run Code Online (Sandbox Code Playgroud)
export default class TesComponent extends Component { 
    render(){
        return (
            <div>test</div>
        )
    }
}
Run Code Online (Sandbox Code Playgroud)
import { shallow } from 'enzyme'
import TestCompoenent from '../TestComponent'

test('component testing', () => {
    const component = shallow(<TestCompoenent />)
    expect(component.contains(<div>test</div>)).toBeTruthy()
})
Run Code Online (Sandbox Code Playgroud)

这是错误

Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • 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/en/configuration.html

    Details:

    /home/user/Documents/local-platform/frontend/src/pages/__tests__/settigs.test.tsx:9
        var component = enzyme_1.shallow(<TestComponent_1.default />);
                                         ^

    SyntaxError: Unexpected token <

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:471:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:513:25)
Run Code Online (Sandbox Code Playgroud)

我希望测试通过,因为测试组件确实包括 <div>test</div>

Pra*_*ali 8

尝试改变 tsconfig.json

"jsx": "preserve" -> "jsx": "react"

  • 最后!!以防万一,将其更改为“反应”解决了我的主要问题。但是,它会引发一些其他错误。正如 Dan Barclay (/sf/answers/4587749211/) 所提到的,通过将其替换为“react-jsx”来解决 (4认同)
  • 这在 nextjs 中不起作用。``- jsx 设置为保留(next.js 实现了自己优化的 jsx 转换)`` (2认同)

Ser*_*ure 5

@babel/plugin-transform-react-jsxas 插件添加到 babel 中babel.config.js为我解决了这个问题。

module.exports = {
  presets: [['@babel/preset-env', { targets: { node: 'current' } }], '@babel/preset-typescript'],
  plugins: ['@babel/plugin-transform-react-jsx'],
};

Run Code Online (Sandbox Code Playgroud)

我希望这对任何人都有帮助。在发现之前花了几个小时尝试不同的方法


Tom*_*cki 2

您的配置似乎无法正确处理 TypeScript 文件 (.tsx)。

这是我的工作配置的一部分:

"jest": {
    "transform": {
       "^.+\\.tsx?$": "ts-jest",
    },
    "moduleFileExtensions": [
       "ts",
       "tsx",
       "js",
       "json"
    ],
    "setupTestFrameworkScriptFile": "<rootDir>/src/setupSpecs.ts"
}
Run Code Online (Sandbox Code Playgroud)

我已经使用setupTestFrameworkScriptFile而不是setupFilesAfterEnv.