Mocha test helper library for textlint rule.
npm install --save-dev textlint-tester mocha
- Write tests by using
textlint-tester
- Run tests by Mocha
$(npm bin)/mocha test/
TextLintTester#run
has two signatures.
- If you want to test single rule, use first method (
TextLintTester#run(ruleName, rule, {valid=[], invalid=[]})
) - If you want to test multiple rules and/or plugins, use second method (
TextLintTester#run(testName, testConfig, {valid=[], invalid=[]})
)
{string} ruleName
ruleName is a name of the rule.{TextlintRuleCreator} rule
rule is textlint rule.
{string} testName
testName is a name of the test.{TestConfig} testConfig
testConfig is the configuration object for the test.
TestConfig
is defined as follows:
export declare type TestConfig = {
plugins?: {
pluginId: string; // name of plugin
plugin: TextlintPluginCreator; // textlint plugin
options?: any; // options for plugin
}[];
rules: {
ruleId: string; // name of rule
rule: TextlintRuleCreator; // textlint rule
options?: any; // options for rule
}[];
};
testConfig
object example:
{
plugins: [
{
pluginId: "html",
plugin: htmlPlugin // = require("textlint-plugin-html")
}
],
rules: [
{
ruleId: "no-todo",
rule: noTodoRule // = require("textlint-rule-no-todo").default
},
{
ruleId: "max-number-of-lines",
rule: maxNumberOfLineRule, // = require("textlint-rule-max-number-of-lines")
options: {
max: 2
}
}
]
}
{string[]|object[]} valid
valid is an array of text which should be passed.- You can use
object
if you want to specify some options.object
can have the following properties:{string} text
: a text to be linted{string} ext
: an extension key. Default:.md
(Markdown){string} inputPath
: a test text filePath that prefer totext
property{object} options
: options to be passed to the rule. Will throw assertion error iftestConfig
is specified
- You can use
TypeScript declaration is for valid as follows:
export declare type TesterValid = string | {
text?: string;
ext?: string;
inputPath?: string;
options?: any;
};
valid
object example:
[
"text",
{ text : "text" },
{
text: "text",
options: {
"key": "value",
},
},
{
text: "<p>this sentence is parsed as HTML document.</p>",
ext: ".html",
},
]
{object[]} invalid
invalid is an array of object which should be failed.object
can have the following properties:{string} text
: a text to be linted.{string} inputPath
: a test text filePath that prefer totext
property.{string} output
: a fixed text.{string} ext
: an extension key.{object[]} errors
: an array of error objects which should be raised against the text.{object} options
: options to be passed to the rule. Will throw assertion error iftestConfig
is specified
TypeScript declaration is as follows:
export declare type TesterInvalid = {
text?: string;
output?: string;
ext?: string;
inputPath?: string;
options?: any;
errors: {
ruleId?: string;
index?: number;
line?: number;
column?: number;
message?: string;
[index: string]: any;
}[];
};
invalid
object example:
[
{
text: "text",
output: "text",
ext: ".txt",
errors: [
{
messages: "expected message",
line: 1,
column: 1
}
]
}
]
test/example-test.js
:
const TextLintTester = require("textlint-tester");
const tester = new TextLintTester();
// rule
const rule = require("textlint-rule-no-todo").default;
// ruleName, rule, { valid, invalid }
tester.run("no-todo", rule, {
valid: [
"This is ok",
{
// text with options
text: "This is test",
options: {
"key": "value"
}
}
],
invalid: [
// line, column
{
text: "- [ ] string",
errors: [
{
message: "Found TODO: '- [ ] string'",
line: 1,
column: 3
}
]
},
// index
{
text: "- [ ] string",
errors: [
{
message: "Found TODO: '- [ ] string'",
index: 2
}
]
},
{
text: "TODO: string",
options: {"key": "value"},
errors: [
{
message: "found TODO: 'TODO: string'",
line: 1,
column: 1
}
]
},
{
text: "TODO: string",
output: "string", // <= fixed output
errors: [
{
message: "found TODO: 'TODO: string'",
line: 1,
column: 1
}
]
}
]
});
See textlint-tester-test.ts
or textlint-tester-plugin.ts
for concrete examples.
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
MIT