textlint-tester: support snapshot testing
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-testeris not command - Instead of, we already have textlint-scripts
- current
- Save the result as inline snapshot or file?
- How to write
snapshotAPI?- Example:
snapshotproperty
- Example:
- Output format?
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
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.
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.