Skip to content

Commit

Permalink
(chore) Refactor svelte2tsx tests for expected errors (#869)
Browse files Browse the repository at this point in the history
Instead of writing a test file by hand and duplicating the "was an error thrown?" behavior, this is now handled by the test helpers. The expected errors go into a `expected.error.json` file.
  • Loading branch information
pushkine authored Mar 10, 2021
1 parent a9dfc85 commit 588a93e
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 76 deletions.
49 changes: 29 additions & 20 deletions packages/svelte2tsx/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -122,35 +122,54 @@ type TransformSampleFn = (
}
) => ReturnType<typeof htmlx2jsx | typeof svelte2tsx>;

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,
sampleName: sample.name,
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')) {
Expand All @@ -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 };
}
8 changes: 7 additions & 1 deletion packages/svelte2tsx/test/htmlx2jsx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
});
Original file line number Diff line number Diff line change
@@ -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 ?}"
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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 ^"
}

This file was deleted.

0 comments on commit 588a93e

Please sign in to comment.