@markedjs/testutils 17.0.1-0 → 17.0.1-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,16 +1,28 @@
1
1
  {
2
2
  "name": "@markedjs/testutils",
3
- "version": "17.0.1-0",
3
+ "version": "17.0.1-1",
4
4
  "description": "Test utilities for marked and marked extensions",
5
- "main": "src/index.js",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./lib/index.js",
10
+ "types": "./lib/index.d.ts"
11
+ }
12
+ },
6
13
  "type": "module",
7
14
  "scripts": {
8
- "test": "node --test",
15
+ "build": "tsc",
16
+ "test": "npm run build && node --import tsx --test test/*.test.ts",
9
17
  "test:lint": "eslint .",
10
18
  "test:format": "prettier . --check",
19
+ "test:types": "attw --pack . --ignore-rules=cjs-resolves-to-esm",
11
20
  "lint": "eslint . --fix",
12
21
  "format": "prettier . --write"
13
22
  },
23
+ "files": [
24
+ "lib/"
25
+ ],
14
26
  "repository": {
15
27
  "type": "git",
16
28
  "url": "git+https://github.com/markedjs/testutils.git"
@@ -22,15 +34,19 @@
22
34
  },
23
35
  "homepage": "https://github.com/markedjs/testutils#readme",
24
36
  "dependencies": {
25
- "@markedjs/html-differ": "^5.0.3",
37
+ "@markedjs/html-differ": "^5.0.4",
26
38
  "front-matter": "^4.0.2",
27
39
  "marked": "17.0.1",
28
40
  "marked-repo": "https://github.com/markedjs/marked/tarball/v17.0.1"
29
41
  },
30
42
  "devDependencies": {
43
+ "@arethetypeswrong/cli": "^0.18.2",
31
44
  "@eslint/js": "^9.33.0",
32
- "eslint": "^9.39.1",
45
+ "@types/node": "^22.10.2",
46
+ "eslint": "^9.39.2",
33
47
  "globals": "^16.5.0",
34
- "prettier": "^3.6.2"
48
+ "prettier": "^3.7.4",
49
+ "tsx": "^4.7.1",
50
+ "typescript": "^5.7.2"
35
51
  }
36
52
  }
@@ -1,11 +0,0 @@
1
- version: 2
2
- updates:
3
- - package-ecosystem: "github-actions"
4
- directory: "/"
5
- schedule:
6
- interval: "weekly"
7
- - package-ecosystem: "npm"
8
- versioning-strategy: "increase"
9
- directory: "/"
10
- schedule:
11
- interval: "weekly"
@@ -1,39 +0,0 @@
1
- name: "Automerge"
2
- on:
3
- workflow_run:
4
- workflows:
5
- - Tests
6
- types:
7
- - completed
8
-
9
- jobs:
10
- Automerge:
11
- runs-on: ubuntu-latest
12
- if: |
13
- github.event.workflow_run.event == 'pull_request' &&
14
- github.event.workflow_run.conclusion == 'success'
15
- steps:
16
- - name: "Merge PR"
17
- uses: actions/github-script@v8
18
- with:
19
- github-token: ${{ secrets.GITHUB_TOKEN }}
20
- script: |
21
- const pr = await github.rest.pulls.get({
22
- owner: context.repo.owner,
23
- repo: context.repo.repo,
24
- pull_number: context.payload.workflow_run.pull_requests[0].number,
25
- });
26
- if (pr.data.title.includes('marked')) {
27
- console.log('Not Merged 🚫');
28
- console.log(`Must update marked-repo as well`);
29
- } else if (pr.data.user.login !== 'dependabot[bot]') {
30
- console.log('Not Merged 🚫');
31
- console.log(`User '${pr.data.user.login}' does not equal 'dependabot[bot]'`);
32
- } else {
33
- await github.rest.pulls.merge({
34
- owner: context.repo.owner,
35
- repo: context.repo.repo,
36
- pull_number: context.payload.workflow_run.pull_requests[0].number,
37
- });
38
- console.log('Merged 🎉');
39
- }
@@ -1,87 +0,0 @@
1
- name: "Tests"
2
- on:
3
- pull_request:
4
- push:
5
- branches:
6
- - main
7
-
8
- permissions:
9
- contents: read
10
-
11
- jobs:
12
- Test:
13
- strategy:
14
- matrix:
15
- node_version: ["lts/*", "*"]
16
- runs-on: ubuntu-latest
17
- steps:
18
- - name: Checkout Code
19
- uses: actions/checkout@v6
20
- - name: Install Node
21
- uses: actions/setup-node@v6
22
- with:
23
- node-version: ${{ matrix.node_version }}
24
- check-latest: true
25
- - name: Install Dependencies
26
- run: npm ci
27
- - name: Test 👩🏽‍💻
28
- run: npm run test
29
-
30
- Lint:
31
- runs-on: ubuntu-latest
32
- steps:
33
- - name: Checkout Code
34
- uses: actions/checkout@v6
35
- - name: Install Node
36
- uses: actions/setup-node@v6
37
- with:
38
- node-version: "lts/*"
39
- - name: Install Dependencies
40
- run: npm ci
41
- - name: Lint ✨
42
- run: npm run test:lint
43
-
44
- Format:
45
- runs-on: ubuntu-latest
46
- steps:
47
- - name: Checkout Code
48
- uses: actions/checkout@v6
49
- - name: Install Node
50
- uses: actions/setup-node@v6
51
- with:
52
- node-version: "lts/*"
53
- - name: Install Dependencies
54
- run: npm ci
55
- - name: Format 💖
56
- run: npm run test:format
57
-
58
- Release:
59
- # This will only work if the version in package.json is updated
60
- needs: [Test, Lint, Format]
61
- if: |
62
- github.ref == 'refs/heads/main' &&
63
- github.event.repository.fork == false
64
- runs-on: ubuntu-latest
65
- steps:
66
- - name: Checkout Code
67
- uses: actions/checkout@v6
68
- - name: Install Node
69
- uses: actions/setup-node@v6
70
- with:
71
- node-version: "lts/*"
72
- registry-url: https://registry.npmjs.org/
73
- - name: Install Dependencies
74
- run: npm ci
75
- - name: Publish 📝
76
- run: |
77
- PUBLISHED=$(npm info @markedjs/testutils version)
78
- CURRENT=$(node -p "require('./package.json').version")
79
- echo "$PUBLISHED -> $CURRENT"
80
- if [ $PUBLISHED != $CURRENT ]
81
- then
82
- npm publish --tag latest
83
- else
84
- echo "No new version"
85
- fi
86
- env:
87
- NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
package/eslint.config.js DELETED
@@ -1,18 +0,0 @@
1
- import js from "@eslint/js";
2
- import globals from "globals";
3
-
4
- export default [
5
- js.configs.recommended,
6
- {
7
- languageOptions: {
8
- globals: {
9
- ...globals.node,
10
- ...globals.es2021,
11
- },
12
- parserOptions: {
13
- ecmaVersion: "latest",
14
- sourceType: "module",
15
- },
16
- },
17
- },
18
- ];
package/src/get-tests.js DELETED
@@ -1,43 +0,0 @@
1
- import { loadTests } from "./load-tests.js";
2
- import { resolve } from "node:path";
3
-
4
- /**
5
- * Get tests from a directory or file
6
- * @param {string | string[]} dirs Can be a string or an array of strings
7
- * @returns {Tests | Tests[]} The return type will match the input, a tests object or an array of tests objects
8
- */
9
- export async function getTests(dirs) {
10
- if (Array.isArray(dirs)) {
11
- return await Promise.all(dirs.map((dir) => loadTests(dir)));
12
- }
13
-
14
- return await loadTests(dirs);
15
- }
16
-
17
- /**
18
- * Get all marked tests
19
- * @returns {{
20
- * CommonMark: Tests,
21
- * GFM: Tests,
22
- * New: Tests,
23
- * Original: Tests,
24
- * ReDOS: Tests,
25
- * }} All marked spec tests
26
- */
27
- export async function getAllMarkedSpecTests() {
28
- const tests = await getTests([
29
- resolve("./node_modules/marked-repo/test/specs/commonmark"),
30
- resolve("./node_modules/marked-repo/test/specs/gfm"),
31
- resolve("./node_modules/marked-repo/test/specs/new"),
32
- resolve("./node_modules/marked-repo/test/specs/original"),
33
- resolve("./node_modules/marked-repo/test/specs/redos"),
34
- ]);
35
-
36
- return {
37
- CommonMark: tests[0],
38
- GFM: tests[1],
39
- New: tests[2],
40
- Original: tests[3],
41
- ReDOS: tests[4],
42
- };
43
- }
package/src/helpers.js DELETED
@@ -1,8 +0,0 @@
1
- import { resolve, dirname } from "node:path";
2
- import { fileURLToPath } from "node:url";
3
-
4
- const __dirname = dirname(fileURLToPath(import.meta.url));
5
-
6
- export function resolvePath(p) {
7
- return resolve(__dirname, p);
8
- }
@@ -1,67 +0,0 @@
1
- import { HtmlDiffer } from "@markedjs/html-differ";
2
-
3
- const htmlDiffer = new HtmlDiffer({
4
- ignoreSelfClosingSlash: true,
5
- ignoreComments: false,
6
- });
7
-
8
- /**
9
- * Check if html will display the same
10
- * @param {string} actual The actual HTML
11
- * @param {string} expected The expected HTML
12
- * @returns {boolean} HTML is the same
13
- */
14
- export function htmlIsEqual(actual, expected) {
15
- return htmlDiffer.isEqual(actual, expected);
16
- }
17
-
18
- /**
19
- * Get the first difference between actual and expected HTML
20
- * @param {string} actual The actual HTML
21
- * @param {string} expected The expected HTML
22
- * @param {number} padding The number of characters to show around the first difference
23
- * @returns {{
24
- * actual: string,
25
- * expected: string,
26
- * }} An object with the characters around the index of the first difference in the expected and actual strings
27
- */
28
- export async function firstDiff(actual, expected, padding) {
29
- padding = padding || 30;
30
- const diffHtml = await htmlDiffer.diffHtml(actual, expected);
31
- const result = diffHtml.reduce(
32
- (obj, diff) => {
33
- if (diff.added) {
34
- if (obj.firstIndex === null) {
35
- obj.firstIndex = obj.expected.length;
36
- }
37
- obj.expected += diff.value;
38
- } else if (diff.removed) {
39
- if (obj.firstIndex === null) {
40
- obj.firstIndex = obj.actual.length;
41
- }
42
- obj.actual += diff.value;
43
- } else {
44
- obj.actual += diff.value;
45
- obj.expected += diff.value;
46
- }
47
-
48
- return obj;
49
- },
50
- {
51
- firstIndex: null,
52
- actual: "",
53
- expected: "",
54
- },
55
- );
56
-
57
- return {
58
- actual: result.actual.substring(
59
- result.firstIndex - padding,
60
- result.firstIndex + padding,
61
- ),
62
- expected: result.expected.substring(
63
- result.firstIndex - padding,
64
- result.firstIndex + padding,
65
- ),
66
- };
67
- }
package/src/index.js DELETED
@@ -1,4 +0,0 @@
1
- export * from "./get-tests.js";
2
- export * from "./html-differ.js";
3
- export * from "./output-table.js";
4
- export * from "./run-tests.js";
package/src/load-tests.js DELETED
@@ -1,81 +0,0 @@
1
- import fs from "node:fs";
2
- import path from "node:path";
3
- import fm from "front-matter";
4
- import { createRequire } from "node:module";
5
-
6
- const require = createRequire(import.meta.url);
7
-
8
- export async function loadTests(fileOrDir) {
9
- const isFile = fs.statSync(fileOrDir).isFile();
10
- const files = isFile ? [path.basename(fileOrDir)] : fs.readdirSync(fileOrDir);
11
- const dir = isFile ? path.dirname(fileOrDir) : fileOrDir;
12
-
13
- const obj = {};
14
-
15
- for (const file of files) {
16
- const ext = path.extname(file);
17
- const name = path.basename(file, ext);
18
- const absFile = path.join(dir, file);
19
- let specs = [];
20
-
21
- switch (ext) {
22
- case ".md": {
23
- const content = fm(fs.readFileSync(absFile, "utf8"));
24
- const skip = content.attributes.skip;
25
- delete content.attributes.skip;
26
- const only = content.attributes.only;
27
- delete content.attributes.only;
28
- specs.push({
29
- section: name,
30
- markdown: content.body,
31
- html: fs.readFileSync(absFile.replace(/[^.]+$/, "html"), "utf8"),
32
- options: content.attributes,
33
- only,
34
- skip,
35
- });
36
- break;
37
- }
38
- case ".js":
39
- case ".mjs":
40
- case ".cjs":
41
- case ".json": {
42
- let json;
43
- try {
44
- // try require first
45
- json = await require(absFile);
46
- } catch (err) {
47
- if (err.code !== "ERR_REQUIRE_ESM") {
48
- throw err;
49
- }
50
- // must import esm
51
- json = await import(absFile);
52
- }
53
- specs = specs.concat(json);
54
- break;
55
- }
56
- default:
57
- // do nothing
58
- }
59
-
60
- for (let i = 0; i < specs.length; i++) {
61
- const spec = specs[i];
62
- if (!spec.section) {
63
- spec.section = `${name}[${i}]`;
64
- }
65
-
66
- if (!obj[spec.section]) {
67
- obj[spec.section] = {
68
- total: 0,
69
- pass: 0,
70
- specs: [],
71
- };
72
- }
73
-
74
- obj[spec.section].total++;
75
- obj[spec.section].pass += spec.shouldFail ? 0 : 1;
76
- obj[spec.section].specs.push(spec);
77
- }
78
- }
79
-
80
- return obj;
81
- }
@@ -1,39 +0,0 @@
1
- /**
2
- * Display a table in stdout that lists the sections and what percentage of the tests are not marked shouldFail
3
- * @param {string} title The title to display above the table
4
- * @param {Tests} tests The tests to display a table for
5
- */
6
- export function outputCompletionTable(title, tests) {
7
- let longestName = 0;
8
- let maxTests = 0;
9
-
10
- for (const section in tests) {
11
- longestName = Math.max(section.length, longestName);
12
- maxTests = Math.max(tests[section].total, maxTests);
13
- }
14
-
15
- const maxTestsLen = ("" + maxTests).length;
16
- const spaces = maxTestsLen * 2 + longestName + 11;
17
-
18
- console.log("-".padEnd(spaces + 4, "-"));
19
- console.log(
20
- `| ${title
21
- .padStart(Math.ceil((spaces + title.length) / 2))
22
- .padEnd(spaces)} |`,
23
- );
24
- console.log(`| ${" ".padEnd(spaces)} |`);
25
- for (const section in tests) {
26
- console.log(
27
- `| ${section.padEnd(longestName)} ${("" + tests[section].pass).padStart(
28
- maxTestsLen,
29
- )} of ${("" + tests[section].total).padStart(maxTestsLen)} ${(
30
- (100 * tests[section].pass) /
31
- tests[section].total
32
- )
33
- .toFixed()
34
- .padStart(4)}% |`,
35
- );
36
- }
37
- console.log("-".padEnd(spaces + 4, "-"));
38
- console.log();
39
- }
package/src/run-tests.js DELETED
@@ -1,142 +0,0 @@
1
- import { htmlIsEqual, firstDiff } from "./html-differ.js";
2
- import { getTests, getAllMarkedSpecTests } from "./get-tests.js";
3
- import nodeTest from "node:test";
4
- import assert from "node:assert";
5
- import { Marked } from "marked";
6
- import { outputCompletionTable } from "./output-table.js";
7
-
8
- /**
9
- * Run spec tests
10
- * @param {{
11
- * tests: Tests
12
- * defaultMarkedOptions: MarkedOptions,
13
- * parse: (marked: Marked, options: MarkedOptions, addExtension: (marked: Marked) => void) => string,
14
- * addExtension: (marked: Marked) => void,
15
- * isEqual: (actual: string, expected: string) => boolean,
16
- * diff: (actual: string, expected: string, padding: number) => {firstDiff: number, actual: string, expected: string},
17
- * }} options
18
- */
19
- export async function runTests({
20
- tests = {},
21
- defaultMarkedOptions = {},
22
- parse = parseMarked,
23
- addExtension = () => {},
24
- isEqual = htmlIsEqual,
25
- diff = firstDiff,
26
- } = {}) {
27
- if (typeof tests === "string") {
28
- tests = getTests(tests);
29
- }
30
-
31
- for (const section of Object.keys(tests)) {
32
- const hasOnly = tests[section].specs.some((test) => test.only);
33
- await nodeTest(section, { only: hasOnly }, async (t) => {
34
- for (const test of tests[section].specs) {
35
- const options = {
36
- ...defaultMarkedOptions,
37
- ...(test.options || {}),
38
- };
39
- const example = test.example ? " example " + test.example : "";
40
- const passFail = test.shouldFail ? "fail" : "pass";
41
-
42
- if (typeof options.silent === "undefined") {
43
- options.silent = true;
44
- }
45
-
46
- await t.test(
47
- `${section} should ${passFail}${example}`,
48
- {
49
- only: test.only,
50
- skip: test.skip,
51
- },
52
- async () => {
53
- const before = process.hrtime();
54
- let parsed = parse(test.markdown, options, addExtension);
55
- if (options.async) {
56
- parsed = await parsed;
57
- }
58
- const elapsed = process.hrtime(before);
59
- const pass = await isEqual(parsed, test.html);
60
- if (test.shouldFail) {
61
- assert.ok(
62
- !pass,
63
- `${test.markdown}\n------\n\nExpected: Should Fail`,
64
- );
65
- } else if (options.renderExact) {
66
- assert.strictEqual(test.html, parsed);
67
- } else {
68
- const testDiff = await diff(parsed, test.html);
69
- assert.ok(
70
- pass,
71
- `Expected: ${testDiff.expected}\n Actual: ${testDiff.actual}`,
72
- );
73
- }
74
-
75
- if (!options.async && elapsed[0] > 0) {
76
- const s = (elapsed[0] + elapsed[1] * 1e-9).toFixed(3);
77
- assert.fail(`took too long: ${s}s`);
78
- }
79
- },
80
- );
81
- }
82
- });
83
- }
84
- }
85
-
86
- /**
87
- * Run all marked specs with an added extension and optionally output completion table
88
- * @param {{
89
- * addExtension: (marked: Marked) => void,
90
- * outputCompletionTable: boolean,
91
- * }} options
92
- */
93
- export async function runAllMarkedSpecTests({
94
- addExtension = () => {},
95
- outputCompletionTables = true,
96
- } = {}) {
97
- const specTests = await getAllMarkedSpecTests();
98
-
99
- await Promise.all(
100
- Object.keys(specTests).map((title) => {
101
- const tests = specTests[title];
102
- switch (title) {
103
- case "CommonMark":
104
- if (outputCompletionTables) {
105
- outputCompletionTable(title, tests);
106
- }
107
- return runTests({
108
- tests,
109
- defaultMarkedOptions: { gfm: false, pedantic: false },
110
- addExtension,
111
- });
112
- case "GFM":
113
- if (outputCompletionTables) {
114
- outputCompletionTable(title, tests);
115
- }
116
- return runTests({
117
- tests,
118
- defaultMarkedOptions: { gfm: true, pedantic: false },
119
- addExtension,
120
- });
121
- case "New":
122
- return runTests({ tests, addExtension });
123
- case "Original":
124
- return runTests({
125
- tests,
126
- defaultMarkedOptions: { gfm: false, pedantic: true },
127
- addExtension,
128
- });
129
- case "ReDOS":
130
- return runTests({ tests, addExtension });
131
- default:
132
- throw new Error("invalid title");
133
- }
134
- }),
135
- );
136
- }
137
-
138
- function parseMarked(markedown, options, addExtension) {
139
- const marked = new Marked(options);
140
- addExtension(marked);
141
- return marked.parse(markedown);
142
- }
@@ -1,22 +0,0 @@
1
- import { getTests } from "../src/index.js";
2
- import { resolvePath } from "../src/helpers.js";
3
- import test from "node:test";
4
- import assert from "node:assert";
5
-
6
- test("get-tests", async (t) => {
7
- await t.test("getTests(string)", async () => {
8
- const tests = await getTests(
9
- resolvePath("../node_modules/marked-repo/test/specs/commonmark"),
10
- );
11
-
12
- assert.ok(tests.Tabs);
13
- });
14
-
15
- await t.test("getTests(array)", async () => {
16
- const tests = await getTests([
17
- resolvePath("../node_modules/marked-repo/test/specs/commonmark"),
18
- ]);
19
-
20
- assert.ok(tests[0].Tabs);
21
- });
22
- });
@@ -1,31 +0,0 @@
1
- import { htmlIsEqual, firstDiff } from "../src/index.js";
2
- import test from "node:test";
3
- import assert from "node:assert";
4
-
5
- test("html-differ", async (t) => {
6
- await t.test("htmlIsEqual is equal", async () => {
7
- const same = await htmlIsEqual("<p> test </p>", "<p> test </p>");
8
-
9
- assert.ok(same);
10
- });
11
-
12
- await t.test("htmlIsEqual is not equal", async () => {
13
- const same = await htmlIsEqual("<p>test1</p>", "<p>test2</p>");
14
-
15
- assert.ok(!same);
16
- });
17
-
18
- await t.test("firstDiff", async () => {
19
- const diff = await firstDiff("<p>test1</p>", "<p>test2</p>");
20
-
21
- assert.strictEqual(diff.actual, "<p>test1</p>");
22
- assert.strictEqual(diff.expected, "<p>test2</p>");
23
- });
24
-
25
- await t.test("firstDiff padding", async () => {
26
- const diff = await firstDiff("<p>test1</p>", "<p>test2</p>", 5);
27
-
28
- assert.strictEqual(diff.actual, "<p>test1");
29
- assert.strictEqual(diff.expected, "<p>test2");
30
- });
31
- });
@@ -1,24 +0,0 @@
1
- import { outputCompletionTable } from "../src/index.js";
2
- import test from "node:test";
3
- import assert from "node:assert";
4
-
5
- test("output-table", (t) => {
6
- t.test("outputCompletionTable", () => {
7
- let output = "";
8
- t.mock.method(console, "log", (...args) => {
9
- output += args.join(" ") + "\n";
10
- });
11
- const tests = {
12
- Tests: {
13
- total: 1,
14
- pass: 1,
15
- },
16
- };
17
- outputCompletionTable("Title", tests);
18
-
19
- assert.strictEqual(
20
- output,
21
- "----------------------\n| Title |\n| |\n| Tests 1 of 1 100% |\n----------------------\n\n",
22
- );
23
- });
24
- });
@@ -1,8 +0,0 @@
1
- import { runAllMarkedSpecTests } from "../src/index.js";
2
- import test from "node:test";
3
-
4
- test("run-tests", async (t) => {
5
- await t.test("runAllMarkedSpecTests", async () => {
6
- await runAllMarkedSpecTests();
7
- });
8
- });
package/types/index.d.ts DELETED
@@ -1,18 +0,0 @@
1
- import type { MarkedOptions } from "marked";
2
-
3
- export interface Spec {
4
- section?: string;
5
- markdown: string;
6
- html: string;
7
- example?: number;
8
- options?: MarkedOptions;
9
- only?: boolean;
10
- skip?: boolean;
11
- shouldFail?: boolean;
12
- }
13
-
14
- export interface Tests {
15
- total: number;
16
- pass: number;
17
- specs: Spec[];
18
- }