Jest 无法解析 preact 中的 TSX

Mu *_*Kim 5 jestjs enzyme preact

我正在尝试使用 TypeScript 在 Preact 应用程序中使用 Jest 和酶运行测试。

\n\n

我收到 Jest 遇到意外令牌错误:

\n\n
FAIL  src/app/app.test.tsx\n  \xe2\x97\x8f Test suite failed to run\n\n    Jest encountered an unexpected token\n\n    This usually means that you are trying to import a file which Jest cannot parse, \n     e.g. it\'s not plain JavaScript.\n...\n    Details:\n\n    src/app/app.test.tsx:10\n        const wrapper = enzyme_1.mount(<app_1.default optOutUrl="none"/>);\n                                       ^\n\n    SyntaxError: Unexpected token <\n\n    at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1059:14)\n
Run Code Online (Sandbox Code Playgroud)\n\n

在看到此存储库中的TypeScript 示例后,我更改了 jsx 选项以在tsconifg.json. 但它对我的项目环境没有帮助:

\n\n
Cannot find module \'react\' from \'styled.js\'\n\n    However, Jest was able to find:\n        \'./styled.js\'\n        \'./styled.js.flow\'\n        \'./styled.js.map\'\n\n    See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string\n\n      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:276:11)\n      at Object.<anonymous> (node_modules/linaria/src/react/styled.js:3:15)\n
Run Code Online (Sandbox Code Playgroud)\n\n

在上面的错误中,它表示样式组件相关文件中的反应解析失败。(I\xe2\x80\x99m 使用自定义 Preact 类型linaria/react中的样式组件。)此问题不会出现在开发和生产环境中。

\n\n

我还尝试使用 babel-jest 和以下配置。

\n\n
\xe2\x80\x9ctransform\xe2\x80\x9d: {\n      \xe2\x80\x9c^.+\\\\.t(s|x)$\xe2\x80\x9d: \xe2\x80\x9cbabel-jest\xe2\x80\x9d\n    }\n
Run Code Online (Sandbox Code Playgroud)\n\n
 FAIL  src/app/app.test.tsx\n  \xe2\x97\x8f Test suite failed to run\n\n    src/app/app.tsx: Cannot find module \'react\'\n
Run Code Online (Sandbox Code Playgroud)\n