0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/integrations/cloudflare/test/test-utils.js
Matthew Phillips 09c1e586ee
Prevent hydration scripts from being rendered in the wrong order (#4080)
* Prevent hydration scripts from being rendered in the wrong order

* Remove comment

* Update jsx

* Remove promise for stop

* Try skipping lit tests

* Stringify these chunks too

* Unskip lit
2022-07-29 10:46:09 -04:00

62 lines
1.3 KiB
JavaScript

import { loadFixture as baseLoadFixture } from '../../../astro/test/test-utils.js';
import { spawn } from 'child_process';
import { fileURLToPath } from 'url';
export { fixLineEndings } from '../../../astro/test/test-utils.js';
export function loadFixture(config) {
if (config?.root) {
config.root = new URL(config.root, import.meta.url);
}
return baseLoadFixture(config);
}
const wranglerPath = fileURLToPath(
new URL('../node_modules/wrangler/bin/wrangler.js', import.meta.url)
);
export function runCLI(basePath, { silent }) {
const script = fileURLToPath(new URL(`${basePath}/dist/_worker.js`, import.meta.url));
const p = spawn('node', [wranglerPath, 'dev', '-l', script]);
p.stderr.setEncoding('utf-8');
p.stdout.setEncoding('utf-8');
const timeout = 10000;
const ready = new Promise(async (resolve, reject) => {
const failed = setTimeout(
() => reject(new Error(`Timed out starting the wrangler CLI`)),
timeout
);
(async function () {
for (const msg of p.stderr) {
if (!silent) {
// eslint-disable-next-line
console.error(msg);
}
}
})();
for await (const msg of p.stdout) {
if (!silent) {
// eslint-disable-next-line
console.log(msg);
}
if (msg.includes(`Listening on`)) {
break;
}
}
clearTimeout(failed);
resolve();
});
return {
ready,
stop() {
p.kill();
},
};
}