一键导入
test-rule
Write and run tests for a SonarJS rule. Use when working on rule tests, writing test fixtures, or running unit tests for a specific rule.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Write and run tests for a SonarJS rule. Use when working on rule tests, writing test fixtures, or running unit tests for a specific rule.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Provides JavaScript/TypeScript test file structure and patterns for SonarJS rule testing. Use when writing tests, understanding test structure, or debugging test failures.
Use before a SonarJS release or when the Peach Main Analysis workflow on SonarSource/peachee-js shows failed jobs or suspicious project issue-count drops that need triage. Classify failed Peach jobs and flag likely project-configuration regressions using docs/peach-main-analysis.md.
Use before a SonarJS release or when the Peach Main Analysis workflow on SonarSource/peachee-js shows failed jobs or suspicious project issue-count drops that need triage. Classify failed Peach jobs and flag likely project-configuration regressions using docs/peach-main-analysis.md.
Provides guidance on implementing and fixing SonarJS rules. Use also when tracing false positives, working with rule configuration, or understanding native vs external rule implementations.
Add or modify rule options in SonarJS, including the fields array, SonarQube UI visibility, and Java check class configuration. Use when working on rule configurations.
Provides test quality standards and best practices. Use when writing test cases, creating unit tests, implementing tests, or refining/reviewing test code. Essential for test generation and test refinement phases.
| name | test-rule |
| description | Write and run tests for a SonarJS rule. Use when working on rule tests, writing test fixtures, or running unit tests for a specific rule. |
npx tsx --test packages/analysis/src/jsts/rules/S1234/**/*.test.ts
Replace S1234 with the actual rule number. Do not run the full test suite (npm run bridge:test) — it takes too long.
| RuleTester | Use When |
|---|---|
DefaultParserRuleTester | Pure JavaScript rules, no TypeScript syntax |
NoTypeCheckingRuleTester | JS/TS rules that don't need type information |
RuleTester | Rules requiring TypeScript type information |
RuleTester with @babel/eslint-parser | Legacy JavaScript or Babel-specific syntax (e.g. Flow types, decorator proposals) |
Test files are named *.fixture.* (e.g., cb.fixture.js, cb.fixture.ts) and live in the rule folder.
some.clean.code(); // no issue
some.faulty.code(); // Noncompliant {{Message to assert}}
// ^^^^^^
The // ^^^^^^ underline marks the primary location (optional but recommended).
some.faulty.code(); // Noncompliant [[qf1]] {{Message}}
// fix@qf1 {{Suggestion description}}
// edit@qf1 [[sc=1;ec=5]] {{replacement text}}
For ESLint fixes (not suggestions), use ! suffix: [[qf1!]]. ESLint fixes must not have a fix@ comment.
context.report({
node,
message: toEncodedMessage(message, [secondaryNode], ['secondary message']),
});
In fixture:
primary.node(); // Noncompliant {{primary message}}
secondary.node(); // ^^^^^^^^^^^^^^< {{secondary message}}
Arrow direction: < means secondary is after primary; > means secondary is before primary.
// Noncompliant@+1 {{message}} ← issue is on next line
some.faulty.code();
// Noncompliant@-1 ← issue is on previous line
some.faulty.code();
some.faulty.code(); // Noncompliant@2 ← absolute line number
Add a cb.options.json file in the rule folder:
[7, { "ignoreIIFE": true }]
process.chdir(__dirname); // use local package.json for dependency detection
For rules needing TypeScript type checking:
import { RuleTester } from '../../../tests/tools/testers/rule-tester.js';
const ruleTester = new RuleTester();
ruleTester.run('rule-name', rule, {
valid: [{ code: 'valid code here' }],
invalid: [
{
code: 'invalid code here',
errors: [{ messageId: 'errorKey' }],
},
],
});
| Syntax | Effect |
|---|---|
// edit@qf [[sc=1;ec=5]] {{text}} | Replace column 1–5 with text |
// edit@qf {{whole line replacement}} | Replace entire line |
// add@qf {{new line content}} | Add new line after issue line |
// del@qf | Delete the line |
// add@qf@+1 {{content}} | Add after line+1 |
// Three issues, fix for 1st and 3rd:
code(); // Noncompliant [[qf1,,qf3]] {{msg1}} {{msg2}} {{msg3}}
// edit@qf1 {{fix for msg1}}
// edit@qf3 {{fix for msg3}}
// Or with explicit index:
code(); // Noncompliant [[qf1,qf3=2]] {{msg1}} {{msg2}} {{msg3}}