textlint-tester
Mocha test helper library for textlint rule.
Installation
npm install --save-dev textlint-tester mocha
Usage
- Write tests by using
textlint-tester
- Run tests by Mocha
TextLintTester
TextLintTester#run(ruleName, rule, {valid=[], invalid=[]})
{string} ruleName
ruleName is a name of the rule.{Function} rule
rule is the exported function of the rule.{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{object} options
: options to be passed to the rule{string} ext
: an extension key. Default: .md
(Markdown)
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} output
: a fixed text.{string} ext
: an extension key.{object[]} errors
: an array of error objects which should be raised againt the text.
invalid
object example:
[
{
text: "text",
output: "text",
ext: ".txt",
errors: [
{
messages: "expected message",
line: 1,
column: 1
}
]
}
]
Example
test/example-test.js
:
const TextLintTester = require("textlint-tester");
const tester = new TextLintTester();
const rule = require("textlint-rule-no-todo");
tester.run("no-todo", rule, {
valid: [
"This is ok",
{
text: "This is test",
options: {
"key": "value"
}
}
],
invalid: [
{
text: "- [ ] string",
errors: [
{
message: "Found TODO: '- [ ] string'",
line: 1,
column: 3
}
]
},
{
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",
errors: [
{
message: "found TODO: 'TODO: string'",
line: 1,
column: 1
}
]
}
]
});
Run the tests:
$(npm bin)/mocha test/
Contributing
- 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
License
MIT