如何在每次文本执行时抑制 "WARN: 'Could not find HammerJS."?

How can I suppress "WARN: 'Could not find HammerJS." on each text execution?

执行测试时,每个测试都会记录以下内容:

WARN: 'Could not find HammerJS. Certain Angular Material components may not work correctly.'

来自 Angular Material Getting Started Guide:

Some components (md-slide-toggle, md-slider, mdTooltip) rely on HammerJS for gestures. In order to get the full feature-set of these components, HammerJS must be loaded into the application.

基于此,我的结论是我不需要安装 HammerJS,因为我没有专门使用需要它的 Angular Material 组件。 不需要就不要加!

但是在执行 1000 多个测试时,每次都打印出该警告确实没有帮助,所以我想摆脱它或只打印一次。

以下解决方案将:

  1. 关于缺少 HammerJS 的警告仅一次。
  2. 如果您将来最终使用 new Hammer() 构造函数,则向控制台打印错误。
  3. 不需要您在测试环境中调用 enableProdMode(),因此您可以继续受益于双重更改检测提供的额外错误检查。

将以下 beforeAll 函数添加到您的测试包中:

beforeAll(function() {
    if (!window.hasOwnProperty('Hammer')) {
        window['Hammer'] = function() {
            console.error(
                'HammerJS is not installed. If you\'re now using Angular Material components that require it, please "npm i hammerjs"'
            );
        };
        console.warn('Could not find HammerJS. Certain Angular Material components may not work correctly.');
    }
});