@hyperfrontend/linter/testingtesting
Specifies a rule with cases: valid texts that must report nothing, and invalid texts with the messages, positions and fixed text they must produce. testRule declares one node:test test per case, so the specs run under node --test or any runner built on it.
Usage
import { testRule } from '@hyperfrontend/linter/testing'
import rule from './no-debugger'
testRule(rule, {
valid: [{ name: 'a plain call', path: '/project/src/a.ts', text: 'run()\n' }],
invalid: [
{
name: 'a debugger statement',
path: '/project/src/a.ts',
text: 'debugger\n',
errors: [{ message: 'Remove the debugger.', line: 1, column: 1 }],
output: null,
},
],
})
Each case names the absolute path the text is linted as; the project root is the path's directory unless root says otherwise. A case can pass options, settings and language the way a configuration block would. On an invalid case, output is the text after the rule's fixes, null when the rule must offer no fix, and unchecked when absent.
API Reference§
ƒ Functions
Declares the tests of a rule.
Parameters
| Name | Type | Description |
|---|---|---|
§rule | RuleModule | The rule under test |
§cases | RuleCases | Its valid and invalid cases |
Example
Specify a rule with one valid and one invalid case
import { testRule } from '@hyperfrontend/linter/testing'
import rule from './no-debugger'
testRule(rule, {
valid: [{ name: 'a plain call', path: '/project/a.ts', text: 'run()\n' }],
invalid: [
{
name: 'a debugger statement',
path: '/project/a.ts',
text: 'debugger\n',
errors: [{ message: "Unexpected 'debugger' statement.", line: 1, column: 1 }],
},
],
})◈ Interfaces
A message the rule must report.
Properties
§
readonly endColumn?:numberThe 1-based end column, when checked; null for a problem without a range.A text the rule must report on.
Properties
§
readonly path:stringThe absolute path the text is linted as; its directory is the project root unless root says otherwise.§
readonly language?:Partial< LanguageSettings>How the text is read, where it differs from the default.§
readonly output?:stringThe text after the rule's fixes when it fixes, null when it must offer no fix; unchecked when absent.A text the rule reads.
Properties
§
readonly path:stringThe absolute path the text is linted as; its directory is the project root unless root says otherwise.§
readonly language?:Partial< LanguageSettings>How the text is read, where it differs from the default.The cases of one rule.