|
|
@ -1,66 +1,78 @@
|
|
|
|
import assert from "assert";
|
|
|
|
import assert from 'assert';
|
|
|
|
import * as fs from "fs";
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import { env, normalizeHtml, svelte } from "../helpers.js";
|
|
|
|
import { env, normalizeHtml, svelte } from '../helpers.js';
|
|
|
|
|
|
|
|
|
|
|
|
function tryRequire(file) {
|
|
|
|
function tryRequire(file) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const mod = require(file);
|
|
|
|
const mod = require(file);
|
|
|
|
return mod.default || mod;
|
|
|
|
return mod.default || mod;
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code !== "MODULE_NOT_FOUND") throw err;
|
|
|
|
if (err.code !== 'MODULE_NOT_FOUND') throw err;
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function normalizeWarning(warning) {
|
|
|
|
function normalizeWarning(warning) {
|
|
|
|
warning.frame = warning.frame.replace(/^\n/, '').replace(/^\t+/gm, '').replace(/\s+$/gm, '');
|
|
|
|
warning.frame = warning.frame
|
|
|
|
|
|
|
|
.replace(/^\n/, '')
|
|
|
|
|
|
|
|
.replace(/^\t+/gm, '')
|
|
|
|
|
|
|
|
.replace(/\s+$/gm, '');
|
|
|
|
delete warning.filename;
|
|
|
|
delete warning.filename;
|
|
|
|
delete warning.toString;
|
|
|
|
delete warning.toString;
|
|
|
|
return warning;
|
|
|
|
return warning;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
describe("css", () => {
|
|
|
|
describe('css', () => {
|
|
|
|
fs.readdirSync("test/css/samples").forEach(dir => {
|
|
|
|
fs.readdirSync('test/css/samples').forEach(dir => {
|
|
|
|
if (dir[0] === ".") return;
|
|
|
|
if (dir[0] === '.') return;
|
|
|
|
|
|
|
|
|
|
|
|
// add .solo to a sample directory name to only run that test
|
|
|
|
// add .solo to a sample directory name to only run that test
|
|
|
|
const solo = /\.solo/.test(dir);
|
|
|
|
const solo = /\.solo/.test(dir);
|
|
|
|
const skip = /\.skip/.test(dir);
|
|
|
|
const skip = /\.skip/.test(dir);
|
|
|
|
|
|
|
|
|
|
|
|
if (solo && process.env.CI) {
|
|
|
|
if (solo && process.env.CI) {
|
|
|
|
throw new Error("Forgot to remove `solo: true` from test");
|
|
|
|
throw new Error('Forgot to remove `solo: true` from test');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
(solo ? it.only : skip ? it.skip : it)(dir, () => {
|
|
|
|
(solo ? it.only : skip ? it.skip : it)(dir, () => {
|
|
|
|
const config = tryRequire(`./samples/${dir}/_config.js`) || {};
|
|
|
|
const config = tryRequire(`./samples/${dir}/_config.js`) || {};
|
|
|
|
const input = fs
|
|
|
|
const input = fs
|
|
|
|
.readFileSync(`test/css/samples/${dir}/input.html`, "utf-8")
|
|
|
|
.readFileSync(`test/css/samples/${dir}/input.html`, 'utf-8')
|
|
|
|
.replace(/\s+$/, "");
|
|
|
|
.replace(/\s+$/, '');
|
|
|
|
|
|
|
|
|
|
|
|
const expectedWarnings = (config.warnings || []).map(normalizeWarning);
|
|
|
|
const expectedWarnings = (config.warnings || []).map(normalizeWarning);
|
|
|
|
const domWarnings = [];
|
|
|
|
const domWarnings = [];
|
|
|
|
const ssrWarnings = [];
|
|
|
|
const ssrWarnings = [];
|
|
|
|
|
|
|
|
|
|
|
|
const dom = svelte.compile(input, Object.assign(config, {
|
|
|
|
const dom = svelte.compile(
|
|
|
|
format: 'iife',
|
|
|
|
input,
|
|
|
|
name: 'SvelteComponent',
|
|
|
|
Object.assign(config, {
|
|
|
|
onwarn: warning => {
|
|
|
|
format: 'iife',
|
|
|
|
domWarnings.push(warning);
|
|
|
|
name: 'SvelteComponent',
|
|
|
|
}
|
|
|
|
onwarn: warning => {
|
|
|
|
}));
|
|
|
|
domWarnings.push(warning);
|
|
|
|
|
|
|
|
}
|
|
|
|
const ssr = svelte.compile(input, Object.assign(config, {
|
|
|
|
})
|
|
|
|
format: 'iife',
|
|
|
|
);
|
|
|
|
generate: 'ssr',
|
|
|
|
|
|
|
|
name: 'SvelteComponent',
|
|
|
|
const ssr = svelte.compile(
|
|
|
|
onwarn: warning => {
|
|
|
|
input,
|
|
|
|
ssrWarnings.push(warning);
|
|
|
|
Object.assign(config, {
|
|
|
|
}
|
|
|
|
format: 'iife',
|
|
|
|
}));
|
|
|
|
generate: 'ssr',
|
|
|
|
|
|
|
|
name: 'SvelteComponent',
|
|
|
|
|
|
|
|
onwarn: warning => {
|
|
|
|
|
|
|
|
ssrWarnings.push(warning);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
assert.equal(dom.css, ssr.css);
|
|
|
|
assert.equal(dom.css, ssr.css);
|
|
|
|
|
|
|
|
|
|
|
|
assert.deepEqual(domWarnings.map(normalizeWarning), ssrWarnings.map(normalizeWarning));
|
|
|
|
assert.deepEqual(
|
|
|
|
|
|
|
|
domWarnings.map(normalizeWarning),
|
|
|
|
|
|
|
|
ssrWarnings.map(normalizeWarning)
|
|
|
|
|
|
|
|
);
|
|
|
|
assert.deepEqual(domWarnings.map(normalizeWarning), expectedWarnings);
|
|
|
|
assert.deepEqual(domWarnings.map(normalizeWarning), expectedWarnings);
|
|
|
|
|
|
|
|
|
|
|
|
fs.writeFileSync(`test/css/samples/${dir}/_actual.css`, dom.css);
|
|
|
|
fs.writeFileSync(`test/css/samples/${dir}/_actual.css`, dom.css);
|
|
|
@ -75,25 +87,32 @@ describe("css", () => {
|
|
|
|
if (expected.html !== null) {
|
|
|
|
if (expected.html !== null) {
|
|
|
|
const window = env();
|
|
|
|
const window = env();
|
|
|
|
|
|
|
|
|
|
|
|
const Component = eval(`(function () { ${dom.code}; return SvelteComponent; }())`);
|
|
|
|
const Component = eval(
|
|
|
|
const target = window.document.querySelector("main");
|
|
|
|
`(function () { ${dom.code}; return SvelteComponent; }())`
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
const target = window.document.querySelector('main');
|
|
|
|
|
|
|
|
|
|
|
|
new Component({ target, data: config.data });
|
|
|
|
new Component({ target, data: config.data });
|
|
|
|
const html = target.innerHTML;
|
|
|
|
const html = target.innerHTML;
|
|
|
|
|
|
|
|
|
|
|
|
// dom
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
|
|
|
|
normalizeHtml(window, html.replace(/svelte-\d+/g, 'svelte-xyz')),
|
|
|
|
|
|
|
|
normalizeHtml(window, expected.html)
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.writeFileSync(`test/css/samples/${dir}/_actual.html`, html);
|
|
|
|
fs.writeFileSync(`test/css/samples/${dir}/_actual.html`, html);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// dom
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
|
|
|
|
normalizeHtml(window, html.replace(/svelte-\d+/g, 'svelte-xyz')),
|
|
|
|
|
|
|
|
normalizeHtml(window, expected.html)
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// ssr
|
|
|
|
// ssr
|
|
|
|
const component = eval(`(function () { ${ssr.code}; return SvelteComponent; }())`);
|
|
|
|
const component = eval(
|
|
|
|
|
|
|
|
`(function () { ${ssr.code}; return SvelteComponent; }())`
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
assert.equal(
|
|
|
|
normalizeHtml(window, component.render(config.data).replace(/svelte-\d+/g, 'svelte-xyz')),
|
|
|
|
normalizeHtml(
|
|
|
|
|
|
|
|
window,
|
|
|
|
|
|
|
|
component.render(config.data).replace(/svelte-\d+/g, 'svelte-xyz')
|
|
|
|
|
|
|
|
),
|
|
|
|
normalizeHtml(window, expected.html)
|
|
|
|
normalizeHtml(window, expected.html)
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -104,7 +123,7 @@ describe("css", () => {
|
|
|
|
function read(file) {
|
|
|
|
function read(file) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
return fs.readFileSync(file, 'utf-8');
|
|
|
|
return fs.readFileSync(file, 'utf-8');
|
|
|
|
} catch(err) {
|
|
|
|
} catch (err) {
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|