Skip to content

Commit

Permalink
test: separate cases, so it's easier to know what fails
Browse files Browse the repository at this point in the history
  • Loading branch information
theoludwig committed Jan 9, 2024
1 parent 1ddcdc7 commit 7465ffd
Show file tree
Hide file tree
Showing 17 changed files with 164 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .markdownlint-cli2.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"MD033": false
},
"globs": ["**/*.{md,mdx}"],
"ignores": ["**/node_modules", "**/test/fixtures"],
"ignores": ["**/node_modules", "**/test/fixtures/**"],
"customRules": ["./src/index.js"]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
46 changes: 0 additions & 46 deletions test/basic.test.js

This file was deleted.

19 changes: 0 additions & 19 deletions test/fixtures/Invalid.md

This file was deleted.

21 changes: 0 additions & 21 deletions test/fixtures/Valid.md

This file was deleted.

3 changes: 3 additions & 0 deletions test/fixtures/invalid/non-existing-file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Invalid

[File](./index.test.js)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Valid

## Existing Heading
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Invalid

[Link fragment](./awesome.md#non-existing-heading)
3 changes: 3 additions & 0 deletions test/fixtures/invalid/non-existing-image.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Invalid

![Image](./image.png)
3 changes: 3 additions & 0 deletions test/fixtures/valid/existing-file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Valid

[File](../../index.test.js)
13 changes: 13 additions & 0 deletions test/fixtures/valid/existing-heading-fragment/awesome.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Valid

## Existing Heading

### Repeated Heading

Text

### Repeated Heading

Text

### Repeated Heading
Original file line number Diff line number Diff line change
@@ -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 changes: 3 additions & 0 deletions test/fixtures/valid/existing-image.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Valid

![Image](./image.png)
3 changes: 3 additions & 0 deletions test/fixtures/valid/ignore-absolute-paths.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Valid

![Absolute Path](/absolute/path.png)
7 changes: 7 additions & 0 deletions test/fixtures/valid/ignore-external-links.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Valid

[External https link](https://example.com/)

[External https link 2](https:./external.https)

[External ftp link](ftp:./external.ftp)
File renamed without changes
112 changes: 112 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -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',
)
})
})
})

0 comments on commit 7465ffd

Please sign in to comment.