@hyperfrontend/linter/testing

testing

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

§function

testRule(rule: RuleModule, cases: RuleCases): void

Declares the tests of a rule.
Parameters
NameTypeDescription
§rule
RuleModuleThe rule under test
§cases
RuleCasesIts 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

§interface

ExpectedError

A message the rule must report.
Properties
§readonly message:stringThe text shown to the user.
§readonly line:numberThe 1-based start line, or null for a problem about the file as a whole.
§readonly column:numberThe 1-based start column, or null for a problem about the file as a whole.
§readonly endLine?:numberThe 1-based end line, when checked; null for a problem without a range.
§readonly endColumn?:numberThe 1-based end column, when checked; null for a problem without a range.
§interface

InvalidCase

A text the rule must report on.
Properties
§readonly name:stringThe test's name.
§readonly path:stringThe absolute path the text is linted as; its directory is the project root unless root says otherwise.
§readonly root?:stringThe project root, absolute, when it is not the file's directory.
§readonly text:stringThe text.
§readonly options?:unknownThe rule's options.
§readonly settings?:Readonly<Record<string, unknown>>The settings.
§readonly language?:Partial<LanguageSettings>How the text is read, where it differs from the default.
§readonly errors:unknownThe messages, in position order.
§readonly output?:stringThe text after the rule's fixes when it fixes, null when it must offer no fix; unchecked when absent.
§interface

RuleCase

A text the rule reads.
Properties
§readonly name:stringThe test's name.
§readonly path:stringThe absolute path the text is linted as; its directory is the project root unless root says otherwise.
§readonly root?:stringThe project root, absolute, when it is not the file's directory.
§readonly text:stringThe text.
§readonly options?:unknownThe rule's options.
§readonly settings?:Readonly<Record<string, unknown>>The settings.
§readonly language?:Partial<LanguageSettings>How the text is read, where it differs from the default.
§interface

RuleCases

The cases of one rule.
Properties
§readonly valid:unknownTexts that must report nothing.
§readonly invalid:unknownTexts that must report.