From c09bc954d37f1155d51c722b570f84bf625fbe81 Mon Sep 17 00:00:00 2001 From: tanhauhau Date: Tue, 22 Jun 2021 19:26:18 +0800 Subject: [PATCH] test normalise with preserve comments --- test/helpers.ts | 17 ++++++++++++++--- test/server-side-rendering/index.ts | 8 +++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/test/helpers.ts b/test/helpers.ts index 4de53e6cf91a..101490d4dc2c 100644 --- a/test/helpers.ts +++ b/test/helpers.ts @@ -4,7 +4,7 @@ import glob from 'tiny-glob/sync'; import * as path from 'path'; import * as fs from 'fs'; import * as colors from 'kleur'; -export const assert = (assert$1 as unknown) as typeof assert$1 & { htmlEqual: (actual, expected, message?) => void }; +export const assert = (assert$1 as unknown) as typeof assert$1 & { htmlEqual: (actual, expected, message?) => void, htmlEqualWithComments: (actual, expected, message?) => void }; // for coverage purposes, we need to test source files, // but for sanity purposes, we need to test dist files @@ -118,6 +118,9 @@ function cleanChildren(node) { node.removeChild(child); child = previous; } + } else if (child.nodeType === 8) { + // comment + // do nothing } else { cleanChildren(child); } @@ -137,11 +140,11 @@ function cleanChildren(node) { } } -export function normalizeHtml(window, html) { +export function normalizeHtml(window, html, preserveComments = false) { try { const node = window.document.createElement('div'); node.innerHTML = html - .replace(//g, '') + .replace(/()/g, preserveComments ? '$1' : '') .replace(/>[\s\r\n]+<') .trim(); cleanChildren(node); @@ -162,6 +165,14 @@ export function setupHtmlEqual() { message ); }; + // eslint-disable-next-line no-import-assign + assert.htmlEqualWithComments = (actual, expected, message) => { + assert.deepEqual( + normalizeHtml(window, actual, true), + normalizeHtml(window, expected, true), + message + ); + }; } export function loadConfig(file) { diff --git a/test/server-side-rendering/index.ts b/test/server-side-rendering/index.ts index 15fb6134da29..cc3f20d63717 100644 --- a/test/server-side-rendering/index.ts +++ b/test/server-side-rendering/index.ts @@ -81,11 +81,9 @@ describe('ssr', () => { if (css.code) fs.writeFileSync(`${dir}/_actual.css`, css.code); try { - if (compileOptions.preserveComments) { - assert.equal(html.trim(), expectedHtml.trim()); - } else { - assert.htmlEqual(html, expectedHtml); - } + (compileOptions.preserveComments + ? assert.htmlEqualWithComments + : assert.htmlEqual)(html, expectedHtml); } catch (error) { if (shouldUpdateExpected()) { fs.writeFileSync(`${dir}/_expected.html`, html);