change html linter, export as test helper
[squeep-html-template-helper] / test / lint-html.js
diff --git a/test/lint-html.js b/test/lint-html.js
new file mode 100644 (file)
index 0000000..26cfed2
--- /dev/null
@@ -0,0 +1,28 @@
+'use strict';
+
+/**
+ * A brief wrapper around html-validate
+ */
+
+const assert = require('node:assert');
+const { HtmlValidate } = require('html-validate'); // eslint-disable-line node/no-unpublished-require
+const stubLogger = require('./stub-logger');
+
+class LintHtml {
+  constructor(logger, ...args) {
+    this.logger = logger;
+    this.htmlValidate = new HtmlValidate(...args);
+  }
+
+  async lint(html) {
+    const ruleViolations = [];
+    const report = await this.htmlValidate.validateString(html);
+    report.results.forEach((m) => {
+      ruleViolations.push(m.ruleId);
+      stubLogger?.debug('LintHtml', 'message', m);
+    })
+    assert(report.valid, '' + ruleViolations.join(', '));
+  }
+}
+
+module.exports = LintHtml;
\ No newline at end of file