gpt4 book ai didi

jestjs - 如何编写Jest transformIgnorePatterns

转载 作者:行者123 更新时间:2023-12-04 12:56:49 25 4
gpt4 key购买 nike

我有这个感激

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)


并在config.json中尝试了这些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上运行此命令,所以也尝试了 this格式

最佳答案

transformIgnorePatterns表示如果测试路径与任何模式都匹配,则不会进行转换。

注意:请勿分割多行。

开玩笑将默认忽略所有node_modules

所以你必须这样写:

"transformIgnorePatterns": [
// all exceptions must be first line
"/node_modules/(?!@microsoft/sp-core-library|sp-dialog|other_libs_need_transform)"
],

关于jestjs - 如何编写Jest transformIgnorePatterns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52035066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com