files
陣列決定了哪些檔案會被包含在瀏覽器中、被監控以及被 Karma 提供。
files
#類型. 陣列 無預設值. 此屬性為必填。 說明. 每個項目可以是字串(等同於 { pattern: "<string>" }
)或是一個具有以下屬性的物件
pattern
#type
#js
。css
- 使用 <link rel="stylesheet">
標籤包含。html
- 使用 HTML Imports 包含。請注意,此功能已過時,在現代瀏覽器中無法使用。js
- 使用 <script></script>
標籤包含。module
- 使用 <script type="module"></script>
標籤包含。dom
- 將檔案內容內嵌到頁面中。例如,這可以用於測試結合 HTML 和 JS 的組件。watched
#true
autoWatch
為 true
,則所有將 watched
設置為 true 的檔案都將被監控以查看是否有變更。included
#類型. 布林值
預設值. true
說明. 檔案是否應該使用 <script>
標籤包含在瀏覽器中?如果您想手動加載它們,例如使用 Require.js,請使用 false
。
如果一個檔案被多個具有不同 include
屬性的模式覆蓋,則最具體的模式優先於其他模式。
模式的具體性定義為一個六元組,其中較大的元組表示較小的具體性: (nglob parts, nglob star, nstar, next glob, nrange, noptional)。元組按字典順序比較。
nglob parts 是括號部分展開後的模式數量。例如,模式 {0...9} 將產生 nglob parts=10。元組的其餘部分則由每個展開模式中最不具體的模式決定。
served
#true
nocache
#false
integrity
#undefined
integrity
HTML 屬性值設置為加載匹配給定模式的資源的 <script>
或 <link>
標籤。basePath
#basePath
解析。basePath
是相對路徑,則它會解析為設定檔所在的目錄。test/unit/**/*.spec.js
。根據前置處理器的設定,請注意加載的檔案可能會被轉換,並且不再以其原始格式提供。例如,如果啟用了 html2js 前置處理器,則實際的 .html 檔案將不再提供 - 而是以 window.__html__['my.html']
的形式提供。閱讀更多關於 前置處理器 的資訊。
以下是一個完整範例,顯示了不同的選項
files: [
// Detailed pattern to include a file. Similarly other options can be used
{ pattern: 'lib/angular.js', watched: false },
// Prefer to have watched false for library files. No need to watch them for changes
// simple pattern to load the needed testfiles
// equal to {pattern: 'test/unit/*.spec.js', watched: true, served: true, included: true}
'test/unit/*.spec.js',
// this file gets served but will be ignored by the watcher
// note if html2js preprocessor is active, reference as `window.__html__['compiled/index.html']`
{pattern: 'compiled/index.html', watched: false},
// this file only gets watched and is otherwise ignored
{pattern: 'app/index.html', included: false, served: false},
// this file will be served on demand from disk and will be ignored by the watcher
{pattern: 'compiled/app.js.map', included: false, served: true, watched: false, nocache: true}
],
模式也可以是絕對網址。這允許包含 Karma 未提供的檔案。
範例
config.set({
files: [
'https://example.com/my-lib.js',
{ pattern: 'https://example.com/my-lib', type: 'js' }
]
})
與常規檔案路徑相比,絕對網址有一些特殊規則
watched
永遠是 false
included
永遠是 true
served
永遠是 false
nocache
永遠是 false
預設情況下,所有資源都通過 https://127.0.0.1:[PORT]/base/
提供
加載圖片的範例
files: [
{pattern: 'test/images/*.jpg', watched: false, included: false, served: true, nocache: false}
],
該模式是一個萬用字元,匹配指定的圖片資源。由於圖片不是測試,因此不需要監控和包含。但是,它們需要提供給瀏覽器。
在這種情況下,可以通過 https://127.0.0.1:[PORT]/base/test/images/[MY IMAGE].jpg
訪問圖片
請注意網址中的 base,它是對您的 basePath 的引用。您不需要替換或提供您自己的 base。
此外,您可以使用代理
proxies: {
"/img/": "https://127.0.0.1:8080/base/test/images/"
},
現在您可以在 https://127.0.0.1:8080/img/[MY IMAGE].jpg
獲取 test/images
中的圖片
將 8080 更改為您使用的端口
您也可以使用代理而不指定協議、主機名和端口
proxies: {
"/img/": "/base/test/images/"
},
middleware
設定選項)。