Skip to content

Commit d92f2b3

Browse files
committed
wip(tests): add body processor to test tools
1 parent fe822fe commit d92f2b3

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

tests/testsTools.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,60 @@ const mime = require('mime-types');
55

66
const local = new URL('http://local/');
77

8-
async function createPage({ files }) {
8+
async function createPage({ files, processors= {} }) {
99

10-
async function getFile(url, encoding) {
10+
async function getFile(url) {
1111

1212
const { origin, pathname } = new URL(url);
1313

1414
if ( origin !== local.origin )
1515
return null
1616

17+
let body = files[pathname]
18+
if (processors[pathname]) {
19+
body = processors[pathname](body)
20+
}
21+
1722
const res = {
1823
contentType: mime.lookup(Path.extname(pathname)) || '',
19-
body: files[pathname],
24+
body,
2025
status: files[pathname] === undefined ? 404 : 200,
2126
};
2227

2328
return res;
2429
}
2530

2631
const page = await browser.newPage();
27-
2832
page.setDefaultTimeout(3000);
2933

30-
3134
await page.setRequestInterception(true);
3235
page.on('request', async interceptedRequest => {
33-
34-
// console.log(interceptedRequest.url())
35-
3636
try {
3737

3838
const file = await getFile(interceptedRequest.url(), 'utf-8');
39-
if ( file !== null ) {
40-
39+
if (file) {
4140
return void interceptedRequest.respond({
4241
...file,
4342
contentType: file.contentType + '; charset=utf-8',
4443
});
4544
}
4645

4746
interceptedRequest.continue();
48-
4947
} catch (ex) {
50-
5148
page.emit('pageerror', ex)
5249
}
5350
});
5451

5552
const output = [];
5653

57-
page.on('console', async msg => output.push({ type: msg.type(), content: await Promise.all( msg.args().map(e => e.jsonValue()) ) }) );
58-
page.on('pageerror', error => output.push({ type: 'pageerror', content: error }) );
54+
page.on('console', async msg => {
55+
console.log(msg)
56+
output.push({ type: msg.type(), text: msg.text(), content: await Promise.all( msg.args().map(e => e.jsonValue()) ) })
57+
} );
58+
page.on('pageerror', error => {
59+
console.log(error)
60+
output.push({ type: 'pageerror', content: error })
61+
} );
5962

6063
page.on('error', msg => console.log('ERROR', msg));
6164

@@ -99,7 +102,6 @@ afterAll(async () => {
99102
const defaultFiles = {
100103
'/vue3-sfc-loader.js': Fs.readFileSync(Path.join(__dirname, '../dist/vue3-sfc-loader.js'), { encoding: 'utf-8' }),
101104
'/vue': Fs.readFileSync(Path.join(__dirname, '../node_modules/vue/dist/vue.global.js'), { encoding: 'utf-8' }),
102-
103105
'/options.js': `
104106
105107
class HttpError extends Error {
@@ -132,7 +134,6 @@ const defaultFiles = {
132134
},
133135
134136
getFile(path) {
135-
136137
return fetch(path).then(res => res.ok ? res.text() : Promise.reject(new HttpError(path, res)));
137138
},
138139
@@ -151,7 +152,9 @@ const defaultFiles = {
151152
152153
export default options;
153154
`,
154-
155+
'/optionsOverride.js': `
156+
export default () => {};
157+
`,
155158
'/boot.js': `
156159
export default ({ options, createApp, mountApp }) => mountApp( createApp(options) );
157160
`,
@@ -161,10 +164,12 @@ const defaultFiles = {
161164
<html><body>
162165
<script src="vue"></script>
163166
<script src="vue3-sfc-loader.js"></script>
167+
<!-- scripts -->
164168
<script type="module">
165169
166170
import boot from '/boot.js'
167171
import options from '/options.js'
172+
import optionsOverride from '/optionsOverride.js'
168173
169174
const { loadModule } = window['vue3-sfc-loader'];
170175
@@ -185,6 +190,8 @@ const defaultFiles = {
185190
186191
return app.mount('#' + eltId);
187192
}
193+
194+
optionsOverride(options)
188195
189196
boot({ options, createApp, mountApp, Vue });
190197
@@ -199,6 +206,9 @@ const defaultFilesVue2 = {
199206
'/vue2-sfc-loader.js': Fs.readFileSync(Path.join(__dirname, '../dist/vue2-sfc-loader.js'), { encoding: 'utf-8' }),
200207
'/vue': Fs.readFileSync(Path.join(__dirname, '../node_modules/vue2/dist/vue.js'), { encoding: 'utf-8' }),
201208
'/options.js': defaultFiles['/options.js'],
209+
'/optionsOverride.js': `
210+
export default () => {};
211+
`,
202212
'/boot.js': `
203213
export default ({ options, createApp, mountApp }) => createApp(options).then(app => mountApp(app));
204214
`,
@@ -207,10 +217,12 @@ const defaultFilesVue2 = {
207217
<html><body>
208218
<script src="vue"></script>
209219
<script src="vue2-sfc-loader.js"></script>
220+
<!-- scripts -->
210221
<script type="module">
211222
212223
import boot from '/boot.js'
213224
import options from '/options.js'
225+
import optionsOverride from '/optionsOverride.js'
214226
215227
const { loadModule } = window['vue2-sfc-loader'];
216228
@@ -236,6 +248,8 @@ const defaultFilesVue2 = {
236248
return app.$mount('#' + mountElId);
237249
}
238250
251+
optionsOverride(options)
252+
239253
boot({ options, createApp, mountApp, Vue });
240254
241255
//window._done && window._done();

0 commit comments

Comments
 (0)