test: separate cases, so it's easier to know what fails

This commit is contained in:
Théo LUDWIG 2024-01-10 00:01:13 +01:00
parent 1ddcdc7b18
commit 7465ffd8bc
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
17 changed files with 164 additions and 88 deletions

View File

@ -6,6 +6,6 @@
"MD033": false
},
"globs": ["**/*.{md,mdx}"],
"ignores": ["**/node_modules", "**/test/fixtures"],
"ignores": ["**/node_modules", "**/test/fixtures/**"],
"customRules": ["./src/index.js"]
}

View File

@ -37,7 +37,7 @@
"lint:prettier": "prettier . --check --ignore-path .gitignore",
"lint:javascript": "tsc --project jsconfig.json --noEmit",
"lint:staged": "lint-staged",
"test": "node --test ./test",
"test": "node --test --experimental-test-coverage ./test",
"release": "semantic-release",
"postinstall": "husky install",
"prepublishOnly": "pinst --disable",

View File

@ -1,46 +0,0 @@
const { test } = require("node:test")
const assert = require("node:assert/strict")
const { markdownlint } = require("markdownlint").promises
const relativeLinks = require("../src/index.js")
test("ensure the rule validate correctly", async () => {
const lintResults = await markdownlint({
files: ["test/fixtures/Valid.md", "test/fixtures/Invalid.md"],
config: {
default: false,
"relative-links": true,
},
customRules: [relativeLinks],
})
assert.equal(lintResults["test/fixtures/Valid.md"]?.length, 0)
assert.equal(lintResults["test/fixtures/Invalid.md"]?.length, 3)
assert.equal(
lintResults["test/fixtures/Invalid.md"]?.[0]?.ruleDescription,
"Relative links should be valid",
)
assert.equal(
lintResults["test/fixtures/Invalid.md"]?.[0]?.errorDetail,
'"./basic.test.js" should exist in the file system',
)
assert.equal(
lintResults["test/fixtures/Invalid.md"]?.[1]?.ruleDescription,
"Relative links should be valid",
)
assert.equal(
lintResults["test/fixtures/Invalid.md"]?.[1]?.errorDetail,
'"../image.png" should exist in the file system',
)
assert.equal(
lintResults["test/fixtures/Invalid.md"]?.[2]?.ruleDescription,
"Relative links should be valid",
)
assert.equal(
lintResults["test/fixtures/Invalid.md"]?.[2]?.errorDetail,
'"./Valid.md#not-existing-heading" should have a valid fragment identifier',
)
})

View File

@ -1,19 +0,0 @@
# Invalid
[basic.js](./basic.test.js)
![Image](../image.png)
[Link fragment](./Valid.md#not-existing-heading)
## Existing Heading
### Repeated Heading
Text
### Repeated Heading
Text
### Repeated Heading

View File

@ -1,21 +0,0 @@
# Valid
[basic.js](../basic.test.js)
![Image](./image.png)
![Absolute Path](/absolute/path.png)
[External https link](https://example.com/)
[External https link 2](https:./external.https)
[External ftp link](ftp:./external.ftp)
[Link fragment](./Invalid.md#existing-heading)
[Link fragment Repeated 0](./Invalid.md#repeated-heading)
[Link fragment Repeated 1](./Invalid.md#repeated-heading-1)
[Link fragment Repeated 2](./Invalid.md#repeated-heading-2)

View File

@ -0,0 +1,3 @@
# Invalid
[File](./index.test.js)

View File

@ -0,0 +1,3 @@
# Valid
## Existing Heading

View File

@ -0,0 +1,3 @@
# Invalid
[Link fragment](./awesome.md#non-existing-heading)

View File

@ -0,0 +1,3 @@
# Invalid
![Image](./image.png)

3
test/fixtures/valid/existing-file.md vendored Normal file
View File

@ -0,0 +1,3 @@
# Valid
[File](../../index.test.js)

View File

@ -0,0 +1,13 @@
# Valid
## Existing Heading
### Repeated Heading
Text
### Repeated Heading
Text
### Repeated Heading

View File

@ -0,0 +1,9 @@
# Valid
[Link fragment](./awesome.md#existing-heading)
[Link fragment Repeated 0](./awesome.md#repeated-heading)
[Link fragment Repeated 1](./awesome.md#repeated-heading-1)
[Link fragment Repeated 2](./awesome.md#repeated-heading-2)

3
test/fixtures/valid/existing-image.md vendored Normal file
View File

@ -0,0 +1,3 @@
# Valid
![Image](./image.png)

View File

@ -0,0 +1,3 @@
# Valid
![Absolute Path](/absolute/path.png)

View File

@ -0,0 +1,7 @@
# Valid
[External https link](https://example.com/)
[External https link 2](https:./external.https)
[External ftp link](ftp:./external.ftp)

View File

Before

Width:  |  Height:  |  Size: 95 B

After

Width:  |  Height:  |  Size: 95 B

112
test/index.test.js Normal file
View File

@ -0,0 +1,112 @@
const { test } = require("node:test")
const assert = require("node:assert/strict")
const { markdownlint } = require("markdownlint").promises
const relativeLinksRule = require("../src/index.js")
/**
*
* @param {string} fixtureFile
* @returns
*/
const validateMarkdownLint = async (fixtureFile) => {
const lintResults = await markdownlint({
files: [fixtureFile],
config: {
default: false,
"relative-links": true,
},
customRules: [relativeLinksRule],
})
return lintResults[fixtureFile]
}
test("ensure the rule validates correctly", async (t) => {
await t.test("should be valid", async (t) => {
await t.test("with an existing heading fragment", async () => {
const lintResults = await validateMarkdownLint(
"test/fixtures/valid/existing-heading-fragment/existing-heading-fragment.md",
)
assert.equal(lintResults?.length, 0)
})
await t.test("with an existing file", async () => {
const lintResults = await validateMarkdownLint(
"test/fixtures/valid/existing-file.md",
)
assert.equal(lintResults?.length, 0)
})
await t.test("with an existing image", async () => {
const lintResults = await validateMarkdownLint(
"test/fixtures/valid/existing-image.md",
)
assert.equal(lintResults?.length, 0)
})
await t.test("should ignore absolute paths", async () => {
const lintResults = await validateMarkdownLint(
"test/fixtures/valid/ignore-absolute-paths.md",
)
assert.equal(lintResults?.length, 0)
})
await t.test("should ignore external links", async () => {
const lintResults = await validateMarkdownLint(
"test/fixtures/valid/ignore-external-links.md",
)
assert.equal(lintResults?.length, 0)
})
})
await t.test("should be invalid", async (t) => {
await t.test("with a non-existing heading fragment", async () => {
const lintResults = await validateMarkdownLint(
"test/fixtures/invalid/non-existing-heading-fragment/non-existing-heading-fragment.md",
)
assert.equal(lintResults?.length, 1)
assert.deepEqual(lintResults?.[0]?.ruleNames, relativeLinksRule.names)
assert.equal(
lintResults?.[0]?.ruleDescription,
relativeLinksRule.description,
)
assert.equal(
lintResults?.[0]?.errorDetail,
'"./awesome.md#non-existing-heading" should have a valid fragment identifier',
)
})
await t.test("with a non-existing file", async () => {
const lintResults = await validateMarkdownLint(
"test/fixtures/invalid/non-existing-file.md",
)
assert.equal(lintResults?.length, 1)
assert.deepEqual(lintResults?.[0]?.ruleNames, relativeLinksRule.names)
assert.equal(
lintResults?.[0]?.ruleDescription,
relativeLinksRule.description,
)
assert.equal(
lintResults?.[0]?.errorDetail,
'"./index.test.js" should exist in the file system',
)
})
await t.test("with a non-existing image", async () => {
const lintResults = await validateMarkdownLint(
"test/fixtures/invalid/non-existing-image.md",
)
assert.equal(lintResults?.length, 1)
assert.deepEqual(lintResults?.[0]?.ruleNames, relativeLinksRule.names)
assert.equal(
lintResults?.[0]?.ruleDescription,
relativeLinksRule.description,
)
assert.equal(
lintResults?.[0]?.errorDetail,
'"./image.png" should exist in the file system',
)
})
})
})