chore(test): tidy up html equal (#8599)

pull/8608/head
gtmnayan 1 year ago committed by GitHub
parent d35d9045f6
commit 61218d3c9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -155,7 +155,7 @@
"tslib": "^2.5.0",
"typescript": "^5.0.4",
"util": "^0.12.5",
"vitest": "^0.31.0"
"vitest": "^0.31.1"
},
"packageManager": "pnpm@8.4.0"
}

@ -81,7 +81,7 @@ devDependencies:
version: 2.27.5(@typescript-eslint/parser@5.58.0)(eslint@8.35.0)
eslint-plugin-svelte3:
specifier: ^4.0.0
version: 4.0.0(eslint@8.35.0)(svelte@3.58.0)
version: 4.0.0(eslint@8.35.0)(svelte@3.59.1)
estree-walker:
specifier: ^3.0.3
version: 3.0.3
@ -111,7 +111,7 @@ devDependencies:
version: 2.8.8
prettier-plugin-svelte:
specifier: ^2.10.0
version: 2.10.0(prettier@2.8.8)(svelte@3.58.0)
version: 2.10.0(prettier@2.8.8)(svelte@3.59.1)
rollup:
specifier: ^3.20.2
version: 3.20.2
@ -137,8 +137,8 @@ devDependencies:
specifier: ^0.12.5
version: 0.12.5
vitest:
specifier: ^0.31.0
version: 0.31.0(happy-dom@9.18.3)(jsdom@21.1.1)
specifier: ^0.31.1
version: 0.31.1(happy-dom@9.18.3)(jsdom@21.1.1)
packages:
@ -797,39 +797,39 @@ packages:
eslint-visitor-keys: 3.4.0
dev: true
/@vitest/expect@0.31.0:
resolution: {integrity: sha512-Jlm8ZTyp6vMY9iz9Ny9a0BHnCG4fqBa8neCF6Pk/c/6vkUk49Ls6UBlgGAU82QnzzoaUs9E/mUhq/eq9uMOv/g==}
/@vitest/expect@0.31.1:
resolution: {integrity: sha512-BV1LyNvhnX+eNYzJxlHIGPWZpwJFZaCcOIzp2CNG0P+bbetenTupk6EO0LANm4QFt0TTit+yqx7Rxd1qxi/SQA==}
dependencies:
'@vitest/spy': 0.31.0
'@vitest/utils': 0.31.0
'@vitest/spy': 0.31.1
'@vitest/utils': 0.31.1
chai: 4.3.7
dev: true
/@vitest/runner@0.31.0:
resolution: {integrity: sha512-H1OE+Ly7JFeBwnpHTrKyCNm/oZgr+16N4qIlzzqSG/YRQDATBYmJb/KUn3GrZaiQQyL7GwpNHVZxSQd6juLCgw==}
/@vitest/runner@0.31.1:
resolution: {integrity: sha512-imWuc82ngOtxdCUpXwtEzZIuc1KMr+VlQ3Ondph45VhWoQWit5yvG/fFcldbnCi8DUuFi+NmNx5ehMUw/cGLUw==}
dependencies:
'@vitest/utils': 0.31.0
'@vitest/utils': 0.31.1
concordance: 5.0.4
p-limit: 4.0.0
pathe: 1.1.0
dev: true
/@vitest/snapshot@0.31.0:
resolution: {integrity: sha512-5dTXhbHnyUMTMOujZPB0wjFjQ6q5x9c8TvAsSPUNKjp1tVU7i9pbqcKPqntyu2oXtmVxKbuHCqrOd+Ft60r4tg==}
/@vitest/snapshot@0.31.1:
resolution: {integrity: sha512-L3w5uU9bMe6asrNzJ8WZzN+jUTX4KSgCinEJPXyny0o90fG4FPQMV0OWsq7vrCWfQlAilMjDnOF9nP8lidsJ+g==}
dependencies:
magic-string: 0.30.0
pathe: 1.1.0
pretty-format: 27.5.1
dev: true
/@vitest/spy@0.31.0:
resolution: {integrity: sha512-IzCEQ85RN26GqjQNkYahgVLLkULOxOm5H/t364LG0JYb3Apg0PsYCHLBYGA006+SVRMWhQvHlBBCyuByAMFmkg==}
/@vitest/spy@0.31.1:
resolution: {integrity: sha512-1cTpt2m9mdo3hRLDyCG2hDQvRrePTDgEJBFQQNz1ydHHZy03EiA6EpFxY+7ODaY7vMRCie+WlFZBZ0/dQWyssQ==}
dependencies:
tinyspy: 2.1.0
dev: true
/@vitest/utils@0.31.0:
resolution: {integrity: sha512-kahaRyLX7GS1urekRXN2752X4gIgOGVX4Wo8eDUGUkTWlGpXzf5ZS6N9RUUS+Re3XEE8nVGqNyxkSxF5HXlGhQ==}
/@vitest/utils@0.31.1:
resolution: {integrity: sha512-yFyRD5ilwojsZfo3E0BnH72pSVSuLg2356cN1tCEe/0RtDzxTPYwOomIC+eQbot7m6DRy4tPZw+09mB7NkbMmA==}
dependencies:
concordance: 5.0.4
loupe: 2.3.6
@ -1544,14 +1544,14 @@ packages:
semver: 6.3.0
dev: true
/eslint-plugin-svelte3@4.0.0(eslint@8.35.0)(svelte@3.58.0):
/eslint-plugin-svelte3@4.0.0(eslint@8.35.0)(svelte@3.59.1):
resolution: {integrity: sha512-OIx9lgaNzD02+MDFNLw0GEUbuovNcglg+wnd/UY0fbZmlQSz7GlQiQ1f+yX0XvC07XPcDOnFcichqI3xCwp71g==}
peerDependencies:
eslint: '>=8.0.0'
svelte: ^3.2.0
dependencies:
eslint: 8.35.0
svelte: 3.58.0
svelte: 3.59.1
dev: true
/eslint-scope@5.1.1:
@ -2690,14 +2690,14 @@ packages:
engines: {node: '>= 0.8.0'}
dev: true
/prettier-plugin-svelte@2.10.0(prettier@2.8.8)(svelte@3.58.0):
/prettier-plugin-svelte@2.10.0(prettier@2.8.8)(svelte@3.59.1):
resolution: {integrity: sha512-GXMY6t86thctyCvQq+jqElO+MKdB09BkL3hexyGP3Oi8XLKRFaJP1ud/xlWCZ9ZIa2BxHka32zhHfcuU+XsRQg==}
peerDependencies:
prettier: ^1.16.4 || ^2.0.0
svelte: ^3.2.0
dependencies:
prettier: 2.8.8
svelte: 3.58.0
svelte: 3.59.1
dev: true
/prettier@2.8.8:
@ -3000,8 +3000,8 @@ packages:
engines: {node: '>= 0.4'}
dev: true
/svelte@3.58.0:
resolution: {integrity: sha512-brIBNNB76mXFmU/Kerm4wFnkskBbluBDCjx/8TcpYRb298Yh2dztS2kQ6bhtjMcvUhd5ynClfwpz5h2gnzdQ1A==}
/svelte@3.59.1:
resolution: {integrity: sha512-pKj8fEBmqf6mq3/NfrB9SLtcJcUvjYSWyePlfCqN9gujLB25RitWK8PvFzlwim6hD/We35KbPlRteuA6rnPGcQ==}
engines: {node: '>= 8'}
dev: true
@ -3186,8 +3186,8 @@ packages:
which-typed-array: 1.1.9
dev: true
/vite-node@0.31.0(@types/node@14.14.31):
resolution: {integrity: sha512-8x1x1LNuPvE2vIvkSB7c1mApX5oqlgsxzHQesYF7l5n1gKrEmrClIiZuOFbFDQcjLsmcWSwwmrWrcGWm9Fxc/g==}
/vite-node@0.31.1(@types/node@14.14.31):
resolution: {integrity: sha512-BajE/IsNQ6JyizPzu9zRgHrBwczkAs0erQf/JRpgTIESpKvNj9/Gd0vxX905klLkb0I0SJVCKbdrl5c6FnqYKA==}
engines: {node: '>=v14.18.0'}
hasBin: true
dependencies:
@ -3240,8 +3240,8 @@ packages:
fsevents: 2.3.2
dev: true
/vitest@0.31.0(happy-dom@9.18.3)(jsdom@21.1.1):
resolution: {integrity: sha512-JwWJS9p3GU9GxkG7eBSmr4Q4x4bvVBSswaCFf1PBNHiPx00obfhHRJfgHcnI0ffn+NMlIh9QGvG75FlaIBdKGA==}
/vitest@0.31.1(happy-dom@9.18.3)(jsdom@21.1.1):
resolution: {integrity: sha512-/dOoOgzoFk/5pTvg1E65WVaobknWREN15+HF+0ucudo3dDG/vCZoXTQrjIfEaWvQXmqScwkRodrTbM/ScMpRcQ==}
engines: {node: '>=v14.18.0'}
hasBin: true
peerDependencies:
@ -3274,11 +3274,11 @@ packages:
'@types/chai': 4.3.5
'@types/chai-subset': 1.3.3
'@types/node': 14.14.31
'@vitest/expect': 0.31.0
'@vitest/runner': 0.31.0
'@vitest/snapshot': 0.31.0
'@vitest/spy': 0.31.0
'@vitest/utils': 0.31.0
'@vitest/expect': 0.31.1
'@vitest/runner': 0.31.1
'@vitest/snapshot': 0.31.1
'@vitest/spy': 0.31.1
'@vitest/utils': 0.31.1
acorn: 8.8.2
acorn-walk: 8.2.0
cac: 6.7.14
@ -3296,7 +3296,7 @@ packages:
tinybench: 2.5.0
tinypool: 0.5.0
vite: 4.3.5(@types/node@14.14.31)
vite-node: 0.31.0(@types/node@14.14.31)
vite-node: 0.31.1(@types/node@14.14.31)
why-is-node-running: 2.2.2
transitivePeerDependencies:
- less
@ -3456,6 +3456,6 @@ packages:
eslint: 8.35.0
eslint-plugin-import: 2.27.5(@typescript-eslint/parser@5.58.0)(eslint@8.35.0)
eslint-plugin-node: 11.1.0(eslint@8.35.0)
eslint-plugin-svelte3: 4.0.0(eslint@8.35.0)(svelte@3.58.0)
eslint-plugin-svelte3: 4.0.0(eslint@8.35.0)(svelte@3.59.1)
typescript: 5.0.4
dev: true

@ -24,10 +24,11 @@ describe('css', () => {
const it_fn = solo ? it.only : skip ? it.skip : it;
it_fn(dir, async () => {
const config = await try_load_config(`${__dirname}/samples/${dir}/_config.js`);
const cwd = `${__dirname}/samples/${dir}`;
const config = await try_load_config(`${cwd}/_config.js`);
const input = fs
.readFileSync(`${__dirname}/samples/${dir}/input.svelte`, 'utf-8')
.readFileSync(`${cwd}/input.svelte`, 'utf-8')
.replace(/\s+$/, '')
.replace(/\r/g, '');
@ -35,12 +36,12 @@ describe('css', () => {
const dom = svelte.compile(
input,
Object.assign(config.compileOptions || {}, { format: 'cjs' })
Object.assign({}, config.compileOptions || {}, { format: 'cjs' })
);
const ssr = svelte.compile(
input,
Object.assign(config.compileOptions || {}, { format: 'cjs', generate: 'ssr' })
Object.assign({}, config.compileOptions || {}, { format: 'cjs', generate: 'ssr' })
);
assert.equal(dom.css.code, ssr.css.code);
@ -51,10 +52,10 @@ describe('css', () => {
assert.deepEqual(dom_warnings, ssr_warnings);
assert.deepEqual(dom_warnings.map(normalize_warning), expected_warnings);
fs.writeFileSync(`${__dirname}/samples/${dir}/_actual.css`, dom.css.code);
fs.writeFileSync(`${cwd}/_actual.css`, dom.css.code);
const expected = {
html: read(`${__dirname}/samples/${dir}/expected.html`),
css: read(`${__dirname}/samples/${dir}/expected.css`)
html: read(`${cwd}/expected.html`),
css: read(`${cwd}/expected.css`)
};
const actual_css = replace_css_hash(dom.css.code);
@ -62,15 +63,13 @@ describe('css', () => {
assert.equal(actual_css, expected.css);
} catch (error) {
if (should_update_expected()) {
fs.writeFileSync(`${__dirname}/samples/${dir}/expected.css`, actual_css);
fs.writeFileSync(`${cwd}/expected.css`, actual_css);
console.log(`Updated ${dir}/expected.css.`);
} else {
throw error;
}
}
const cwd = `${__dirname}/samples/${dir}`;
let ClientComponent;
let ServerComponent;
@ -102,7 +101,7 @@ describe('css', () => {
new ClientComponent({ target, props: config.props });
const html = target.innerHTML;
fs.writeFileSync(`${__dirname}/samples/${dir}/_actual.html`, html);
fs.writeFileSync(`${cwd}/_actual.html`, html);
const actual_html = replace_css_hash(html);
assert_html_equal(actual_html, expected.html);

@ -29,7 +29,7 @@ export async function try_load_config(path) {
// a whole
// bunch
const _ = 1;
// of lines
// cause
@ -84,7 +84,7 @@ export function show_output(cwd, options = {}) {
try {
const { js } = compile(
fs.readFileSync(`${cwd}/${file}`, 'utf-8'),
Object.assign(options, {
Object.assign({}, options, {
filename: file
})
);
@ -158,142 +158,6 @@ export function create_loader(compileOptions, cwd) {
return (file) => load(path.resolve(cwd, file));
}
function cleanChildren(node) {
let previous = null;
// sort attributes
const attributes = Array.from(node.attributes).sort((a, b) => {
return a.name < b.name ? -1 : 1;
});
attributes.forEach((attr) => {
node.removeAttribute(attr.name);
});
attributes.forEach((attr) => {
node.setAttribute(attr.name, attr.value);
});
for (let child of [...node.childNodes]) {
if (child.nodeType === 3) {
// text
if (
node.namespaceURI === 'http://www.w3.org/2000/svg' &&
node.tagName !== 'text' &&
node.tagName !== 'tspan'
) {
node.removeChild(child);
}
child.data = child.data.replace(/[ \t\n\r\f]+/g, '\n');
if (previous && previous.nodeType === 3) {
previous.data += child.data;
previous.data = previous.data.replace(/[ \t\n\r\f]+/g, '\n');
node.removeChild(child);
child = previous;
}
} else if (child.nodeType === 8) {
// comment
// do nothing
} else {
cleanChildren(child);
}
previous = child;
}
// collapse whitespace
if (node.firstChild && node.firstChild.nodeType === 3) {
node.firstChild.data = node.firstChild.data.replace(/^[ \t\n\r\f]+/, '');
if (!node.firstChild.data.length) node.removeChild(node.firstChild);
}
if (node.lastChild && node.lastChild.nodeType === 3) {
node.lastChild.data = node.lastChild.data.replace(/[ \t\n\r\f]+$/, '');
if (!node.lastChild.data.length) node.removeChild(node.lastChild);
}
}
/**
*
* @param {Window} window
* @param {string} html
* @param {{ removeDataSvelte?: boolean, preserveComments?: boolean }} param2
* @returns
*/
export function normalizeHtml(
window,
html,
{ removeDataSvelte = false, preserveComments = false }
) {
try {
const node = window.document.createElement('div');
node.innerHTML = html
.replace(/(<!--.*?-->)/g, preserveComments ? '$1' : '')
.replace(/(data-svelte-h="[^"]+")/g, removeDataSvelte ? '' : '$1')
.replace(/>[ \t\n\r\f]+</g, '><')
.trim();
cleanChildren(node);
return node.innerHTML.replace(/<\/?noscript\/?>/g, '');
} catch (err) {
throw new Error(`Failed to normalize HTML:\n${html}`);
}
}
/**
* @param {string} html
* @returns {string}
*/
export function normalizeNewline(html) {
return html.replace(/\r\n/g, '\n');
}
/**
* @param {{ removeDataSvelte?: boolean }} options
*/
export function setupHtmlEqual(options = {}) {
// eslint-disable-next-line no-import-assign
assert.htmlEqual = (actual, expected, message) => {
assert.deepEqual(
normalizeHtml(window, actual, options),
normalizeHtml(window, expected, options),
message
);
};
/**
*
* @param {string} actual
* @param {string} expected
* @param {{ preserveComments?: boolean, withoutNormalizeHtml?: boolean }} param2
* @param {string?} message
*/
assert.htmlEqualWithOptions = (
actual,
expected,
{ preserveComments, withoutNormalizeHtml },
message
) => {
assert.deepEqual(
withoutNormalizeHtml
? normalizeNewline(actual).replace(
/(\sdata-svelte-h="[^"]+")/g,
options.removeDataSvelte ? '' : '$1'
)
: normalizeHtml(window, actual, { ...options, preserveComments }),
withoutNormalizeHtml
? normalizeNewline(expected).replace(
/(\sdata-svelte-h="[^"]+")/g,
options.removeDataSvelte ? '' : '$1'
)
: normalizeHtml(window, expected, { ...options, preserveComments }),
message
);
};
}
export function create_deferred() {
/** @type {(value: any) => void} */
let resolve;

@ -1,39 +1,12 @@
import { assert } from 'vitest';
/** @type {HTMLDivElement} */
let _container;
/**
* @param {string} html
* @param {{
* removeDataSvelte?: boolean,
* preserveComments?: boolean,
* }} options
*/
export function normalize_html(html, options = {}) {
const container = (_container ??= document.createElement('div'));
if (!options.preserveComments) {
html = html.replace(/(<!--.*?-->)/g, '');
}
if (options.removeDataSvelte) {
html = html.replace(/(data-svelte-h="[^"]+")/g, '');
}
html = html.replace(/>[ \t\n\r\f]+</g, '><').trim();
container.innerHTML = html;
clean_children(container);
return container.innerHTML.replace(/<\/?noscript\/?>/g, '');
}
/** @param {any} node */
function clean_children(node) {
let previous = null;
// sort attributes
const attributes = Array.from(node.attributes).sort((a, b) => (a.name < b.name ? -1 : 1));
const attributes = Array.from(node.attributes).sort((a, b) => {
return a.name < b.name ? -1 : 1;
});
attributes.forEach((attr) => {
node.removeAttribute(attr.name);
@ -43,9 +16,7 @@ function clean_children(node) {
node.setAttribute(attr.name, attr.value);
});
let previous = null;
// recurse
[...node.childNodes].forEach((child) => {
for (let child of [...node.childNodes]) {
if (child.nodeType === 3) {
// text
if (
@ -73,7 +44,7 @@ function clean_children(node) {
}
previous = child;
});
}
// collapse whitespace
if (node.firstChild && node.firstChild.nodeType === 3) {
@ -88,40 +59,99 @@ function clean_children(node) {
}
/**
*
* @param {string} actual
* @param {string} expected
* @param {{
* message?: string,
* normalize_html?: {
* removeDataSvelte?: boolean,
* preserveComments?: boolean,
* },
* without_normalize?: boolean,
* }} options
* @param {Window} window
* @param {string} html
* @param {{ removeDataSvelte?: boolean, preserveComments?: boolean }} param2
*/
export function assert_html_equal(actual, expected, options = {}) {
if (options.without_normalize) {
actual = actual.replace(/\r\n/g, '\n');
expected = expected.replace(/\r\n/g, '\n');
if (options.normalize_html.removeDataSvelte) {
actual = actual.replace(/(\sdata-svelte-h="[^"]+")/g, '');
expected = expected.replace(/(\sdata-svelte-h="[^"]+")/g, '');
}
} else {
actual = normalize_html(actual, options.normalize_html);
expected = normalize_html(expected, options.normalize_html);
}
export function normalize_html(
window,
html,
{ removeDataSvelte = false, preserveComments = false }
) {
try {
assert.equal(actual, expected, options.message);
const node = window.document.createElement('div');
node.innerHTML = html
.replace(/(<!--.*?-->)/g, preserveComments ? '$1' : '')
.replace(/(data-svelte-h="[^"]+")/g, removeDataSvelte ? '' : '$1')
.replace(/>[ \t\n\r\f]+</g, '><')
.trim();
clean_children(node);
return node.innerHTML.replace(/<\/?noscript\/?>/g, '');
} catch (err) {
// Remove this function from the stack trace so that the error is shown in the test file
throw new Error(`Failed to normalize HTML:\n${html}`);
}
}
/**
* @param {string} html
* @returns {string}
*/
export function normalize_new_line(html) {
return html.replace(/\r\n/g, '\n');
}
if (Error.captureStackTrace) {
Error.captureStackTrace(err, assert_html_equal);
/**
* @param {{ removeDataSvelte?: boolean }} options
*/
export function setup_html_equal(options = {}) {
/**
* @param {string} actual
* @param {string} expected
* @param {string} [message]
*/
const assert_html_equal = (actual, expected, message) => {
try {
assert.deepEqual(
normalize_html(window, actual, options),
normalize_html(window, expected, options),
message
);
} catch (e) {
if (Error.captureStackTrace) Error.captureStackTrace(e, assert_html_equal);
throw e;
}
throw err;
}
};
/**
*
* @param {string} actual
* @param {string} expected
* @param {{ preserveComments?: boolean, withoutNormalizeHtml?: boolean }} param2
* @param {string} [message]
*/
const assert_html_equal_with_options = (
actual,
expected,
{ preserveComments, withoutNormalizeHtml },
message
) => {
try {
assert.deepEqual(
withoutNormalizeHtml
? normalize_new_line(actual).replace(
/(\sdata-svelte-h="[^"]+")/g,
options.removeDataSvelte ? '' : '$1'
)
: normalize_html(window, actual, { ...options, preserveComments }),
withoutNormalizeHtml
? normalize_new_line(expected).replace(
/(\sdata-svelte-h="[^"]+")/g,
options.removeDataSvelte ? '' : '$1'
)
: normalize_html(window, expected, { ...options, preserveComments }),
message
);
} catch (e) {
if (Error.captureStackTrace) Error.captureStackTrace(e, assert_html_equal_with_options);
throw e;
}
};
return {
assert_html_equal,
assert_html_equal_with_options
};
}
// Common case without options
export const { assert_html_equal, assert_html_equal_with_options } = setup_html_equal();

@ -5,7 +5,6 @@ import * as fs from 'fs';
import * as path from 'path';
import { assert, describe, it } from 'vitest';
import { create_loader, should_update_expected, try_load_config } from '../helpers.js';
import { assert_html_equal } from '../html_equal.js';
describe('hydration', async () => {
@ -20,7 +19,7 @@ describe('hydration', async () => {
it_fn(dir, async () => {
const cwd = path.resolve(`${__dirname}/samples/${dir}`);
let compileOptions = Object.assign({}, config.compileOptions, {
const compileOptions = Object.assign({}, config.compileOptions, {
accessors: 'accessors' in config ? config.accessors : true,
format: 'cjs',
hydratable: true

@ -33,7 +33,7 @@ describe('js-output', () => {
let actual;
try {
const options = Object.assign(config.options || {});
const options = Object.assign({}, config.options || {});
actual = svelte
.compile(input, options)

@ -32,7 +32,7 @@ describe('parse', () => {
try {
const { ast } = svelte.compile(
input,
Object.assign(options, {
Object.assign({}, options, {
generate: false
})
);

@ -1,4 +1,6 @@
// @ts-expect-error
import SvelteComponent from '__MAIN_DOT_SVELTE__';
// @ts-expect-error
import config from '__CONFIG__';
import * as assert from 'assert';
@ -16,6 +18,8 @@ export default async function (target) {
{},
{
target,
// @ts-expect-error
// eslint-disable-next-line no-undef
hydrate: __HYDRATE__,
props: config.props,
intro: config.intro
@ -63,7 +67,7 @@ export default async function (target) {
if (config.after_test) config.after_test();
} catch (error) {
if (config.error) {
assert.equal(err.message, config.error);
assert.equal(error.message, config.error);
} else {
throw error;
}

@ -6,20 +6,24 @@ import glob from 'tiny-glob/sync.js';
import { beforeAll, afterAll, describe, it, assert } from 'vitest';
import { compile } from '../../src/compiler/index.js';
import { clear_loops, flush, set_now, set_raf } from 'svelte/internal';
import { show_output, try_load_config, mkdirp, create_loader, setupHtmlEqual } from '../helpers.js';
import { show_output, try_load_config, mkdirp, create_loader } from '../helpers.js';
import { setTimeout } from 'timers/promises';
import { setup_html_equal } from '../html_equal.js';
let unhandled_rejection = false;
function unhandledRejection_handler(err) {
unhandled_rejection = err;
}
let listeners = process.rawListeners('unhandledRejection');
const listeners = process.rawListeners('unhandledRejection');
const { assert_html_equal, assert_html_equal_with_options } = setup_html_equal({
removeDataSvelte: true
});
describe('runtime', async () => {
beforeAll(() => {
process.prependListener('unhandledRejection', unhandledRejection_handler);
return setupHtmlEqual({ removeDataSvelte: true });
});
afterAll(() => {
@ -54,7 +58,7 @@ describe('runtime', async () => {
const cwd = path.resolve(`${__dirname}/samples/${dir}`);
const compileOptions = Object.assign(config.compileOptions || {}, {
const compileOptions = Object.assign({}, config.compileOptions || {}, {
format: 'cjs',
hydratable: hydrate,
immutable: config.immutable,
@ -129,6 +133,7 @@ describe('runtime', async () => {
}
// Put things we need on window for testing
// @ts-ignore
window.SvelteComponent = SvelteComponent;
window.location.href = '';
window.document.title = '';
@ -192,7 +197,7 @@ describe('runtime', async () => {
}
if (config.html) {
assert.htmlEqualWithOptions(target.innerHTML, config.html, {
assert_html_equal_with_options(target.innerHTML, config.html, {
withoutNormalizeHtml: config.withoutNormalizeHtml
});
}
@ -200,7 +205,11 @@ describe('runtime', async () => {
try {
if (config.test) {
await config.test({
assert,
assert: {
...assert,
htmlEqual: assert_html_equal,
htmlEqualWithOptions: assert_html_equal_with_options
},
component,
mod,
target,
@ -213,11 +222,12 @@ describe('runtime', async () => {
}
} finally {
component.$destroy();
assert.htmlEqual(target.innerHTML, '');
assert_html_equal(target.innerHTML, '');
// TODO: This seems useless, unhandledRejection is only triggered on the next task
// by which time the test has already finished and the next test resets it to null above
if (unhandled_rejection) {
// eslint-disable-next-line no-unsafe-finally
throw unhandled_rejection;
}
}
@ -248,7 +258,7 @@ describe('runtime', async () => {
// Free up the microtask queue, so that
// 1. Vitest's test runner which uses setInterval can log progress
// 2. Any expected unhandled rejections are ran before we reattach the listeners
await setTimeout();
await setTimeout(0);
if (config.expect_unhandled_rejections) {
listeners.forEach((listener) => {

@ -3,7 +3,7 @@ import { tick } from 'svelte';
let value = 'initial';
let subscribers = [];
let observable = {
const observable = {
subscribe: (fn) => {
subscribers.push(fn);

@ -14,7 +14,7 @@ import {
try_load_json,
try_read_file
} from '../helpers.js';
import { assert_html_equal } from '../html_equal.js';
import { assert_html_equal_with_options } from '../html_equal.js';
describe('ssr', async () => {
async function run_test(dir) {
@ -50,9 +50,9 @@ describe('ssr', async () => {
if (css.code) fs.writeFileSync(`${dir}/_actual.css`, css.code);
try {
assert_html_equal(html, expectedHtml, {
normalize_html: { preserveComments: compileOptions.preserveComments },
without_normalize: config.withoutNormalizeHtml
assert_html_equal_with_options(html, expectedHtml, {
preserveComments: compileOptions.preserveComments,
withoutNormalizeHtml: config.withoutNormalizeHtml
});
} catch (error) {
if (should_update_expected()) {
@ -83,9 +83,13 @@ describe('ssr', async () => {
fs.writeFileSync(`${dir}/_actual-head.html`, head);
try {
assert_html_equal(head, fs.readFileSync(`${dir}/_expected-head.html`, 'utf-8'), {
normalize_html: { preserveComments: compileOptions.hydratable }
});
assert_html_equal_with_options(
head,
fs.readFileSync(`${dir}/_expected-head.html`, 'utf-8'),
{
preserveComments: compileOptions.hydratable
}
);
} catch (error) {
if (should_update_expected()) {
fs.writeFileSync(`${dir}/_expected-head.html`, head);

@ -1,12 +1,13 @@
// @vitest-environment jsdom
import * as path from 'path';
import { describe, it, assert } from 'vitest';
import * as fs from 'fs';
import { try_load_config, mkdirp, create_loader } from '../helpers.js';
import { assert_html_equal } from '../html_equal.js';
import glob from 'tiny-glob/sync';
import * as path from 'path';
import { setTimeout } from 'timers/promises';
import glob from 'tiny-glob/sync';
import { assert, describe, it } from 'vitest';
import { compile } from '../../compiler.mjs';
import { create_loader, mkdirp, try_load_config } from '../helpers.js';
import { assert_html_equal, assert_html_equal_with_options } from '../html_equal.js';
// duplicate client-side tests, as far as possible
run_runtime_samples('runtime');
@ -38,11 +39,11 @@ function run_runtime_samples(suite) {
const load = create_loader(compileOptions, cwd);
glob('**/*.svelte', { cwd: cwd }).forEach((file) => {
glob('**/*.svelte', { cwd }).forEach((file) => {
if (file[0] === '_') return;
const dir = `${cwd}/_output/ssr`;
const out = `${cwd}/${file.replace(/\.svelte$/, '.js')}`;
const out = `${dir}/${file.replace(/\.svelte$/, '.js')}`;
if (fs.existsSync(out)) {
fs.unlinkSync(out);
@ -71,18 +72,14 @@ function run_runtime_samples(suite) {
});
if (config.ssrHtml) {
assert_html_equal(html, config.ssrHtml, {
normalize_html: {
preserveComments: compileOptions.preserveComments,
withoutNormalizeHtml: config.withoutNormalizeHtml
}
assert_html_equal_with_options(html, config.ssrHtml, {
preserveComments: compileOptions.preserveComments,
withoutNormalizeHtml: config.withoutNormalizeHtml
});
} else if (config.html) {
assert_html_equal(html, config.html, {
normalize_html: {
preserveComments: compileOptions.preserveComments,
withoutNormalizeHtml: config.withoutNormalizeHtml
}
assert_html_equal_with_options(html, config.html, {
preserveComments: compileOptions.preserveComments,
withoutNormalizeHtml: config.withoutNormalizeHtml
});
}
@ -90,7 +87,8 @@ function run_runtime_samples(suite) {
await config.test_ssr({
assert: {
...assert,
htmlEqual: assert_html_equal
htmlEqual: assert_html_equal,
htmlEqualWithOptions: assert_html_equal_with_options
},
load
});
@ -112,7 +110,7 @@ function run_runtime_samples(suite) {
}
// wait for vitest to report progress
await setTimeout(10);
await setTimeout(0);
});
}
}

Loading…
Cancel
Save