設定

為了更好地服務您,Karma 需要了解您的專案才能進行測試,這是透過設定檔來完成的。本頁說明如何建立這樣的設定檔。

有關語法和所有可用選項的更多資訊,請參閱設定檔文件

產生設定檔 #

可以使用 karma init 產生設定檔。

$ karma init my.conf.js

Which testing framework do you want to use?
Press tab to list possible options. Enter to move to the next question.
> jasmine

Do you want to use Require.js?
This will add Require.js plugin.
Press tab to list possible options. Enter to move to the next question.
> no

Do you want to capture a browser automatically?
Press tab to list possible options. Enter empty string to move to the next question.
> Chrome
> Firefox
>

What is the location of your source and test files?
You can use glob patterns, eg. "js/*.js" or "test/**/*Spec.js".
Press Enter to move to the next question.
> *.js
> test/**/*.js
>

Should any of the files included by the previous patterns be excluded?
You can use glob patterns, eg. "**/*.swp".
Press Enter to move to the next question.
>

Do you want Karma to watch all the files and run the tests on change?
Press tab to list possible options.
> yes

Config file generated at "/Users/vojta/Code/karma/my.conf.js".

設定檔也可以用 CoffeeScript 撰寫。事實上,如果您使用 *.coffee 副檔名執行 karma init,例如 karma init karma.conf.coffee,它將會產生一個 CoffeeScript 檔案。

當然,您可以手動撰寫設定檔,或從其他專案複製貼上 ;-)

啟動 Karma #

啟動 Karma 時,可以將設定檔路徑作為第一個參數傳入。

預設情況下,Karma 會在目前的目錄中尋找 karma.conf.jskarma.conf.coffee

# Start Karma using your configuration:
$ karma start my.conf.js

有關 Karma 設定檔的更多詳細資訊,例如可用選項和功能,請閱讀設定檔文件

命令列參數 #

設定檔中已存在的某些設定可以透過在執行 Karma 時將設定指定為命令列參數來覆蓋。

karma start my.conf.js --log-level debug --single-run

如果您想查看所有可用選項,請嘗試 karma start --help

與 Grunt/Gulp 整合 #