运行测试套件
Discourse 为核心、插件和主题提供了大量的前端测试。搭建好可正常运行的本地开发环境后,可以通过多种不同的方式在本地运行这些测试。
在浏览器中运行测试
核心 / 插件: 在开发环境中访问 /tests
主题: 在开发(或生产)环境中访问 /theme-qunit,然后选择主题
一般来说,在编写核心测试时应从 "Target" 下拉菜单中选择 "core",在编写插件测试时应选择具体的插件。
:information_source: 遗憾的是,在撰写本文时,测试中存在内存泄漏问题,因此在单个浏览器中尝试运行整个测试套件往往会触及浏览器的内存上限。要在多个浏览器中并行运行整个测试套件,请查看下方的 CLI 示例。
在 CLI 上运行测试
核心
在 Discourse 的根目录下使用 bin/qunit(确保自上次拉取上游变更后已运行过 pnpm install)。它封装了 Ember Exam 以实现随机化和并行化,默认针对正在运行的 Rails 服务器和现有的 JS 资源运行。运行 bin/qunit --help 可查看完整的选项列表。
下面是一些实用的示例:
# Run a single test file, or every test in a directory:
bin/qunit frontend/discourse/tests/integration/components/bookmark-test.gjs
bin/qunit frontend/discourse/tests/integration/components
# Run the entire core suite across 5 headless Chrome instances in parallel:
bin/qunit --parallel 5
# Run all tests whose "module: test name" contains a substring (case-insensitive):
bin/qunit --filter "Integration | Component | bookmark"
# Slash-wrap the value to match with a regular expression, e.g. for alternation:
bin/qunit --filter "/bookmark|reaction/i"
# Spin up an isolated server and run in a visible browser for easier debugging:
bin/qunit --standalone --no-headless
:information_source:
--filter会转发给 QUnit:普通值按不区分大小写的子字符串匹配,而像/foo/i这样用斜杠包裹的值则按正则表达式匹配。加入i标志可实现不区分大小写的正则匹配。
插件
在 Discourse 的根目录下:
bin/qunit plugins/chat/test/javascripts/unit/lib/chat-audio-test.js # Run one file
bin/qunit plugins/chat/test/javascripts/unit # Run a directory
bin/rake "plugin:qunit" # Run all plugin qunit tests
bin/rake "plugin:qunit[discourse-chat-integration]" # Run a single plugin's qunit tests
主题
在 Discourse 的根目录下:
bin/rake "themes:qunit[url,<theme_url>]"
bin/rake "themes:qunit[name,<theme_name>]"
bin/rake "themes:qunit[id,<theme_id>]"
这篇文档对你有帮助吗?
谢谢你的反馈!