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

73 lines
2.0 KiB
TypeScript
Raw Normal View History

2020-09-30 02:19:43 +02:00
import * as assert from 'assert';
import * as fs from 'fs';
import * as path from 'path';
import * as colors from 'kleur';
import { loadConfig, svelte, shouldUpdateExpected } from '../helpers';
2020-09-30 02:19:43 +02:00
describe('js', () => {
2019-10-23 20:52:49 +02:00
fs.readdirSync(`${__dirname}/samples`).forEach(dir => {
2020-09-30 02:19:43 +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) {
2020-09-30 02:19:43 +02:00
throw new Error('Forgot to remove `solo: true` from test');
}
2019-11-17 14:59:13 +01:00
const resolved = path.resolve(`${__dirname}/samples`, dir);
2019-11-06 17:55:07 +01:00
2019-11-17 14:59:13 +01:00
if (!fs.existsSync(`${resolved}/input.svelte`)) {
console.log(colors.red().bold(`Missing file ${dir}/input.svelte. If you recently switched branches you may need to delete this directory`));
return;
}
2019-11-06 17:55:07 +01:00
2019-11-14 22:38:44 +01:00
(solo ? it.only : it)(dir, () => {
2019-11-17 14:59:13 +01:00
const config = loadConfig(`${resolved}/_config.js`);
2020-09-30 02:19:43 +02:00
const input = fs.readFileSync(`${resolved}/input.svelte`, 'utf-8').replace(/\s+$/, '').replace(/\r/g, '');
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;
}
2019-11-17 14:59:13 +01:00
const output = `${resolved}/_actual.js`;
2018-04-16 03:13:39 +02:00
fs.writeFileSync(output, actual);
2018-10-28 03:57:09 +01:00
2019-11-17 14:59:13 +01:00
const expectedPath = `${resolved}/expected.js`;
2018-10-28 03:57:09 +01:00
let expected = '';
try {
2020-09-30 02:19:43 +02:00
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(
2020-09-30 02:19:43 +02:00
actual.trim().replace(/^[ \t]+$/gm, '').replace(/\r/g, ''),
expected.trim().replace(/^[ \t]+$/gm, '').replace(/\r/g, '')
);
} catch (error) {
if (shouldUpdateExpected()) {
fs.writeFileSync(expectedPath, actual);
console.log(`Updated ${expectedPath}.`);
} else {
throw error;
}
}
});
});
});