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-04-04 18:05:54 +02:00
|
|
|
|
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;
|
2017-04-04 18:05:54 +02:00
|
|
|
|
|
|
|
// 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-04-04 18:05:54 +02:00
|
|
|
|
2017-06-03 04:04:36 +02:00
|
|
|
if (solo && process.env.CI) {
|
|
|
|
throw new Error("Forgot to remove `solo: true` from test");
|
2017-04-04 18:05:54 +02:00
|
|
|
}
|
|
|
|
|
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-04 18:05:54 +02:00
|
|
|
|
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"
|
|
|
|
);
|
2017-04-04 18:05:54 +02:00
|
|
|
|
2017-05-27 15:52:33 +02:00
|
|
|
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-05-27 15:52:33 +02:00
|
|
|
}
|
2017-06-03 04:04:36 +02:00
|
|
|
]
|
2017-05-27 15:52:33 +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-27 15:52:33 +02:00
|
|
|
|
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
|
|
|
);
|
|
|
|
|
2017-05-27 15:52:33 +02:00
|
|
|
assert.equal(
|
2017-06-03 04:04:36 +02:00
|
|
|
actualBundle.trim().replace(/^\s+$/gm, ""),
|
|
|
|
expectedBundle.trim().replace(/^\s+$/gm, "")
|
2017-05-27 15:52:33 +02:00
|
|
|
);
|
|
|
|
});
|
2017-04-04 18:05:54 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|