14.3.0 • Published 8 months ago

textlint-tester v14.3.0

Weekly downloads
3,929
License
MIT
Repository
github
Last release
8 months ago

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
                        }
                    }
                }
            ]
        },
        // Depreacted 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

14.3.0

8 months ago

14.2.1

8 months ago

14.1.0

10 months ago

14.2.0

10 months ago

14.0.5

10 months ago

14.0.4

1 year ago

14.0.3

1 year ago

14.0.2

1 year ago

14.0.0

1 year ago

14.0.1

1 year ago

13.4.2-next.0

1 year ago

13.4.1

2 years ago

13.4.0

2 years ago

13.3.3

2 years ago

13.3.1

2 years ago

13.3.2

2 years ago

13.3.0

2 years ago

13.0.4

2 years ago

13.0.5

2 years ago

13.0.2

2 years ago

13.0.3

2 years ago

13.0.0

2 years ago

13.0.1

2 years ago

13.1.3

2 years ago

13.1.4

2 years ago

13.1.1

2 years ago

13.1.2

2 years ago

13.1.0

2 years ago

12.6.1

2 years ago

12.6.0

2 years ago

13.2.0

2 years ago

12.5.2

2 years ago

12.4.0

2 years ago

12.3.0

2 years ago

12.3.1

2 years ago

12.2.3

3 years ago

12.2.4

3 years ago

12.5.0

2 years ago

12.2.2

3 years ago

12.2.1

3 years ago

12.2.0

3 years ago

12.1.0

4 years ago

12.1.1

3 years ago

12.0.2

4 years ago

12.0.0-beta.2

4 years ago

12.0.0-beta.3

4 years ago

12.0.0-beta.0

4 years ago

12.0.0-beta.1

4 years ago

12.0.0

4 years ago

5.3.5

4 years ago

5.3.3

4 years ago

5.3.4

4 years ago

5.3.2

4 years ago

5.3.1

4 years ago

5.3.0

4 years ago

5.2.7

4 years ago

5.2.6

5 years ago

5.2.5

5 years ago

5.2.4

5 years ago

5.2.3

5 years ago

5.2.2

5 years ago

5.2.1

5 years ago

5.2.0

5 years ago

5.1.15

5 years ago

5.1.14

5 years ago

5.1.13

5 years ago

5.1.12

5 years ago

5.1.11

6 years ago

5.1.10

6 years ago

5.1.9

6 years ago

5.1.8

6 years ago

5.1.7

6 years ago

5.1.6

6 years ago

5.1.5

6 years ago

5.1.4

6 years ago

5.1.3

6 years ago

5.1.2

6 years ago

5.1.1

6 years ago

5.1.0

6 years ago

5.0.2

6 years ago

5.0.1

7 years ago

5.0.0

7 years ago

4.1.3

7 years ago

4.1.2

7 years ago

4.1.1

7 years ago

4.1.0

7 years ago

4.0.6

7 years ago

4.0.5

7 years ago

4.0.4

7 years ago

4.0.3

7 years ago

4.0.2

7 years ago

4.0.1

7 years ago

4.0.0

7 years ago

4.0.0-next.2

7 years ago

4.0.0-next.1

7 years ago

4.0.0-next.0

8 years ago

3.0.3

8 years ago

3.0.2

8 years ago

3.0.1

8 years ago

3.0.0

8 years ago

3.0.0-beta.1

8 years ago

3.0.0-beta.0

8 years ago

2.2.4

8 years ago

2.2.3

8 years ago

2.2.2

8 years ago

2.2.1

8 years ago

2.2.0

8 years ago

2.1.1

8 years ago

2.1.0

9 years ago

2.0.0

9 years ago

1.2.0

9 years ago

1.1.0

9 years ago

1.0.0

9 years ago

0.5.1

9 years ago

0.5.1-0

9 years ago

0.5.0-0

9 years ago

0.4.1

10 years ago

0.4.0

10 years ago

0.3.3

10 years ago

0.3.2

10 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago