測試 Webpack

測試 Webpack 案例

注意: tests/webpack-test 主要基於 webpack/test

逐步遷移 webpack 測試

原本,我們使用此公式計算相容性 passedTestCaseCount / totalTestCount,totalTestCount = passedTestCaseCount + failedTestCount + skippedTestCount,但有時由於某些原因(例如效能、我們不想支援的舊功能),可能難以與所有 webpack 測試案例相容,我們需要一種方法來跳過這些我們不支援的測試。因此,我們將原始公式調整為 (passedTestCaseCount + willNotSupportTestCount) / totalTestCount

目前,我們在每個失敗測試案例目錄下使用 test.filter.js 來跳過失敗的測試案例,使用此方法可以讓我們逐步遷移 webpack 測試案例,而不會影響真正的相容性(因為此方法不會影響真正的 passedTestCaseCount)。

例如:

// test.filter.js
module.exports = () => {
  return false; // false means this testcase is skipped for now, but maybe we will support in the future, `-1` means this test case we don't want to compatible with, this related to `willNotSupportTest`.
};

當您發現我們已通過一些現在被跳過的失敗測試案例時,您可以將 test.filter.js 變更為

module.exports = () => {
  return true;
};

或刪除 test.filter.js

測試 Webpack 外掛案例

基於實作差異和效能考量,Rspack 將在內部整合一些 Webpack 外掛。這些外掛的測試套件也會複製到 tests/plugin-test 資料夾,以測試外掛的相容性。

因此,為了與原始儲存庫保持一致,不建議修改這些測試案例,除非在以下情況下:

  • 當新的 Webpack 外掛整合到 Rspack 時,需要複製該外掛的測試案例。
  • 當 Rspack 和 Webpack 的產出物之間存在差異時(例如,不同的雜湊值),可能需要修改某些測試案例。

在上述提及的情況之外,請遵循 Rspack 測試 指南來新增測試案例。

執行測試

您可以使用以下方式執行這些測試案例

  • 在根目錄中執行 ./x test pluginpnpm run test:plugin
  • 或者在 rspack/tests/plugin-test 目錄中執行 npm run test
  • 要更新快照,請在 tests/plugin-test 目錄中執行 npm run test -- -u
  • 要傳遞特定的 jest cli 參數,請在 tests/plugin-test 目錄中執行 npm run test -- {args}

新增測試案例

  1. 建立 tests/plugin-test/{plugin-name} 資料夾,並將該外掛的測試案例複製到資料夾中。
  2. 調整 tests/plugin-test/jest.config.js 中的測試設定。如果存在特殊設定,請遵循以下步驟:
    1. 建立 tests/plugin-test/jest.{plugin-name}.config.js,匯入 jest.config.js,並在此基礎上進行修改。
    2. test:{plugin-name} 命令新增到 tests/plugin-test/package.jsonscripts 屬性中。
    3. 如果包含快照測試,請使用 global.updateSnapshot 來判斷是否更新快照。
  3. 更新授權資訊
    1. 新增一個 tests/plugin-test/{plugin-name}/README.md 檔案,並包含來自測試案例來源儲存庫的授權資訊。
    2. 更新 tests/plugin-test/README.md 檔案,以包含來自測試案例來源儲存庫的連結和作者資訊。