0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 01:11:24 +01:00
svelte/test/js/index.js

69 lines
1.7 KiB
JavaScript
Raw Normal View History

import * as assert from "assert";
2017-06-03 04:04:36 +02:00
import * as fs from "fs";
import * as path from "path";
import { loadConfig, svelte, shouldUpdateExpected } from "../helpers.js";
2018-04-13 18:02:07 +02:00
describe("js", () => {
2019-10-23 20:52:49 +02:00
fs.readdirSync(`${__dirname}/samples`).forEach(dir => {
2017-06-03 04:04:36 +02:00
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");
}
2019-11-06 17:55:07 +01:00
dir = path.resolve(`${__dirname}/samples`, dir);
const skip = !fs.existsSync(`${dir}/input.svelte`);
(skip ? it.skip : solo ? it.only : it)(dir, () => {
const config = loadConfig(`${dir}/_config.js`);
2019-02-08 15:32:36 +01:00
const input = fs.readFileSync(`${dir}/input.svelte`, "utf-8").replace(/\s+$/, "");
2018-04-16 03:13:39 +02:00
let actual;
2017-04-17 03:57:00 +02:00
2018-04-16 03:13:39 +02:00
try {
const options = Object.assign(config.options || {});
2018-04-16 03:13:39 +02:00
2019-02-04 04:21:03 +01:00
actual = svelte.compile(input, options).js.code.replace(/generated by Svelte v\d+\.\d+\.\d+(-\w+\.\d+)?/, 'generated by Svelte vX.Y.Z');
2018-04-16 03:13:39 +02:00
} catch (err) {
console.log(err.frame);
throw err;
}
2018-04-16 03:13:39 +02:00
const output = `${dir}/_actual.js`;
fs.writeFileSync(output, actual);
2018-10-28 03:57:09 +01:00
const expectedPath = `${dir}/expected.js`;
2018-10-28 03:57:09 +01:00
let expected = '';
try {
expected = fs.readFileSync(expectedPath, "utf-8");
} catch (error) {
console.log(error);
if (error.code === 'ENOENT') {
// missing expected.js
fs.writeFileSync(expectedPath, actual);
}
}
try {
assert.equal(
actual.trim().replace(/^[ \t]+$/gm, ""),
expected.trim().replace(/^[ \t]+$/gm, "")
);
} catch (error) {
if (shouldUpdateExpected()) {
fs.writeFileSync(expectedPath, actual);
console.log(`Updated ${expectedPath}.`);
} else {
throw error;
}
}
});
});
});