Reactjs Jest测试失败:语法错误:意外标记<;
不确定在哪里查找此错误 使用带有React、Jest和Enzyme的Typescript进行单元测试 Package.json示例:Reactjs Jest测试失败:语法错误:意外标记<;,reactjs,typescript,jestjs,Reactjs,Typescript,Jestjs,不确定在哪里查找此错误 使用带有React、Jest和Enzyme的Typescript进行单元测试 Package.json示例: "scripts": { "start": "node server.js", "bundle": "cross-env NODE_ENV=production webpack -p", "test": "jest" }, "jest": { "transform": { "^.+\\.tsx?$": "<
"scripts": {
"start": "node server.js",
"bundle": "cross-env NODE_ENV=production webpack -p",
"test": "jest"
},
"jest": {
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json"
]
}
“脚本”:{
“开始”:“node server.js”,
“bundle”:“交叉环境节点_env=生产网页包-p”,
“测试”:“开玩笑”
},
“笑话”:{
“转变”:{
“^.+\.tsx?$”:“/node\u modules/ts jest/preprocessor.js”
},
“testRegex”:“(/\uuuu测试/*\ \(测试规范))\ \(ts | tsx | js)$”,
“moduleFileExtensions”:[
“ts”,
“tsx”,
“js”,
“json”
]
}
在以下位置运行npm测试结果:
FAIL src/components/Component.test.tsx
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){<?xml version="1.0" encoding="UTF-8"?>
^
SyntaxError: Unexpected token <
src/components/Component.test.tsx失败
({“Object.”:函数(模块、导出、require、目录名、文件名、全局、jest){
^
SyntaxError:意外标记<
编辑:这似乎发生在我使用
require
加载静态.svg
文件的第一个地方。为什么它不能处理这个问题?有没有办法忽略在使用require时抛出这个错误?Jest不使用Webpack,所以它不知道如何加载除js/jsx之外的其他文件扩展名。添加对其他扩展名的支持ons您需要编写自定义转换器。其中一个转换器是您在此片段的配置中定义的Typescript转换器:
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
当然,它是一个基本的转换器,总是返回相同的值
指向文档的链接:尝试删除
xml
标记,只包括以下svg
tag没问题!我删除了我的评论,因为您修复了它
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js",
"^.+\\.svg$": "<rootDir>/svgTransform.js"
},
module.exports = {
process() {
return 'module.exports = {};';
},
getCacheKey() {
// The output is always the same.
return 'svgTransform';
},
};