0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-11-30 08:56:14 +01:00
svelte/test/js/index.js

71 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-06-03 04:04:36 +02:00
import assert from "assert";
import * as fs from "fs";
import * as path from "path";
import { rollup } from "rollup";
import { svelte } from "../helpers.js";
2017-07-11 21:21:18 +02:00
describe("js", () => {
2017-06-03 04:04:36 +02:00
fs.readdirSync("test/js/samples").forEach(dir => {
if (dir[0] === ".") return;
// add .solo to a sample directory name to only run that test
2017-06-03 04:04:36 +02:00
const solo = /\.solo/.test(dir);
2017-06-03 04:04:36 +02:00
if (solo && process.env.CI) {
throw new Error("Forgot to remove `solo: true` from test");
}
2017-06-03 04:04:36 +02:00
(solo ? it.only : it)(dir, () => {
dir = path.resolve("test/js/samples", dir);
const input = fs
.readFileSync(`${dir}/input.html`, "utf-8")
.replace(/\s+$/, "");
2017-04-17 03:57:00 +02:00
let actual;
try {
2017-06-03 04:04:36 +02:00
actual = svelte.compile(input, {
2017-04-17 03:57:00 +02:00
shared: true
}).code;
2017-06-03 04:04:36 +02:00
} catch (err) {
console.log(err.frame);
2017-04-17 03:57:00 +02:00
throw err;
}
2017-04-08 23:41:26 +02:00
2017-06-03 04:04:36 +02:00
fs.writeFileSync(`${dir}/_actual.js`, actual);
const expected = fs.readFileSync(`${dir}/expected.js`, "utf-8");
const expectedBundle = fs.readFileSync(
`${dir}/expected-bundle.js`,
"utf-8"
);
return rollup({
entry: `${dir}/_actual.js`,
2017-06-03 04:04:36 +02:00
plugins: [
{
resolveId(importee, importer) {
if (!importer) return importee;
if (importee === "svelte/shared.js")
return path.resolve("shared.js");
return null;
}
}
2017-06-03 04:04:36 +02:00
]
}).then(bundle => {
2017-06-03 04:04:36 +02:00
const actualBundle = bundle.generate({ format: "es" }).code;
fs.writeFileSync(`${dir}/_actual-bundle.js`, actualBundle);
2017-05-28 18:49:50 +02:00
assert.equal(
2017-06-03 04:04:36 +02:00
actual.trim().replace(/^\s+$/gm, ""),
expected.trim().replace(/^\s+$/gm, "")
2017-05-28 18:49:50 +02:00
);
assert.equal(
2017-06-03 04:04:36 +02:00
actualBundle.trim().replace(/^\s+$/gm, ""),
expectedBundle.trim().replace(/^\s+$/gm, "")
);
});
});
});
});