Ui5 Best Practices Qunit

作者 UI5a99b882ce364无许可证收录于 2026年10月8日更新于 2026年10月8日

Use when the user asks to "write a QUnit test", "fix a failing QUnit test", "add a QUnit module", "modernize QUnit tests", "migrate from QUnit 1", or mentions QUnit-specific constructs such as assert.async, nextUIUpdate, Core.applyChanges, sinon sandbox, asyncTest, or QUnit.module. Covers coding standards for OpenUI5/SAPUI5 unit test files: const/let over var, arrow functions over .bind(this), async/await over assert.async(), assert.expect() in every async test, sinon.createSandbox(), descriptive test names, beforeEach/afterEach module isolation, nextUIUpdate vs Core.applyChanges rules, try/finally teardown in helpers, QUnit 1 to QUnit 2 globals migration, and non-ASCII character avoidance.

AI 生成的概览

用于编写、修复和现代化 OpenUI5/SAPUI5 QUnit 单元测试的编码规范。

功能
该技能为 OpenUI5/SAPUI5 项目中的 QUnit 测试文件编写与审查提供规则和参考指引。内容涵盖代码风格约定,例如用 const/let 代替 var、用箭头函数代替 .bind(this)、用 async/await 代替 assert.async()、异步测试中使用 assert.expect()、sinon.createSandbox()、描述性测试名称、beforeEach/afterEach 隔离、try/finally 清理、nextUIUpdate 与 Core.applyChanges 的取舍,以及 QUnit 1 到 QUnit 2 的迁移。它还会引导代理查阅三个随附参考文档,分别涉及编写新测试、现代化现有测试和异步模式。
适用场景
当被要求编写 QUnit 测试、修复失败的 QUnit 测试、添加 QUnit 模块、现代化或迁移 QUnit 测试时使用,或在出现 assert.async、nextUIUpdate、Core.applyChanges、sinon sandbox、asyncTest、QUnit.module 等 QUnit 专有构造时使用。它面向 UI5 单元测试代码,而非应用代码。
运行要求
不含脚本,仅包含说明和三个 Markdown 参考文件。代理应在生成输出前加载相关参考文档。目标项目预期使用 QUnit、Sinon 和 OpenUI5/SAPUI5,并配合 ESLint。

QUnit Test Best Practices for UI5

When to load each reference

TriggerLoad
Writing a new QUnit test file or module from scratchreferences/writing-new-tests.md [blocked]
Modernizing, refactoring, or reviewing existing test codereferences/modernizing-tests.md [blocked]
Migrating from QUnit 1 (globals: test, asyncTest, ok, stop, start) to QUnit 2references/modernizing-tests.md [blocked]
Any test touches nextUIUpdate, Core.applyChanges, assert.async, fake timers, or event-based asyncreferences/async-patterns.md [blocked]

Load the reference before producing any output. Do not work from memory.


Core rules (always apply)

RuleDetail
No varUse const or let. One declaration per line - no comma chains.
No .bind(this)Use arrow functions for callbacks that do not need their own this.
assert.expect(N) in every async testGuards against silent passes when async callbacks never fire. Not required for sync tests.
sinon.createSandbox()sinon.sandbox.create() emits a runtime deprecation warning in Sinon 5+ - prefer sinon.createSandbox(). Alternatively use the QUnit-sinon bridge (this.stub(), this.spy(), this.mock(); this.clock only when sinon.config.useFakeTimers is truthy). Do not mix both approaches in the same module.
Descriptive test namesSentence describing behavior. Never start with "it should". Unique within each module.
beforeEach / afterEach in every moduleCreate all controls in beforeEach, destroy them in afterEach. No shared mutable state between tests.
try/finally in helper-created controlsHelpers that create a control must destroy it in finally so it is cleaned up even when assertions throw.
No non-ASCII charactersNo non-ASCII characters in comments, strings, or JSDoc. Use plain ASCII hyphens, not em dashes. UTF-8 is required, but non-ASCII in comments has historically caused encoding issues.
ESLint - 0 errorsWarnings for pre-existing patterns (max-nested-callbacks, no-use-before-define, valid-jsdoc) are acceptable.

Quick-reference checklist

Use when authoring or reviewing a QUnit test file:

  • No var - use const or let; one declaration per line (no comma chains)
  • No .bind(this) - use arrow functions for callbacks that do not need their own this
  • No assert.async() in simple cases - use async function + await new Promise(...)
  • Every async test has assert.expect(N)
  • No sinon.sandbox.create() in new code - use sinon.createSandbox() or the bridge (this.stub(), this.spy(), this.mock()); this.clock only when fake timers are enabled
  • No "it should..." test titles - use descriptive sentences
  • Every QUnit.module has beforeEach / afterEach that create and destroy all controls
  • With fake timers: prefer await nextUIUpdate(this.clock) over Core.applyChanges(); only keep Core.applyChanges() when nextUIUpdate(clock) cannot handle the case
  • Helper functions that create controls destroy them in try/finally
  • No non-ASCII characters in comments or strings (UTF-8 required, but non-ASCII causes encoding issues)

来源与署名

来源:UI5/plugins-coding-agents位于plugins/ui5/skills/ui5-best-practices-qunit提交a99b882

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架