我有个例外
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:
C:\Code\SPFx\BCO\node_modules\@microsoft\sp-core-library\lib\index.js:11
export { default as _BrowserDetection } from './BrowserDetection';
^^^^^^
SyntaxError: Unexpected token export
19 | } from 'office-ui-fabric-react/lib/Utilities';
20 | import { IUserProvider } from "../UserProviders/IUserProvider";
> 21 | import {
| ^
22 | Environment,
23 | EnvironmentType
24 | } from '@microsoft/sp-core-library';
at ScriptTransformer._transformAndBuildScript (node_modules/jest/node_modules/jest-runtime/build/script_transformer.js:403:17)
at Object.<anonymous> (src/webparts/BCO/components/EmployeeSelector/EmployeeSelector.tsx:21:1)
at Object.<anonymous> (src/webparts/BCO/components/FieldMapping/FieldMapping.tsx:13:1)
并尝试了这些transformIgnorePatterns表达式,
"transformIgnorePatterns": [
"\\node_modules\\@microsoft\\sp-dialog",
"\\node_modules\\@microsoft\\sp-core-library",
"node_modules/(?!sp-core-library)",
"node_modules/(?!@microsoft/sp-core-library)"
],
但都没成功我在Windows 10上运行这个,所以我也尝试了这种格式
3条答案
按热度按时间0dxa2lsx1#
transformIgnorePatterns
意味着如果测试路径与任何模式匹配,则不会对其进行转换。注:请勿分割多行。
jest将默认忽略所有
node_modules
。所以你必须这样写:
omqzjyyz2#
实际上,看起来类型脚本与JEST测试用例不太匹配,所以我觉得JEST需要忽略这些文件,或者这些包需要被编译成JEST可以理解的js代码,
你可以试试这个
在tsconfig.json中
欲了解更多详情,请查看此帖子
https://github.com/SharePoint/sp-dev-docs/issues/2325#issuecomment-446416878
fkvaft9z3#
我在使用jest,webpack和vanilla js进行项目时遇到了类似的错误,因此当jest在任何
/*?.js$/
文件中遇到这行代码import '../css/styles.css';
时,都会抛出错误。我通过将jest配置从
package.json
文件移动到jest.config.js
文件来解决这个问题,并使用以下最小配置:babel.config.js
: