textlint icon indicating copy to clipboard operation
textlint copied to clipboard

textlint-tester: support snapshot testing

Open azu opened this issue 7 years ago • 5 comments

Snapshot Testing is usefful feature. specially, textlint-tester is best match with snapshot testing.

Illustlation

Test Code:

const rule = require("../src/textlint-rule-no-invalid-control-character");
tester.run("textlint-rule-no-invalid-control-character", rule, {
    valid: [
        "Ich ♥ Bücher",
        "new line \n",
        "tab \t",
        "rn \r\n",
        {
            text: "back\v",
            options: {
                allow: ["\v"]
            }
        },
        {
            text: "\u0019",
            options: {
                allow: ["\u0019"]
            }
        }
    ],
    // snapshot target
    snapshot: [
        {
            text: "back\v"
        }
    ]
});

Update snapshot:

textlint-tester --updateSnapshot test/*.js

Results

const rule = require("../src/textlint-rule-no-invalid-control-character");
tester.run("textlint-rule-no-invalid-control-character", rule, {
    valid: [
        "Ich ♥ Bücher",
        "new line \n",
        "tab \t",
        "rn \r\n",
        {
            text: "back\v",
            options: {
                allow: ["\v"]
            }
        },
        {
            text: "\u0019",
            options: {
                allow: ["\u0019"]
            }
        }
    ],
    // snapshot target
    snapshot: [
        {
            text: "back\v",
            output: "back",
            errors: [
                {
                    index: 4,
                    message: "Found invalid control character(LINE TABULATION \\u000b)"
                }
            ]
        }
    ]
});

Discuss point

  • How to update snapshot?
    • current textlint-tester is not command
    • Instead of, we already have textlint-scripts
  • Save the result as inline snapshot or file?
  • How to write snapshot API?
    • Example: snapshot property
  • Output format?

azu avatar Jul 22 '18 12:07 azu

textlint-tester test that TextlintMessage match with expected object.

Prviously, I've implemented snapshot testing for similar object.

regexp-string-matcher/snapshot-test.ts at master · textlint/regexp-string-matcher

In this implementaion, Output was markdown format. https://github.com/textlint/regexp-string-matcher/blob/master/test/snapshots/global-regexp/output-for-human.md

azu avatar Jul 22 '18 12:07 azu

I've implemented snapshot tester in https://github.com/secretlint/secretlint/tree/master/packages/%40secretlint/tester. It works. We can implement it for textlint, I think.

azu avatar May 16 '20 04:05 azu

https://github.com/secretlint/secretlint/blob/master/docs/secretlint-rule.md#test-secretlintsecretlint-rule-example https://github.com/secretlint/secretlint/blob/master/packages/%40secretlint/tester/src/index.ts secretlint implement snapshot testing. It is good example.

azu avatar Jul 11 '20 03:07 azu

Node.js support node:test as native

azu avatar Jan 30 '24 15:01 azu