0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/create-astro/test/utils.js

52 lines
1.3 KiB
JavaScript

import { execa } from 'execa';
import { dirname } from 'path';
import stripAnsi from 'strip-ansi';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
export const testDir = dirname(__filename);
export const timeout = 25000;
const timeoutError = function (details) {
let errorMsg = 'Timed out waiting for create-astro to respond with expected output.';
if (details) {
errorMsg += '\nLast output: "' + details + '"';
}
return new Error(errorMsg);
};
export function promiseWithTimeout(testFn) {
return new Promise((resolve, reject) => {
let lastStdout;
function onStdout(chunk) {
lastStdout = stripAnsi(chunk.toString()).trim() || lastStdout;
}
const timeoutEvent = setTimeout(() => {
reject(timeoutError(lastStdout));
}, timeout);
function resolver() {
clearTimeout(timeoutEvent);
resolve();
}
testFn(resolver, onStdout);
});
}
export const PROMPT_MESSAGES = {
directory: 'Where would you like to create your new project?',
template: 'How would you like to setup your new project?',
typescript: 'How would you like to setup TypeScript?',
typescriptSucceed: 'next',
};
export function setup(args = []) {
const { stdout, stdin } = execa('../create-astro.mjs', [...args, '--skip-houston', '--dry-run'], {
cwd: testDir,
});
return {
stdin,
stdout,
};
}