-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
import-queries.spec.ts
162 lines (147 loc) · 6.09 KB
/
import-queries.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { browserLogs, fetchFromPage, getText, isBuild, testDir } from '~utils';
import { createServer, ViteDevServer } from 'vite';
import { VERSION } from 'svelte/compiler';
function normalizeSnapshot(result: string) {
// during dev, the import is rewritten but can vary on the v= hash. replace with stable short import
return result
.replace(/\(Svelte v\d+.\d+.\d+-next\.\d+\)/, '(Svelte vXXX)') // stable svelte5 compiler comment
.replace('// Note: compiler output will change before 5.0 is released!', '') // strip svelte5 compiler hint
.replace(/\.js\?v=[0-9a-f]{8}/g, '.js?v=XXX') // vite import analysis import rewrite version query
.replace(/"total": *\d+\.\d+/g, '"total":0.123456789'); // svelte compile stats
}
const svelteMajor = VERSION.split('.', 1)[0];
function snapshotFilename(name: string) {
return `./__snapshots__/svelte-${svelteMajor}/${name}.txt`;
}
// TODO remove .skip in this file once svelte5 output is considered stable
describe.skip('raw', () => {
test('does not have failed requests', async () => {
browserLogs.forEach((msg) => {
expect(msg).not.toMatch('404');
});
});
test('Dummy.svelte', async () => {
expect(await getText('#component button')).toBe('dummy clicks: 0');
});
test('Dummy.svelte?raw', async () => {
const result = await getText('#raw');
expect(result).toMatchFileSnapshot(snapshotFilename('raw'));
});
test('Dummy.svelte?raw&svelte&type=preprocessed', async () => {
const result = await getText('#preprocessed');
expect(result).toMatchFileSnapshot(snapshotFilename('preprocessed'));
});
test('Dummy.svelte?raw&svelte&type=script', async () => {
const result = await getText('#script');
expect(normalizeSnapshot(result)).toMatchFileSnapshot(snapshotFilename('script'));
});
test('Dummy.svelte?raw&svelte&type=script&compilerOptions={"customElement":true}', async () => {
const result = await getText('#wcScript');
expect(normalizeSnapshot(result)).toMatchFileSnapshot(snapshotFilename('custom-element'));
});
test('Dummy.svelte?raw&svelte&type=style', async () => {
const result = await getText('#style');
expect(result).toMatchFileSnapshot(snapshotFilename('style'));
});
test('Dummy.svelte?raw&svelte&type=all&sourcemap', async () => {
const result = await getText('#all');
expect(normalizeSnapshot(result)).toMatchFileSnapshot(snapshotFilename('all'));
});
describe.runIf(!isBuild)('mixed exports', () => {
test('Dummy.svelte?raw&svelte&type=preprocessed', async () => {
const module = await fetchFromPage('src/Dummy.svelte?raw&svelte&type=preprocessed').then(
(res) => res.text()
);
expect(normalizeSnapshot(module)).toMatchFileSnapshot(snapshotFilename('mixed-preprocessed'));
});
test('Dummy.svelte?raw&svelte&type=style', async () => {
const module = await fetchFromPage('src/Dummy.svelte?raw&svelte&type=style').then((res) =>
res.text()
);
expect(normalizeSnapshot(module)).toMatchFileSnapshot(snapshotFilename('mixed-style'));
});
test('Dummy.svelte?raw&svelte&type=script', async () => {
const module = await fetchFromPage('src/Dummy.svelte?raw&svelte&type=script').then((res) =>
res.text()
);
expect(normalizeSnapshot(module)).toMatchFileSnapshot(snapshotFilename('mixed-script'));
});
test('Dummy.svelte?raw&svelte&type=all', async () => {
const module = await fetchFromPage('src/Dummy.svelte?raw&svelte&type=all').then((res) =>
res.text()
);
expect(normalizeSnapshot(module)).toMatchFileSnapshot(snapshotFilename('mixed-all'));
});
});
});
describe.runIf(!isBuild).skip('direct', () => {
test('Dummy.svelte?direct&svelte&type=style&sourcemap&lang.css', async () => {
const response = await fetchFromPage(
'src/Dummy.svelte?direct&svelte&type=style&sourcemap&lang.css',
{
headers: { Accept: 'text/css' }
}
);
expect(response.ok).toBe(true);
expect(response.headers.get('Content-Type')).toBe('text/css');
const css = await response.text();
expect(css).toMatchFileSnapshot(snapshotFilename('direct-css'));
});
test('Dummy.svelte?direct&svelte&type=script&sourcemap&lang.js', async () => {
const response = await fetchFromPage(
'src/Dummy.svelte?direct&svelte&type=script&sourcemap&lang.js',
{
headers: { Accept: 'text/javascript' }
}
);
expect(response.ok).toBe(true);
// vite switched from application/javascript to text/javascript in 5.1
expect(response.headers.get('Content-Type')).toMatch(/^(?:text|application)\/javascript$/);
const js = await response.text();
expect(normalizeSnapshot(js)).toMatchFileSnapshot(snapshotFilename('direct-js'));
});
});
describe.runIf(!isBuild).skip('ssrLoadModule', () => {
let vite: ViteDevServer;
let ssrLoadDummy;
beforeAll(async () => {
vite = await createServer({
root: testDir + '/',
appType: 'custom',
server: { middlewareMode: true, hmr: false }
});
// needed to init plugins
await vite.pluginContainer.buildStart({});
ssrLoadDummy = async (query) =>
vite
.ssrLoadModule('./src/Dummy.svelte' + query, { fixStacktrace: true })
.then((m) => m.default?.code ?? m.default);
return async () => {
await vite.close();
vite = null;
ssrLoadDummy = null;
};
});
test('?raw', async () => {
const result = await ssrLoadDummy('?raw');
expect(result).toMatchFileSnapshot(snapshotFilename('ssr-raw'));
});
test('?raw&svelte&type=preprocessed', async () => {
const result = await ssrLoadDummy('?raw&svelte&type=preprocessed');
expect(result).toMatchFileSnapshot(snapshotFilename('ssr-preprocessed'));
});
test('?raw&svelte&type=script', async () => {
const result = await ssrLoadDummy('?raw&svelte&type=script');
expect(normalizeSnapshot(result)).toMatchFileSnapshot(snapshotFilename('ssr-script'));
});
test('?raw&svelte&type=script&compilerOptions={"customElement":true}', async () => {
const result = await ssrLoadDummy(
'?raw&svelte&type=script&compilerOptions={"customElement":true}'
);
expect(normalizeSnapshot(result)).toMatchFileSnapshot(snapshotFilename('ssr-custom-element'));
});
test('?raw&svelte&type=style', async () => {
const result = await ssrLoadDummy('?raw&svelte&type=style');
expect(result).toMatchFileSnapshot(snapshotFilename('ssr-style'));
});
});