flecks/packages/headless/test/helpers/with-web.js

59 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2024-02-14 20:16:31 -06:00
import {startServer} from '@flecks/server/test/helpers/start-server';
2024-02-11 21:03:06 -06:00
2024-02-14 21:41:30 -06:00
import {createBrowser, connectPage} from './connect';
2024-02-11 21:03:06 -06:00
export function withWeb(task, options) {
return async function withWeb() {
2024-02-14 21:41:30 -06:00
const optionsWithTask = {...options, task: this};
2024-02-15 04:55:47 -06:00
const server = await startServer({
...optionsWithTask,
opts: {
...optionsWithTask.opts,
env: {
FLECKS_ENV__flecks_web__port: '0',
FLECKS_ENV__flecks_web__devPort: '0',
2024-02-17 07:02:03 -06:00
...optionsWithTask.opts?.env,
2024-02-15 04:55:47 -06:00
},
},
});
2024-02-14 21:41:30 -06:00
const socket = await server.waitForSocket(optionsWithTask);
2024-02-11 21:03:06 -06:00
if (options.beforeConnect) {
await options.beforeConnect({server, socket});
}
const start = Date.now();
const previousTimeout = this.timeout();
this.timeout(0);
2024-02-15 04:55:47 -06:00
const {payload} = await socket.send({type: 'web.public'});
2024-02-11 21:03:06 -06:00
this.timeout(previousTimeout + (Date.now() - start));
2024-02-14 21:41:30 -06:00
const {browser, page} = await createBrowser(optionsWithTask);
2024-02-14 22:35:10 -06:00
if (options.beforePage) {
await options.beforePage({
browser,
page,
server,
socket,
});
}
2024-02-17 07:02:03 -06:00
const {pagePath = '/'} = options;
const response = await connectPage(page, `http://${payload}${pagePath}`, optionsWithTask);
2024-02-14 22:35:10 -06:00
let taskError;
try {
await task({
browser,
page,
response,
server,
socket,
task: this,
});
}
catch (error) {
taskError = error;
}
await browser.close();
if (taskError) {
throw taskError;
}
2024-02-11 21:03:06 -06:00
};
}