Switch branches/tags
v7.4.0 v7.3.0 v7.2.2 v3.6.0 v3.5.0 v3.3.1 v3.3.0 v3.1.2 v3.1.1 txt-to-ast@3.0.3 txt-to-ast@3.0.2 txt-to-ast@3.0.1 txt-to-ast@3.0.0 txt-to-ast@3.0.0-next.0 txt-to-ast@2.0.0 txt-to-ast@2.0.0-beta.0 txt-ast-traverse@2.0.4 txt-ast-traverse@2.0.3 txt-ast-traverse@2.0.2 txt-ast-traverse@2.0.1 txt-ast-traverse@2.0.0 txt-ast-traverse@2.0.0-next.0 textlint@11.0.1 textlint@11.0.0 textlint@10.2.1 textlint@10.2.0 textlint@10.1.5 textlint@10.1.4 textlint@10.1.3 textlint@10.1.2 textlint@10.1.1 textlint@10.1.0 textlint@10.0.1 textlint@10.0.0 textlint@10.0.0-next.2 textlint@10.0.0-next.1 textlint@10.0.0-next.0 textlint@9.1.1 textlint@9.1.0 textlint@9.0.1 textlint@9.0.0 textlint@9.0.0-beta.0 textlint@8.2.1 textlint@8.2.0 textlint@8.1.0 textlint@8.0.1 textlint@8.0.0 @textlint/textlint-plugin-text@4.0.1 @textlint/textlint-plugin-text@4.0.0 @textlint/textlint-plugin-text@3.0.10 @textlint/textlint-plugin-text@3.0.9 @textlint/textlint-plugin-text@3.0.8 @textlint/textlint-plugin-text@3.0.7 @textlint/textlint-plugin-markdown@5.0.1 @textlint/textlint-plugin-markdown@5.0.0 @textlint/textlint-plugin-markdown@4.0.10 @textlint/textlint-plugin-markdown@4.0.9 @textlint/textlint-plugin-markdown@4.0.8 @textlint/textlint-plugin-markdown@4.0.7 @textlint/text-to-ast@3.0.9 @textlint/text-to-ast@3.0.8 @textlint/text-to-ast@3.0.7 @textlint/text-to-ast@3.0.6 @textlint/text-to-ast@3.0.5 @textlint/text-to-ast@3.0.4 @textlint/monorepo-scripts@2.0.0 @textlint/monorepo-scripts@2.0.0-beta.0 @textlint/markdown-to-ast@6.0.9 @textlint/markdown-to-ast@6.0.8 @textlint/markdown-to-ast@6.0.7 @textlint/markdown-to-ast@6.0.6 @textlint/markdown-to-ast@6.0.5 @textlint/markdown-to-ast@6.0.4 @textlint/linter-formatter@3.0.8 @textlint/linter-formatter@3.0.7 @textlint/linter-formatter@3.0.6 @textlint/linter-formatter@3.0.5 @textlint/linter-formatter@3.0.4 @textlint/linter-formatter@3.0.3 @textlint/kernel@3.0.0 @textlint/kernel@2.0.9 @textlint/kernel@2.0.8 @textlint/kernel@2.0.7 @textlint/kernel@2.0.6 @textlint/kernel@2.0.5 @textlint/kernel@2.0.4 @textlint/kernel@2.0.3 @textlint/kernel@2.0.2 @textlint/kernel@2.0.1 @textlint/kernel@2.0.0 @textlint/kernel@2.0.0-next.2 @textlint/kernel@2.0.0-next.1 @textlint/kernel@2.0.0-next.0 @textlint/kernel@1.0.3 @textlint/kernel@1.0.2 @textlint/kernel@1.0.1 @textlint/kernel@1.0.0 @textlint/kernel@1.0.0-beta.0 @textlint/kernel@0.2.1 @textlint/kernel@0.2.0
Nothing to show
Find file History
azu Publish (#552)
- gulp-textlint@5.0.12
 - textlint-tester@5.0.1
 - textlint@11.0.1
 - textlint-example-cli@2.0.12
 - textlint-example-config-file@2.0.12
 - textlint-example-filter@2.0.12
 - textlint-example-fix-dry-run@2.0.12
 - textlint-example-fix@2.0.12
 - textlint-example-html-plugin@2.0.12
 - textlint-perf-test@2.0.12
 - textlint-example-plugin-extensions-option@2.0.12
 - textlint-example-preset@2.0.12
 - textlint-example-rulesdir@2.0.12
 - textlint-example-use-as-module@2.0.12
 - textlint-example-use-as-ts-module@2.1.9
 - @textlint/textlint-plugin-markdown@5.0.1
 - @textlint/textlint-plugin-text@4.0.1
 - integration-test@2.0.12
 - textlint-website@10.5.0
Latest commit 72ae64c Oct 13, 2018

README.md

textlint-tester

Mocha test helper library for textlint rule.

Installation

npm install --save-dev textlint-tester mocha

Usage

  1. Write tests by using textlint-tester
  2. Run tests by Mocha
$(npm bin)/mocha test/

TextLintTester

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=[]}))

TextLintTester#run(ruleName, rule, {valid=[], invalid=[]})

  • {string} ruleName ruleName is a name of the rule.
  • {TextlintRuleCreator} rule rule is textlint rule.

TextLintTester#run(testName, testConfig, {valid=[], invalid=[]})

  • {string} testName testName is a name of the test.
  • {TestConfig} testConfig testConfig is the configuration object for the test.
TestConfig object

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")
        },
        {
            ruleId: "max-number-of-lines",
            rule: maxNumberOfLineRule, // = require("textlint-rule-max-number-of-lines")
            options: {
                max: 2
            }
        }
    ]
}
valid object
  • {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 to text property
      • {object} options: options to be passed to the rule. Will throw assertion error if testConfig is specified

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",
    },
]
invalid object
  • {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 to text 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 if testConfig 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
            }
        ]
    }
]

Example

test/example-test.js:

const TextLintTester = require("textlint-tester");
const tester = new TextLintTester();
// rule
const rule = require("textlint-rule-no-todo");
// 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.

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