diff --git a/packages/svelte2tsx/test/helpers.ts b/packages/svelte2tsx/test/helpers.ts index 15f0c0bf9..a7dbe1855 100644 --- a/packages/svelte2tsx/test/helpers.ts +++ b/packages/svelte2tsx/test/helpers.ts @@ -8,7 +8,7 @@ export function benchmark(fn: () => void) { return -Date.now() + (fn(), Date.now()); } -export function readFileSync(path: string) { +function readFileSync(path: string) { return fs.existsSync(path) ? fs.readFileSync(path, 'utf-8').replace(/\r\n/g, '\n').replace(/\s+$/, '') : null; @@ -122,22 +122,19 @@ type TransformSampleFn = ( } ) => ReturnType; -export function test_samples(dir: string, transform: TransformSampleFn, tsx: 'jsx' | 'tsx') { +export function test_samples(dir: string, transform: TransformSampleFn, jsx: 'jsx' | 'tsx') { for (const sample of each_sample(dir)) { const svelteFile = sample.folder.find((f) => f.endsWith('.svelte')); sample.check_dir({ required: ['*.svelte'], - allowed: ['expected.js', `expected.${tsx}`, 'test.js'] + allowed: ['expected.js', `expected.${jsx}`, 'expected.error.json'] }); - const shouldGenerateExpected = !sample.has(`expected.${tsx}`); + const shouldGenerateExpected = !sample.has(`expected.${jsx}`); + const shouldGenerateError = sample.get('expected.error.json') === ''; sample.it(function () { - if (sample.has('test.js')) { - sample.eval('test.js'); - return; - } const input = sample.get(svelteFile); const config = { fileName: svelteFile, @@ -145,12 +142,34 @@ export function test_samples(dir: string, transform: TransformSampleFn, tsx: 'js emitOnTemplateError: false }; + if (sample.has('expected.error.json')) { + let hadError = false; + try { + transform(input, config); + } catch (error) { + hadError = true; + if (shouldGenerateError) { + sample.generate( + 'expected.error.json', + JSON.stringify(error, null, 4) + '\n' + ); + } else { + assert.deepEqual( + JSON.parse(sample.get('expected.error.json')), + JSON.parse(JSON.stringify(error)) + ); + } + } + config.emitOnTemplateError = true; + assert(hadError, `Expected a template error but got none`); + } + const output = transform(input, config); if (shouldGenerateExpected) { - sample.generate(`expected.${tsx}`, output.code); + sample.generate(`expected.${jsx}`, output.code); } else { - assert.strictEqual(output.code, sample.get(`expected.${tsx}`)); + assert.strictEqual(output.code, sample.get(`expected.${jsx}`)); } if (sample.has('expected.js')) { @@ -165,13 +184,3 @@ export function* each_sample(dir: string) { yield new Sample(dir, name); } } - -/** - * - * @param {string} dirPath - */ -export function get_input_content(dirPath) { - const filename = fs.readdirSync(dirPath).find((f) => f.endsWith('.svelte')); - const content = readFileSync(`${dirPath}/${filename}`); - return { filename, content }; -} diff --git a/packages/svelte2tsx/test/htmlx2jsx/index.ts b/packages/svelte2tsx/test/htmlx2jsx/index.ts index 64b4aa6eb..3e64e4108 100644 --- a/packages/svelte2tsx/test/htmlx2jsx/index.ts +++ b/packages/svelte2tsx/test/htmlx2jsx/index.ts @@ -2,5 +2,11 @@ import { htmlx2jsx } from '../build/htmlxtojsx'; import { test_samples } from '../helpers'; describe('htmlx2jsx', () => { - test_samples(__dirname, htmlx2jsx, 'jsx'); + test_samples( + __dirname, + (input, { emitOnTemplateError }) => { + return htmlx2jsx(input, { emitOnTemplateError }); + }, + 'jsx' + ); }); diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/editing-mustache/expected.error.json b/packages/svelte2tsx/test/htmlx2jsx/samples/editing-mustache/expected.error.json new file mode 100644 index 000000000..afb677f97 --- /dev/null +++ b/packages/svelte2tsx/test/htmlx2jsx/samples/editing-mustache/expected.error.json @@ -0,0 +1,16 @@ +{ + "name": "ParseError", + "code": "parse-error", + "start": { + "line": 1, + "column": 8, + "character": 8 + }, + "end": { + "line": 1, + "column": 8, + "character": 8 + }, + "pos": 8, + "frame": "1: {abc. }\n ^\n2: {abc?. }\n3: {abc ?}" +} diff --git a/packages/svelte2tsx/test/htmlx2jsx/samples/editing-mustache/test.js b/packages/svelte2tsx/test/htmlx2jsx/samples/editing-mustache/test.js deleted file mode 100644 index 970b8e422..000000000 --- a/packages/svelte2tsx/test/htmlx2jsx/samples/editing-mustache/test.js +++ /dev/null @@ -1,26 +0,0 @@ -const assert = require('assert'); -const { htmlx2jsx } = require('../../../build/htmlxtojsx'); -const { get_input_content, readFileSync } = require('../../../helpers'); - -module.exports = function () { - const input = get_input_content(__dirname); - - assert.throws( - () => { - htmlx2jsx(input.content); - }, - { - name: 'ParseError', - code: 'parse-error', - start: { line: 1, column: 8, character: 8 }, - end: { line: 1, column: 8, character: 8 }, - frame: '1: {abc. }\n ^\n2: {abc?. }\n3: {abc ?}' - } - ); - - const expected_path = `${__dirname}/expected.jsx`; - assert.strictEqual( - htmlx2jsx(input.content, { emitOnTemplateError: true }).code, - readFileSync(expected_path).toString() - ); -}; diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/editing-mustache/expected.error.json b/packages/svelte2tsx/test/svelte2tsx/samples/editing-mustache/expected.error.json new file mode 100644 index 000000000..95cf71155 --- /dev/null +++ b/packages/svelte2tsx/test/svelte2tsx/samples/editing-mustache/expected.error.json @@ -0,0 +1,16 @@ +{ + "name": "ParseError", + "code": "parse-error", + "start": { + "line": 1, + "column": 4, + "character": 4 + }, + "end": { + "line": 1, + "column": 4, + "character": 4 + }, + "pos": 4, + "frame": "1: {a?.}\n ^" +} diff --git a/packages/svelte2tsx/test/svelte2tsx/samples/editing-mustache/test.js b/packages/svelte2tsx/test/svelte2tsx/samples/editing-mustache/test.js deleted file mode 100644 index 429136706..000000000 --- a/packages/svelte2tsx/test/svelte2tsx/samples/editing-mustache/test.js +++ /dev/null @@ -1,29 +0,0 @@ -const assert = require('assert'); -const svelte2tsx = require('../../../build/index'); -const { get_input_content, readFileSync } = require('../../../helpers'); - -module.exports = function () { - const input = get_input_content(__dirname); - - assert.throws( - () => { - svelte2tsx(input.content); - }, - { - name: 'ParseError', - code: 'parse-error', - start: { line: 1, column: 4, character: 4 }, - end: { line: 1, column: 4, character: 4 }, - frame: '1: {a?.}\n ^' - } - ); - - const expected_path = `${__dirname}/expected.tsx`; - assert.strictEqual( - svelte2tsx(input.content, { - emitOnTemplateError: true, - filename: input.filename - }).code, - readFileSync(expected_path).toString() - ); -};