Skip to content

Commit 588a93e

Browse files
authored
(chore) Refactor svelte2tsx tests for expected errors (#869)
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.
1 parent a9dfc85 commit 588a93e

File tree

6 files changed

+68
-76
lines changed

6 files changed

+68
-76
lines changed

packages/svelte2tsx/test/helpers.ts

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function benchmark(fn: () => void) {
88
return -Date.now() + (fn(), Date.now());
99
}
1010

11-
export function readFileSync(path: string) {
11+
function readFileSync(path: string) {
1212
return fs.existsSync(path)
1313
? fs.readFileSync(path, 'utf-8').replace(/\r\n/g, '\n').replace(/\s+$/, '')
1414
: null;
@@ -122,35 +122,54 @@ type TransformSampleFn = (
122122
}
123123
) => ReturnType<typeof htmlx2jsx | typeof svelte2tsx>;
124124

125-
export function test_samples(dir: string, transform: TransformSampleFn, tsx: 'jsx' | 'tsx') {
125+
export function test_samples(dir: string, transform: TransformSampleFn, jsx: 'jsx' | 'tsx') {
126126
for (const sample of each_sample(dir)) {
127127
const svelteFile = sample.folder.find((f) => f.endsWith('.svelte'));
128128

129129
sample.check_dir({
130130
required: ['*.svelte'],
131-
allowed: ['expected.js', `expected.${tsx}`, 'test.js']
131+
allowed: ['expected.js', `expected.${jsx}`, 'expected.error.json']
132132
});
133133

134-
const shouldGenerateExpected = !sample.has(`expected.${tsx}`);
134+
const shouldGenerateExpected = !sample.has(`expected.${jsx}`);
135+
const shouldGenerateError = sample.get('expected.error.json') === '';
135136

136137
sample.it(function () {
137-
if (sample.has('test.js')) {
138-
sample.eval('test.js');
139-
return;
140-
}
141138
const input = sample.get(svelteFile);
142139
const config = {
143140
fileName: svelteFile,
144141
sampleName: sample.name,
145142
emitOnTemplateError: false
146143
};
147144

145+
if (sample.has('expected.error.json')) {
146+
let hadError = false;
147+
try {
148+
transform(input, config);
149+
} catch (error) {
150+
hadError = true;
151+
if (shouldGenerateError) {
152+
sample.generate(
153+
'expected.error.json',
154+
JSON.stringify(error, null, 4) + '\n'
155+
);
156+
} else {
157+
assert.deepEqual(
158+
JSON.parse(sample.get('expected.error.json')),
159+
JSON.parse(JSON.stringify(error))
160+
);
161+
}
162+
}
163+
config.emitOnTemplateError = true;
164+
assert(hadError, `Expected a template error but got none`);
165+
}
166+
148167
const output = transform(input, config);
149168

150169
if (shouldGenerateExpected) {
151-
sample.generate(`expected.${tsx}`, output.code);
170+
sample.generate(`expected.${jsx}`, output.code);
152171
} else {
153-
assert.strictEqual(output.code, sample.get(`expected.${tsx}`));
172+
assert.strictEqual(output.code, sample.get(`expected.${jsx}`));
154173
}
155174

156175
if (sample.has('expected.js')) {
@@ -165,13 +184,3 @@ export function* each_sample(dir: string) {
165184
yield new Sample(dir, name);
166185
}
167186
}
168-
169-
/**
170-
*
171-
* @param {string} dirPath
172-
*/
173-
export function get_input_content(dirPath) {
174-
const filename = fs.readdirSync(dirPath).find((f) => f.endsWith('.svelte'));
175-
const content = readFileSync(`${dirPath}/${filename}`);
176-
return { filename, content };
177-
}

packages/svelte2tsx/test/htmlx2jsx/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,11 @@ import { htmlx2jsx } from '../build/htmlxtojsx';
22
import { test_samples } from '../helpers';
33

44
describe('htmlx2jsx', () => {
5-
test_samples(__dirname, htmlx2jsx, 'jsx');
5+
test_samples(
6+
__dirname,
7+
(input, { emitOnTemplateError }) => {
8+
return htmlx2jsx(input, { emitOnTemplateError });
9+
},
10+
'jsx'
11+
);
612
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "ParseError",
3+
"code": "parse-error",
4+
"start": {
5+
"line": 1,
6+
"column": 8,
7+
"character": 8
8+
},
9+
"end": {
10+
"line": 1,
11+
"column": 8,
12+
"character": 8
13+
},
14+
"pos": 8,
15+
"frame": "1: {abc. }\n ^\n2: {abc?. }\n3: {abc ?}"
16+
}

packages/svelte2tsx/test/htmlx2jsx/samples/editing-mustache/test.js

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "ParseError",
3+
"code": "parse-error",
4+
"start": {
5+
"line": 1,
6+
"column": 4,
7+
"character": 4
8+
},
9+
"end": {
10+
"line": 1,
11+
"column": 4,
12+
"character": 4
13+
},
14+
"pos": 4,
15+
"frame": "1: {a?.}\n ^"
16+
}

packages/svelte2tsx/test/svelte2tsx/samples/editing-mustache/test.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)