2019-02-18 20:19:53 +01:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as assert from 'assert';
|
2020-09-29 17:19:21 +02:00
|
|
|
import { svelte, loadConfig, tryToLoadJson } from '../helpers';
|
2019-02-18 20:19:53 +01:00
|
|
|
|
|
|
|
describe('vars', () => {
|
2019-10-23 20:52:49 +02:00
|
|
|
fs.readdirSync(`${__dirname}/samples`).forEach(dir => {
|
2019-02-18 20:19:53 +01:00
|
|
|
if (dir[0] === '.') return;
|
|
|
|
|
|
|
|
// add .solo to a sample directory name to only run that test
|
|
|
|
const solo = /\.solo/.test(dir);
|
|
|
|
const skip = /\.skip/.test(dir);
|
|
|
|
|
|
|
|
if (solo && process.env.CI) {
|
|
|
|
throw new Error('Forgot to remove `solo: true` from test');
|
|
|
|
}
|
|
|
|
|
2019-02-25 17:53:36 +01:00
|
|
|
for (const generate of ['dom', 'ssr', false]) {
|
|
|
|
(solo ? it.only : skip ? it.skip : it)(`${dir}, generate: ${generate}`, () => {
|
2019-10-23 20:52:49 +02:00
|
|
|
const config = loadConfig(`${__dirname}/samples/${dir}/_config.js`);
|
|
|
|
const filename = `${__dirname}/samples/${dir}/input.svelte`;
|
2019-02-25 17:53:36 +01:00
|
|
|
const input = fs.readFileSync(filename, 'utf-8').replace(/\s+$/, '');
|
|
|
|
|
|
|
|
const expectedError = tryToLoadJson(
|
2019-10-23 20:52:49 +02:00
|
|
|
`${__dirname}/samples/${dir}/error.json`
|
2019-02-25 17:53:36 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
let result;
|
|
|
|
let error;
|
|
|
|
|
|
|
|
try {
|
|
|
|
result = svelte.compile(input, { ...config.options, generate });
|
|
|
|
config.test(assert, result.vars);
|
|
|
|
} catch (e) {
|
|
|
|
error = e;
|
2019-02-18 20:19:53 +01:00
|
|
|
}
|
|
|
|
|
2019-02-25 17:53:36 +01:00
|
|
|
if (error || expectedError) {
|
|
|
|
if (error && !expectedError) {
|
|
|
|
throw error;
|
|
|
|
}
|
2019-02-18 20:19:53 +01:00
|
|
|
|
2019-02-25 17:53:36 +01:00
|
|
|
if (expectedError && !error) {
|
|
|
|
throw new Error(`Expected an error: ${expectedError.message}`);
|
|
|
|
}
|
2019-02-18 20:19:53 +01:00
|
|
|
|
2019-02-25 17:53:36 +01:00
|
|
|
assert.equal(error.message, expectedError.message);
|
|
|
|
assert.deepEqual(error.start, expectedError.start);
|
|
|
|
assert.deepEqual(error.end, expectedError.end);
|
|
|
|
assert.equal(error.pos, expectedError.pos);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-02-18 20:19:53 +01:00
|
|
|
});
|
|
|
|
});
|