如何编写Jest transformIgnorePatterns [英] How to write Jest transformIgnorePatterns

查看:966
本文介绍了如何编写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表达式

And tried these transformIgnorePatterns expressions in config.json

"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上运行此命令,所以也尝试了格式

and none of them worked. I run this on Windows 10 so I tried also this format

推荐答案

实际上,打字稿似乎在JEST测试用例中进展不佳,所以我觉得JEST需要忽略那些文件或需要打包这些软件包转换为JEST可以理解的js代码,

Actually, it seems like typescript is not going well with JEST test cases, so what i feel like either JEST need to ignore those files or those packages need to be transpiled to js code which JEST can understand,

您可以尝试

"transformIgnorePatterns": [ "node_modules/(?!(@microsoft/sp-core-library))" ],

以及在tsconfig.json中

and in tsconfig.json

"allowJs": true,

有关更多详细信息,请查看此帖子

for more details please check this post

https://github.com/SharePoint/sp -dev-docs/issues/2325#issuecomment-446416878

这篇关于如何编写Jest transformIgnorePatterns的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆