testing tool for textlint rule
JavaScript
Latest commit 4af70d9 Nov 24, 2016 @azu azu 2.1.0
Permalink
Failed to load latest commit information.
src fix(test): fix invalid line detection Nov 24, 2016
test fix(test): fix invalid line detection Nov 24, 2016
.babelrc Breaking Change(npm): Update textlint@7 Jun 24, 2016
.gitignore initial commit Oct 3, 2015
.travis.yml
LICENSE
README.md
package.json

README.md

textlint-tester Build Status

Mocha test helper library for textlint rule.

Installation

npm install -D textlint-tester mocha

Usage

  1. Write tests by using textlint-tester
  2. 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)

    e.g.)

[
    "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.

    e.g.)

    [
    {
        text: "text",
        output: "text",
        ext: ".txt",
        errors: [
            {
                messages: "expected message",
                line: 1,
                column: 1
            }
        ]
    }
    ]

Example

test/example-test.js:

var TextLintTester = require("textlint-tester");
var tester = new TextLintTester();
// rule
var rule = require("textlint-rule-no-todo");
// ruleName, rule, { valid, invalid }
tester.run("no-todo", rule, {
    valid: [
        "this is test",
        {
            // text with options
            text: "this is test",
            options: {
                "key": "value"
            }
        }
    ],
    invalid: [
        {
            text: "- [ ] string",
            errors: [
                {
                    message: "found TODO: '- [ ] string'"
                }
            ]
        },
        {
            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
             }
         ]
        }
    ]
});

Run the tests:

$(npm bin)/mocha test/

Tests

$(npm bin)/mocha test/
# npm test

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

License

MIT