Skip to content

Commit 4039f9e

Browse files
committed
remove test duplication
1 parent 07aed41 commit 4039f9e

File tree

8 files changed

+62
-147
lines changed

8 files changed

+62
-147
lines changed

packages/svelte/tests/helpers.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,15 @@ export function create_deferred() {
5858
* @param {Partial<CompileOptions>} compileOptions
5959
* @param {boolean} [output_map]
6060
* @param {any} [preprocessor]
61-
* @param {import('./suite').TemplatingMode} [templating_mode]
6261
*/
6362
export async function compile_directory(
6463
cwd,
6564
generate,
6665
compileOptions = {},
6766
output_map = false,
68-
preprocessor,
69-
templating_mode
67+
preprocessor
7068
) {
71-
const output_dir = `${cwd}/_output/${generate}${templating_mode === 'functional' ? `-functional` : ''}`;
69+
const output_dir = `${cwd}/_output/${generate}`;
7270

7371
fs.rmSync(output_dir, { recursive: true, force: true });
7472

@@ -79,8 +77,7 @@ export async function compile_directory(
7977
let opts = {
8078
filename: path.join(cwd, file),
8179
...compileOptions,
82-
generate,
83-
templatingMode: templating_mode
80+
generate
8481
};
8582

8683
if (file.endsWith('.js')) {
@@ -193,3 +190,6 @@ if (typeof window !== 'undefined') {
193190
};
194191
});
195192
}
193+
194+
export const templatingMode =
195+
/** @type {'string' | 'functional'} */ (process.env.TEMPLATING_MODE) ?? 'string';

packages/svelte/tests/hydration/test.ts

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as fs from 'node:fs';
44
import { assert } from 'vitest';
55
import { compile_directory } from '../helpers.js';
66
import { assert_html_equal } from '../html_equal.js';
7+
import { templatingMode } from '../helpers.js';
78
import { assert_ok, suite, type BaseTest } from '../suite.js';
89
import { createClassComponent } from 'svelte/legacy';
910
import { render } from 'svelte/server';
@@ -41,24 +42,15 @@ function read(path: string): string | void {
4142
return fs.existsSync(path) ? fs.readFileSync(path, 'utf-8') : undefined;
4243
}
4344

44-
const { test, run } = suite<HydrationTest>(async (config, cwd, templating_mode) => {
45+
const { test, run } = suite<HydrationTest>(async (config, cwd) => {
4546
if (!config.load_compiled) {
46-
await compile_directory(
47-
cwd,
48-
'client',
49-
{ accessors: true, ...config.compileOptions },
50-
undefined,
51-
undefined,
52-
templating_mode
53-
);
54-
await compile_directory(
55-
cwd,
56-
'server',
57-
config.compileOptions,
58-
undefined,
59-
undefined,
60-
templating_mode
61-
);
47+
await compile_directory(cwd, 'client', {
48+
accessors: true,
49+
templatingMode,
50+
...config.compileOptions
51+
});
52+
53+
await compile_directory(cwd, 'server', config.compileOptions, undefined, undefined);
6254
}
6355

6456
const target = window.document.body;
@@ -116,11 +108,7 @@ const { test, run } = suite<HydrationTest>(async (config, cwd, templating_mode)
116108
};
117109

118110
const component = createClassComponent({
119-
component: (
120-
await import(
121-
`${cwd}/_output/client${templating_mode === 'functional' ? '-functional' : ''}/main.svelte.js`
122-
)
123-
).default,
111+
component: (await import(`${cwd}/_output/client/main.svelte.js`)).default,
124112
target,
125113
hydrate: true,
126114
props: config.props,
@@ -187,5 +175,4 @@ const { test, run } = suite<HydrationTest>(async (config, cwd, templating_mode)
187175
});
188176
export { test, assert_ok };
189177

190-
await run(__dirname, 'string');
191-
await run(__dirname, 'functional');
178+
await run(__dirname);

packages/svelte/tests/runtime-browser/test.ts

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import * as fs from 'node:fs';
44
import * as path from 'node:path';
55
import { compile } from 'svelte/compiler';
66
import { afterAll, assert, beforeAll, describe } from 'vitest';
7-
import { suite, suite_with_variants, type TemplatingMode } from '../suite';
8-
import { write } from '../helpers';
7+
import { suite, suite_with_variants } from '../suite';
8+
import { write, templatingMode } from '../helpers';
99
import type { Warning } from '#compiler';
1010

1111
const assert_file = path.resolve(__dirname, 'assert.js');
@@ -35,50 +35,42 @@ const { run: run_browser_tests } = suite_with_variants<
3535
return false;
3636
},
3737
() => {},
38-
async (config, test_dir, variant, _, templating_mode) => {
39-
await run_test(test_dir, config, variant === 'hydrate', templating_mode);
38+
async (config, test_dir, variant) => {
39+
await run_test(test_dir, config, variant === 'hydrate');
4040
}
4141
);
4242

4343
describe.concurrent(
4444
'runtime-browser',
45-
() => run_browser_tests(__dirname, 'string'),
46-
// Browser tests are brittle and slow on CI
47-
{ timeout: 20000, retry: process.env.CI ? 1 : 0 }
48-
);
49-
50-
describe.concurrent(
51-
'runtime-browser-functional',
52-
() => run_browser_tests(__dirname, 'functional'),
45+
() => run_browser_tests(__dirname),
5346
// Browser tests are brittle and slow on CI
5447
{ timeout: 20000, retry: process.env.CI ? 1 : 0 }
5548
);
5649

5750
const { run: run_ce_tests } = suite<ReturnType<typeof import('./assert').test>>(
58-
async (config, test_dir, templating_mode) => {
59-
await run_test(test_dir, config, false, templating_mode);
51+
async (config, test_dir) => {
52+
await run_test(test_dir, config, false);
6053
}
6154
);
6255

6356
describe.concurrent(
6457
'custom-elements',
65-
() => run_ce_tests(__dirname, 'string', 'custom-elements-samples'),
58+
() => run_ce_tests(__dirname, 'custom-elements-samples'),
6659
// Browser tests are brittle and slow on CI
6760
{ timeout: 20000, retry: process.env.CI ? 1 : 0 }
6861
);
6962

7063
describe.concurrent(
7164
'custom-elements',
72-
() => run_ce_tests(__dirname, 'functional', 'custom-elements-samples'),
65+
() => run_ce_tests(__dirname, 'custom-elements-samples'),
7366
// Browser tests are brittle and slow on CI
7467
{ timeout: 20000, retry: process.env.CI ? 1 : 0 }
7568
);
7669

7770
async function run_test(
7871
test_dir: string,
7972
config: ReturnType<typeof import('./assert').test>,
80-
hydrate: boolean,
81-
templating_mode: TemplatingMode
73+
hydrate: boolean
8274
) {
8375
const warnings: any[] = [];
8476

@@ -102,17 +94,14 @@ async function run_test(
10294
build.onLoad({ filter: /\.svelte$/ }, (args) => {
10395
const compiled = compile(fs.readFileSync(args.path, 'utf-8').replace(/\r/g, ''), {
10496
generate: 'client',
97+
templatingMode,
10598
...config.compileOptions,
10699
immutable: config.immutable,
107100
customElement: test_dir.includes('custom-elements-samples'),
108-
accessors: 'accessors' in config ? config.accessors : true,
109-
templatingMode: templating_mode
101+
accessors: 'accessors' in config ? config.accessors : true
110102
});
111103

112-
write(
113-
`${test_dir}/_output/client${templating_mode === 'functional' ? '-functional' : ''}/${path.basename(args.path)}.js`,
114-
compiled.js.code
115-
);
104+
write(`${test_dir}/_output/client/${path.basename(args.path)}.js`, compiled.js.code);
116105

117106
compiled.warnings.forEach((warning) => {
118107
if (warning.code === 'options_deprecated_accessors') return;
@@ -121,10 +110,7 @@ async function run_test(
121110

122111
if (compiled.css !== null) {
123112
compiled.js.code += `document.head.innerHTML += \`<style>${compiled.css.code}</style>\``;
124-
write(
125-
`${test_dir}/_output/${templating_mode === 'functional' ? '-functional' : ''}/${path.basename(args.path)}.css`,
126-
compiled.css.code
127-
);
113+
write(`${test_dir}/_output/${path.basename(args.path)}.css`, compiled.css.code);
128114
}
129115

130116
return {
@@ -170,8 +156,7 @@ async function run_test(
170156
...config.compileOptions,
171157
immutable: config.immutable,
172158
customElement: test_dir.includes('custom-elements-samples'),
173-
accessors: 'accessors' in config ? config.accessors : true,
174-
templatingMode: templating_mode
159+
accessors: 'accessors' in config ? config.accessors : true
175160
});
176161

177162
return {

packages/svelte/tests/runtime-legacy/shared.ts

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import { proxy } from 'svelte/internal/client';
66
import { flushSync, hydrate, mount, unmount } from 'svelte';
77
import { render } from 'svelte/server';
88
import { afterAll, assert, beforeAll } from 'vitest';
9-
import { compile_directory } from '../helpers.js';
9+
import { compile_directory, templatingMode } from '../helpers.js';
1010
import { setup_html_equal } from '../html_equal.js';
1111
import { raf } from '../animation-helpers.js';
1212
import type { CompileOptions } from '#compiler';
13-
import { suite_with_variants, type BaseTest, type TemplatingMode } from '../suite.js';
13+
import { suite_with_variants, type BaseTest } from '../suite.js';
1414
import { seen } from '../../src/internal/server/dev.js';
1515

1616
type Assert = typeof import('vitest').assert & {
@@ -142,39 +142,34 @@ export function runtime_suite(runes: boolean) {
142142

143143
return false;
144144
},
145-
(config, cwd, templating_mode) => {
146-
return common_setup(cwd, runes, config, templating_mode);
145+
(config, cwd) => {
146+
return common_setup(cwd, runes, config);
147147
},
148-
async (config, cwd, variant, common, templating_mode) => {
149-
await run_test_variant(cwd, config, variant, common, runes, templating_mode);
148+
async (config, cwd, variant, common) => {
149+
await run_test_variant(cwd, config, variant, common, runes);
150150
}
151151
);
152152
}
153153

154-
async function common_setup(
155-
cwd: string,
156-
runes: boolean | undefined,
157-
config: RuntimeTest,
158-
templating_mode: TemplatingMode
159-
) {
154+
async function common_setup(cwd: string, runes: boolean | undefined, config: RuntimeTest) {
160155
const force_hmr = process.env.HMR && config.compileOptions?.dev !== false && !config.error;
161156

162157
const compileOptions: CompileOptions = {
163158
generate: 'client',
164159
rootDir: cwd,
165160
dev: force_hmr ? true : undefined,
166161
hmr: force_hmr ? true : undefined,
162+
templatingMode,
167163
...config.compileOptions,
168164
immutable: config.immutable,
169165
accessors: 'accessors' in config ? config.accessors : true,
170-
runes,
171-
templatingMode: templating_mode
166+
runes
172167
};
173168

174169
// load_compiled can be used for debugging a test. It means the compiler will not run on the input
175170
// so you can manipulate the output manually to see what fixes it, adding console.logs etc.
176171
if (!config.load_compiled) {
177-
await compile_directory(cwd, 'client', compileOptions, undefined, undefined, templating_mode);
172+
await compile_directory(cwd, 'client', compileOptions, undefined, undefined);
178173
await compile_directory(cwd, 'server', compileOptions);
179174
}
180175

@@ -186,8 +181,7 @@ async function run_test_variant(
186181
config: RuntimeTest,
187182
variant: 'dom' | 'hydrate' | 'ssr',
188183
compileOptions: CompileOptions,
189-
runes: boolean,
190-
templating_mode: TemplatingMode
184+
runes: boolean
191185
) {
192186
let unintended_error = false;
193187

@@ -266,14 +260,9 @@ async function run_test_variant(
266260

267261
// Put things we need on window for testing
268262
const styles = globSync('**/*.css', {
269-
cwd: `${cwd}/_output/client${templating_mode === 'functional' ? '-functional' : ''}`
263+
cwd: `${cwd}/_output/client`
270264
})
271-
.map((file) =>
272-
fs.readFileSync(
273-
`${cwd}/_output/client${templating_mode === 'functional' ? '-functional' : ''}/${file}`,
274-
'utf-8'
275-
)
276-
)
265+
.map((file) => fs.readFileSync(`${cwd}/_output/client/${file}`, 'utf-8'))
277266
.join('\n')
278267
.replace(/\/\*<\/?style>\*\//g, '');
279268

@@ -289,9 +278,7 @@ async function run_test_variant(
289278

290279
globalThis.requestAnimationFrame = globalThis.setTimeout;
291280

292-
let mod = await import(
293-
`${cwd}/_output/client${templating_mode === 'functional' ? '-functional' : ''}/main.svelte.js`
294-
);
281+
let mod = await import(`${cwd}/_output/client/main.svelte.js`);
295282

296283
const target = window.document.querySelector('main') as HTMLElement;
297284

packages/svelte/tests/runtime-legacy/test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ const { test, run } = runtime_suite(false);
1111

1212
export { test, ok };
1313

14-
await run(__dirname, 'string');
15-
await run(__dirname, 'functional');
14+
await run(__dirname);

packages/svelte/tests/runtime-runes/test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ const { test, run } = runtime_suite(true);
55

66
export { test, ok };
77

8-
await run(__dirname, 'string');
9-
await run(__dirname, 'functional');
8+
await run(__dirname);

packages/svelte/tests/snapshot/test.ts

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,17 @@ interface SnapshotTest extends BaseTest {
99
compileOptions?: Partial<import('#compiler').CompileOptions>;
1010
}
1111

12-
const { test, run } = suite<SnapshotTest>(async (config, cwd, templating_mode) => {
13-
await compile_directory(
14-
cwd,
15-
'client',
16-
config.compileOptions,
17-
undefined,
18-
undefined,
19-
templating_mode
20-
);
12+
const { test, run } = suite<SnapshotTest>(async (config, cwd) => {
13+
await compile_directory(cwd, 'client', config.compileOptions);
2114
await compile_directory(cwd, 'server', config.compileOptions);
2215

2316
// run `UPDATE_SNAPSHOTS=true pnpm test snapshot` to update snapshot tests
2417
if (process.env.UPDATE_SNAPSHOTS) {
2518
fs.rmSync(`${cwd}/_expected`, { recursive: true, force: true });
2619
fs.cpSync(`${cwd}/_output`, `${cwd}/_expected`, { recursive: true, force: true });
2720
} else {
28-
const actual = globSync('**', { cwd: `${cwd}/_output`, onlyFiles: true }).filter(
29-
// filters out files that might not yet be compiled (functional is executed after string)
30-
(expected) =>
31-
expected.startsWith('server/') ||
32-
expected.startsWith(`client${templating_mode === 'functional' ? '-functional' : ''}/`)
33-
);
34-
const expected = globSync('**', { cwd: `${cwd}/_expected`, onlyFiles: true }).filter(
35-
// filters out files that might not yet be compiled (functional is executed after string)
36-
(expected) =>
37-
expected.startsWith('server/') ||
38-
expected.startsWith(`client${templating_mode === 'functional' ? '-functional' : ''}/`)
39-
);
21+
const actual = globSync('**', { cwd: `${cwd}/_output`, onlyFiles: true });
22+
const expected = globSync('**', { cwd: `${cwd}/_expected`, onlyFiles: true });
4023

4124
assert.deepEqual(actual, expected);
4225

@@ -58,5 +41,4 @@ const { test, run } = suite<SnapshotTest>(async (config, cwd, templating_mode) =
5841

5942
export { test };
6043

61-
await run(__dirname, 'string');
62-
await run(__dirname, 'functional');
44+
await run(__dirname);

0 commit comments

Comments
 (0)