astro/packages/create-astro/test/utils.js
Nate Moore 0408376281
[Create Astro] Improved prompts, template handling, Houston (#5088)
* feat(create-astro): add houston, improve prompts

* refactor(create-astro): move to giget

* chore: add changeset

* chore: update lockfile

* test(create-astro): update tests to match new output

* chore: prefer named functions

* fix: update template prompt

* fix: update typescript message

* chore: add explicit --skip-houston flag

* test(create-astro): skip flaky typescript test

Co-authored-by: Nate Moore <nate@astro.build>
2022-10-26 10:13:56 -05:00

50 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 = 5000;
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', '--dryrun'], { cwd: testDir });
return {
stdin,
stdout,
};
}