Skip to content

Commit d66d316

Browse files
authored
chore: handle hydration attributes in prerendering tests (#10181)
Svelte 4 adds these and they make tests snapshots not as predictable, so filter them out
1 parent a71d783 commit d66d316

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

packages/kit/test/prerendering/basics/test/tests.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as fs from 'node:fs';
22
import { fileURLToPath } from 'node:url';
33
import { assert, expect, test } from 'vitest';
4+
import { replace_hydration_attrs } from '../../test-utils';
45

56
const build = fileURLToPath(new URL('../build', import.meta.url));
67

@@ -146,7 +147,7 @@ test('decodes paths when writing files', () => {
146147
});
147148

148149
test('prerendering is set to true in root +layout.js', () => {
149-
const content = read('prerendering-true.html');
150+
const content = replace_hydration_attrs(read('prerendering-true.html'));
150151
expect(content).toMatch('<h1>prerendering: true/true</h1>');
151152
});
152153

@@ -198,7 +199,7 @@ test('$env - includes environment variables', () => {
198199
});
199200

200201
test('prerenders a page in a (group)', () => {
201-
const content = read('grouped.html');
202+
const content = replace_hydration_attrs(read('grouped.html'));
202203
expect(content).toMatch('<h1>grouped</h1>');
203204
});
204205

packages/kit/test/prerendering/options/test/tests.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import * as fs from 'node:fs';
22
import { fileURLToPath } from 'node:url';
33
import { assert, expect, test } from 'vitest';
4+
import { replace_hydration_attrs } from '../../test-utils';
45

56
const build = fileURLToPath(new URL('../build', import.meta.url));
67

78
/** @param {string} file */
89
const read = (file) => fs.readFileSync(`${build}/${file}`, 'utf-8');
910

1011
test('prerenders /path-base', () => {
11-
const content = read('index.html');
12+
const content = replace_hydration_attrs(read('index.html'));
1213
expect(content).toMatch('<h1>hello</h1>');
1314
});
1415

1516
test('prerenders nested /path-base', () => {
16-
const content = read('nested/index.html');
17+
const content = replace_hydration_attrs(read('nested/index.html'));
1718
expect(content).toMatch('<h1>nested hello</h1>');
1819
});
1920

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* For static content, Svelte v4 will add a data-svelte-h attribute.
3+
* Replace the hash with something predictable before comparing the output.
4+
*
5+
* @param {string} html
6+
*/
7+
export function replace_hydration_attrs(html) {
8+
return html.replace(/(\s+)data-svelte-h="svelte-\w+"/g, '');
9+
}

0 commit comments

Comments
 (0)