Wallaby.js 組態概述 (03)

此文會介紹 Wallaby.js 配置基本概述。

在前面兩章有稍微介紹到 wallaby.js 是一個配置檔用來建置專案內的測試與檔案。再者如果專案中有使用到編譯器 (像是 Babel / TypeScript) 或模塊綑綁 (像是 Browserify / Webpack) 也可能需要在此 wallaby.js 或 wallaby.conf.js 進行配置。

前言
module.exports = function (wallaby) {
  return {
    files: [
      'src/**/*.js'
    ],

    tests: [
      'test/**/*Spec.js'
    ]
    ...
    // for node.js tests you need to set env property as well
    // https://wallabyjs.com/docs/integration/node.html
  };
};

由於 wallaby.js 是在 node.js 環境中執行的,因此您可以要求任何本地安裝的模組,並編寫並執行您需要的任何代碼。您還可以要求其他配置文件 (karma或 webpack) 一起整合在 wallaby.js 配置中。

wallaby 參數

 在上方的代碼範例中有看到 module.export = function (wallaby) { ... } ,而 wallaby 內含有的屬性有以下:

  • localProjectDir 專案本機位置
  • projectCacheDir 專案 Cache 位置,wallaby 把 files & testslocalProjectDir 複製到 projectCacheDir
  • compilers 編譯器如 coffeeScript / babel / typeScript
  • defaults 配置默認值
測試框架

testFramwork 指定您要使用何種測試框架執行你的測試,你僅能指定框架名字 wallaby.js 會下載當前最新版本,如下範例使用 mocha 框架。

module.exports = function (wallaby) {
  return {
    files: [
      'src/**/*.js'
    ],

    tests: [
      'test/**/*Spec.js'
    ],

    testFramework: 'mocha'
  };
};

如果想知道目前支持那些測試框架請點此

延遲

delays 此物件中可決定在編輯代碼延遲多少毫秒才開始自動運行測試。可以根據您所需要的測試運行頻率來搭配你電腦系統的強大程度來微調。

module.exports = function (wallaby) {
  return {
    files: [
      'src/**/*.js'
    ],

    tests: [
      'test/**/*Spec.js'
    ],

    delays: {
      run: 500
    }
  };
};
偵錯

debug 屬性是布林值,決定是否產生詳細 log 在 Wallaby Console,並提出可能的潛在問題可以提供給官方知道。

module.exports = function (wallaby) {
  return {
    files: [
      'src/**/*.js'
    ],

    tests: [
      'test/**/*Spec.js'
    ],

    debug: true
  };
};
調用 console.error 是否為失敗測試

reportConsoleErrorAsError 這個屬性很特別,當此屬性被設置成 true 時,在測試走到的代碼中有執行到 console.error 都會是呈現 failed 狀態。 

module.exports = function (wallaby) {
  return {
    files: [
      ...
    ],

    tests: [
      ...
    ],

    reportConsoleErrorAsError: true
  };
};
測試 log 訊息限制

Wallaby.js 預設是每條測試 100 條 log 訊息限制,如果你想在 Wallaby Console 看到更多的 log 可以調整 maxConsoleMessagesPerTest 數值。 

module.exports = function (wallaby) {
  return {
    files: [
      ...
    ],

    tests: [
      ...
    ],

    maxConsoleMessagesPerTest: 10000
  };
};
Slow tests

Wallaby 提供的 Report 會列出每項測試的時間,如果該測試執行時間超過 slowTestThreshold 值就會標出該測試過慢的 ICON,預設值為 75 ms。

module.exports = function (wallaby) {
  return {
    files: [
      ...
    ],

    tests: [
      ...
    ],

    slowTestThreshold: 200 // 200 ms
  };
};

Low code coverage level

Wallaby 提供的 Report 中會為輸出檔案旁邊列出 code coverage,如果低於 lowCoverageThreshold 值則會用黃色標示它。預設值為 80 百分比。

module.exports = function (wallaby) {
  return {
    files: [
      ...
    ],

    tests: [
      ...
    ],

    lowCoverageThreshold: 70 // 70%
  };
};

Project Name

預設在 Wallaby App 中會顯示跟資料夾的名稱,如果想改變顯示名稱可以更新此值。

module.exports = function () {

    return {
      name: 'My Project Name',
      ...
    };
  };
排除檔案不列入 code coverage 計算

 您可使用 filesWithNoCoverageCalculated 設置。例如,以下配置將從代碼覆蓋率計算中排除所有以 -helper.js 結尾的檔案:

  module.exports = function () {

    return {
      ...
      filesWithNoCoverageCalculated: ['src/**/*-helper.js']
    };
  };
Unhandled promise rejections

預設 wallaby.js 會把所有拒絕的 Promise 視為 Error,可以藉由設定 reportUnhandledPromises 設定來改變行為。

  module.exports = function () {

    return {
      ...
      reportUnhandledPromises: false
    };
  };
Running all tests in changed or affected test files

默認情況下 wallaby.js 為代碼改變重新運行一組最小的測試。假設你正在改變一個特定的測試,wallaby 只會重新運行這個測試。如果您希望每次測試文件中的所有測試都受到代碼更改的影響,那麼您可以設定 runAllTestsInAffectedTestFile。 

  module.exports = function () {

    return {
      ...
      runAllTestsInAffectedTestFile: true
    };
  };
Ignoring file loading chains for files dependency tracking purposes

ignoreFileLoadingDependencyTracking 簡單解釋就是在 source files 與 test files 忽略 import/require 的模組改變時是否重新執行測試。

  module.exports = function () {

    return {
      ...
      ignoreFileLoadingDependencyTracking: true
    };
  };

Data Source: Wallaby.js Configuration file: Overview