@markedjs/testutils 9.1.0-0 → 9.1.0-2

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/README.md CHANGED
@@ -1,3 +1,111 @@
1
1
  # @markedjs/testutils
2
2
 
3
- Test utilities for marked and marked extensions
3
+ Test utilities for marked and marked extensions.
4
+
5
+ ## Versioning
6
+
7
+ This is versioned as the version of marked that it includes tests for with the prerelease version being an integer that gets incremented when we release a new fix or feature without updating the marked version.
8
+
9
+ We recommend to pin to a single version instead of using a range since any update may contain breaking changes.
10
+
11
+ **Example**
12
+
13
+ ```json
14
+ // package.json
15
+ "dependencies": {
16
+ "@markedjs/testutils": "9.1.0-0"
17
+ }
18
+ ```
19
+
20
+ ## API
21
+
22
+ ### `getAllMarkedSpecTests()`
23
+
24
+ Get all marked [Tests](#tests)
25
+
26
+ ### `getTests(dirs)`
27
+
28
+ Get [Tests](#tests) from a directory or file.
29
+ `dirs` can be a string or an array of strings.
30
+ The return type is the same as the input a Tests object or an array of Tests objects.
31
+
32
+ ### `htmlIsEqual(actual, expected)`
33
+
34
+ Check if html will display the same.
35
+
36
+ ### `firstDiff(actual, expected, padding)`
37
+
38
+ Get the first difference between actual and expected HTML. Returns an object with the characters around the index of the first difference in the expected and actual strings.
39
+
40
+ ### `outputCompletionTable(title, tests)`
41
+
42
+ Display a table in stdout that lists the sections and what percentage of the tests are not marked shouldFail.
43
+
44
+ ### `runTests({tests, defaultMarkedOptions, parse, addExtension, isEqual, diff})`
45
+
46
+ Run spec tests
47
+
48
+ ### `runAllMarkedSpecTests({addExtension, outputCompletionTables})`
49
+
50
+ Run all marked specs with an added extension and optionally output completion table.
51
+
52
+ ## Arguments
53
+
54
+ ### tests
55
+
56
+ ```ts
57
+ interface Tests {
58
+ total: number;
59
+ pass: number;
60
+ specs: Spec[];
61
+ }
62
+
63
+ interface Spec {
64
+ section?: string;
65
+ markdown: string;
66
+ html: string;
67
+ example?: number;
68
+ options?: MarkedOptions;
69
+ only?: boolean;
70
+ skip?: boolean;
71
+ shouldFail?: boolean;
72
+ }
73
+ ```
74
+
75
+ ### defaultMarkedOptions
76
+
77
+ ```ts
78
+ type defaultMarkedOptions = MarkedOptions;
79
+ ```
80
+
81
+ ### parse
82
+
83
+ ```ts
84
+ function parse(
85
+ markdown: string,
86
+ options: MarkedOptions,
87
+ addExtension: addExtension,
88
+ ): string;
89
+ ```
90
+
91
+ ### addExtension
92
+
93
+ ```ts
94
+ function addExtension(marked: Marked): void;
95
+ ```
96
+
97
+ ### isEqual
98
+
99
+ ```ts
100
+ function isEqual(actual: string, expected: string): boolean;
101
+ ```
102
+
103
+ ### diff
104
+
105
+ ```ts
106
+ function diff(
107
+ actual: string,
108
+ expected: string,
109
+ padding: number,
110
+ ): { expected: string; actual: string };
111
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markedjs/testutils",
3
- "version": "9.1.0-0",
3
+ "version": "9.1.0-2",
4
4
  "description": "Test utilities for marked and marked extensions",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/get-tests.js CHANGED
@@ -1,33 +1,43 @@
1
1
  import { loadTests } from "./load-tests.js";
2
2
  import { resolvePath } from "./helpers.js";
3
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
+ */
4
9
  export async function getTests(dirs) {
5
- if (typeof dirs === "string") {
6
- return await loadTests(dirs);
7
- }
8
-
9
10
  if (Array.isArray(dirs)) {
10
11
  return await Promise.all(dirs.map((dir) => loadTests(dir)));
11
12
  }
12
13
 
13
- const keys = Object.keys(dirs);
14
- const tests = await Promise.all(keys.map((key) => loadTests(dirs[key])));
15
- const testsObj = {};
16
- for (let i = 0; i < keys.length; i++) {
17
- testsObj[keys[i]] = tests[i];
18
- }
19
-
20
- return testsObj;
14
+ return await loadTests(dirs);
21
15
  }
22
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
+ */
23
27
  export async function getAllMarkedSpecTests() {
24
- return await getTests({
25
- CommonMark: resolvePath(
26
- "../node_modules/marked-repo/test/specs/commonmark",
27
- ),
28
- GFM: resolvePath("../node_modules/marked-repo/test/specs/gfm"),
29
- New: resolvePath("../node_modules/marked-repo/test/specs/new"),
30
- Original: resolvePath("../node_modules/marked-repo/test/specs/original"),
31
- ReDOS: resolvePath("../node_modules/marked-repo/test/specs/redos"),
32
- });
28
+ const tests = await getTests([
29
+ resolvePath("../node_modules/marked-repo/test/specs/commonmark"),
30
+ resolvePath("../node_modules/marked-repo/test/specs/gfm"),
31
+ resolvePath("../node_modules/marked-repo/test/specs/new"),
32
+ resolvePath("../node_modules/marked-repo/test/specs/original"),
33
+ resolvePath("../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
+ };
33
43
  }
@@ -5,7 +5,26 @@ const htmlDiffer = new HtmlDiffer({
5
5
  ignoreComments: false,
6
6
  });
7
7
 
8
- export const htmlIsEqual = htmlDiffer.isEqual.bind(htmlDiffer);
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
+ */
9
28
  export async function firstDiff(actual, expected, padding) {
10
29
  padding = padding || 30;
11
30
  const diffHtml = await htmlDiffer.diffHtml(actual, expected);
@@ -1,3 +1,8 @@
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
+ */
1
6
  export function outputCompletionTable(title, tests) {
2
7
  let longestName = 0;
3
8
  let maxTests = 0;
package/src/run-tests.js CHANGED
@@ -5,6 +5,17 @@ import assert from "node:assert";
5
5
  import { Marked } from "marked";
6
6
  import { outputCompletionTable } from "./output-table.js";
7
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
+ */
8
19
  export async function runTests({
9
20
  tests = {},
10
21
  defaultMarkedOptions = {},
@@ -71,6 +82,13 @@ export async function runTests({
71
82
  }
72
83
  }
73
84
 
85
+ /**
86
+ * Run all marked specs with an added extension and optionally output completion table
87
+ * @param {{
88
+ * addExtension: (marked: Marked) => void,
89
+ * outputCompletionTable: boolean,
90
+ * }} options
91
+ */
74
92
  export async function runAllMarkedSpecTests({
75
93
  addExtension = () => {},
76
94
  outputCompletionTables = true,
@@ -19,12 +19,4 @@ test("get-tests", async (t) => {
19
19
 
20
20
  assert.ok(tests[0].Tabs);
21
21
  });
22
-
23
- await t.test("getTests(obj)", async () => {
24
- const tests = await getTests({
25
- test: resolvePath("../node_modules/marked-repo/test/specs/commonmark"),
26
- });
27
-
28
- assert.ok(tests.test.Tabs);
29
- });
30
22
  });
package/types/index.d.ts CHANGED
@@ -0,0 +1,18 @@
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
+ }