textlint-tester
TypeScript icon, indicating that this package has built-in type declarations

15.2.1 • Public • Published

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
import TextLintTester from "textlint-tester";
// a rule for testing
import rule from "textlint-rule-no-todo";

const tester = new TextLintTester();
// ruleName, rule, { valid, invalid }
tester.run("rule name", rule, {
    valid: [
        "This is ok",
    ],
    invalid: [
        // line, column
        {
            text: "- [ ] string",
            errors: [
                {
                    message: "Found TODO: '- [ ] string'",
                    range: [2, 6]
                }
            ]
        }
    ]
});
  1. Run tests by Mocha
$ npx 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:

tester.run("rule name", {
    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
            }
        }
    ]
}, { ... })
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
      • {string} description: a description for the test case. This will be displayed as the result of the test.
      • {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;
    description?: string;
};

valid object example:

test.run("test name", rule, {
    valid: [
        "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.
      • {string} description: a description for the test case. This will be displayed as the result of the test.
      • {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's declaration is as follows:

export declare type TesterInvalid = {
    text?: string;
    output?: string;
    ext?: string;
    inputPath?: string;
    options?: any;
    description?: string;
    errors: {
        ruleId?: string;
        range?: readonly [startIndex: number, endIndex: number];
        loc?: {
            start: {
                line: number;
                column: number;
            };
            end: {
                line: number;
                column: number;
            };
        };
        /**
         * @deprecated use `range` option
         */
        index?: number;
        /**
         * @deprecated use `loc` option
         */
        line?: number;
        /**
         * @deprecated use `loc` option
         */
        message?: string;
        [index: string]: any;
    }[];
};

invalid object example:

test.run("rule name", rule, {
    invalid:
        [
            {
                text: "text",
                output: "text",
                ext: ".txt",
                errors: [
                    {
                        messages: "expected message",
                        line: 1,
                        column: 1
                    }
                ]
            }
        ]
})

Example

test/example-test.js:

import TextLintTester from "textlint-tester";
// a rule for testing
import rule from "textlint-rule-no-todo";

const tester = new TextLintTester();
// ruleName, rule, { valid, invalid }
tester.run("no-todo", rule, {
    valid: [
        "This is ok",
        {
            // text with options
            text: "This is test",
            options: {
                "key": "value"
            }
        }
    ],
    invalid: [
        // range
        {
            inputPath: path.join(__dirname, "fixtures/text/ng.md"),
            errors: [
                {
                    message: "Found TODO: '- [ ] This is NG'",
                    range: [2, 6]
                }
            ]
        },
        // loc
        {
            inputPath: path.join(__dirname, "fixtures/text/ng.md"),
            errors: [
                {
                    message: "Found TODO: '- [ ] This is NG'",
                    loc: {
                        start: {
                            line: 1,
                            column: 2
                        },
                        end: {
                            line: 1,
                            column: 6
                        }
                    }
                }
            ]
        },
        // Deprecated way
        // 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

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
4.2.0-alpha.0f2fd6f90canary
15.2.1425latest
13.4.2-next.01next
12.0.0-beta.31beta

Version History

VersionDownloads (Last 7 Days)Published
15.2.1425
15.2.011
15.1.11
15.1.02
15.0.12
15.0.01
14.8.4231
14.8.31
14.8.11
14.8.01
14.7.2338
14.7.11
14.7.01
14.6.02
14.5.08
14.4.234
14.4.11
14.4.09
14.3.02
14.2.11
14.2.02
14.1.04
14.0.59
14.0.4247
14.0.33
14.0.20
14.0.116
14.0.00
13.4.2-next.01
13.4.122,176
13.4.0617
13.3.31,782
13.3.233
13.3.1932
13.3.05
13.2.00
13.1.40
13.1.30
13.1.20
13.1.10
13.1.00
13.0.50
13.0.40
13.0.30
13.0.20
13.0.10
13.0.00
12.6.12,223
12.6.00
12.5.20
12.5.01
12.4.00
12.3.10
12.3.00
12.2.49
12.2.30
12.2.226
12.2.132
12.2.00
12.1.130
12.1.00
12.0.2153
12.0.0120
12.0.0-beta.31
12.0.0-beta.20
12.0.0-beta.10
12.0.0-beta.00
5.3.5201
5.3.4177
5.3.30
5.3.20
5.3.10
5.3.00
5.2.714
5.2.6888
5.2.50
5.2.41
5.2.30
5.2.20
5.2.10
5.2.00
5.1.1514
5.1.140
5.1.131,596
5.1.120
5.1.111,015
5.1.10224
5.1.90
5.1.80
5.1.70
5.1.6158
5.1.50
5.1.49
5.1.30
5.1.222
5.1.10
5.1.00
5.0.20
5.0.1476
5.0.00
4.2.0-alpha.0f2fd6f90
4.2.0-alpha.bbfc8f6d0
4.1.3170
4.1.20
4.1.10
4.1.00
4.0.61
4.0.50
4.0.40
4.0.31
4.0.21
4.0.11
4.0.02
4.0.0-next.20
4.0.0-next.10
4.0.0-next.00
3.0.37
3.0.24
3.0.11
3.0.06
3.0.0-beta.10
3.0.0-beta.00
2.3.0-alpha.6a53d00c0
2.2.4436
2.2.30
2.2.21
2.2.114
2.2.02
2.1.11
2.1.01
2.0.02
1.2.0320
1.1.00
1.0.00
0.5.18
0.5.1-00
0.5.0-00
0.4.1539
0.4.01
0.3.30
0.3.21
0.3.10
0.3.00
0.2.10
0.2.01
0.1.22
0.1.10

Package Sidebar

Install

npm i textlint-tester

Weekly Downloads

35,813

Version

15.2.1

License

MIT

Unpacked Size

76 kB

Total Files

18

Last publish

Collaborators

  • azu
  • textlint-user