测试 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 文件,以包含来自测试用例源代码仓库的链接和署名信息。