jestjs - 如何编写 Jest transformIgnorePatterns
问题描述
我有这个例外
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 上运行它,所以我也尝试了这种格式
解决方案
这transformIgnorePatterns
意味着如果测试路径匹配任何模式,它将不会被转换。
注意:不要拆分多行。
开玩笑将node_modules
默认忽略所有。
所以你必须这样写:
"transformIgnorePatterns": [
// all exceptions must be first line
"/node_modules/(?!@microsoft/sp-core-library|sp-dialog|other_libs_need_transform)"
],
推荐阅读
- pine-script - 交易视图的 Pinscript
- python - 如何在tensorflow的vectorized_map中使用TopK算子
- java - Jess - 有没有一种有效的方法来检查一个事实是否仍在工作记忆中?
- javascript - 计算threecsg网格的UV坐标
- python - ValueError: Expected 2D array, got 1D array instead: 对于矩阵?
- forms - SwiftUI 表单中的动态行高
- python - 只下载了 5 中的一个图像,它敲出一个错误
- airflow - 气流未从 DAG 获取“start_date”
- c# - 是否可以通过SqlDataReader 获取SQL 列的长度?
- django - Django 频道实时聊天保存发送的消息