0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-11-25 09:09:35 +01:00
svelte/test/parser/parser.test.js
gtmnayan 3df9d8466f
chore: speed up test execution (#8598)
- shard runtime tests for better use of Vite's test parallelization
- merge custom element and other browser tests to run in one test suite and use esbuild in it
- on some setups only generate code output when test fails
2023-05-22 15:46:27 +02:00

59 lines
1.7 KiB
JavaScript

import * as fs from 'node:fs';
import { assert, describe, it } from 'vitest';
import * as svelte from 'svelte/compiler';
import { try_load_json } from '../helpers.js';
describe('parse', () => {
fs.readdirSync(`${__dirname}/samples`).forEach((dir) => {
if (dir[0] === '.') return;
// add .solo to a sample directory name to only run that test
const solo = /\.solo$/.test(dir);
const skip = !fs.existsSync(`${__dirname}/samples/${dir}/input.svelte`);
if (skip) {
console.warn(
`skipping ${dir} because no input.svelte exists. This could be a leftover folder from a different branch.`
);
}
const it_fn = skip ? it.skip : solo ? it.only : it;
it_fn(dir, () => {
const options = try_load_json(`${__dirname}/samples/${dir}/options.json`) || {};
const input = fs
.readFileSync(`${__dirname}/samples/${dir}/input.svelte`, 'utf-8')
.trimEnd()
.replace(/\r/g, '');
const expectedOutput = try_load_json(`${__dirname}/samples/${dir}/output.json`);
const expectedError = try_load_json(`${__dirname}/samples/${dir}/error.json`);
try {
const { ast } = svelte.compile(
input,
Object.assign({}, options, {
generate: false
})
);
fs.writeFileSync(
`${__dirname}/samples/${dir}/_actual.json`,
JSON.stringify(ast, null, '\t')
);
assert.deepEqual(ast.html, expectedOutput.html);
assert.deepEqual(ast.css, expectedOutput.css);
assert.deepEqual(ast.instance, expectedOutput.instance);
assert.deepEqual(ast.module, expectedOutput.module);
} catch (err) {
if (err.name !== 'ParseError') throw err;
if (!expectedError) throw err;
const { code, message, pos, start } = err;
assert.deepEqual({ code, message, pos, start }, expectedError);
}
});
});
});