2017-07-26 62 views
1

我目前正在爲某些使用Angular Material的角度組件編寫一些單元測試。我現在遇到的問題是在運行單元測試時不斷收到以下警告。單元測試組件中顯示的角度材質警告

console.warn node_modules\@angular\material\bundles\material.umd.js:183 
Current document does not have a doctype. This may cause some Angular Material components not to behave as expected. 

console.warn node_modules\@angular\material\bundles\material.umd.js:196 
Could not find Angular Material core theme. Most Material components may not work as expected. For more info refer to the theming guide: https://material.ang 
ular.io/guide/theming 

console.warn node_modules\@angular\material\bundles\material.umd.js:3327 
Could not find HammerJS. Certain Angular Material components may not work correctly. 

查看這些警告是否正常?關於如何抑制這些警告的任何想法,因爲我沒有看到它們與單元測試真正相關?

回答

0

發現禁止警告的黑客。在Jest配置中,可以選擇設置可以配置Jest測試框架的腳本文件。

"jest": { 
    "setupTestFrameworkScriptFile": "<rootDir>/src/setup.ts", 
    } 

查看來自玩笑API文檔

運行一些代碼來配置或設置的每個試驗前的 測試框架的模塊的路徑setupTestFrameworkScriptFile選項。由於setupFiles在 之前執行,測試框架安裝在環境中,所以此腳本文件 向您展示了在環境中安裝測試框架 之後立即運行一些代碼的機會。

然後在setup.ts中,我們執行以下操作來抑制所有警告。

console.warn = function() {}; 

可能不是最理想的方式,但做了訣竅。

0

我已經能夠通過添加以下行的文件數組中karma.config.js解決這一

files: [ 
      {pattern: './src/test.ts', watched: false}, 
      { 
       pattern: './node_modules/@angular/material/prebuilt-themes/indigo-pink.css', 
       included: true, 
       watched: true 
      } 
     ],