diff options
Diffstat (limited to 'packages/create-astro/test/directory-step.test.js')
-rw-r--r-- | packages/create-astro/test/directory-step.test.js | 50 |
1 files changed, 8 insertions, 42 deletions
diff --git a/packages/create-astro/test/directory-step.test.js b/packages/create-astro/test/directory-step.test.js index 122bd3f8f..02ddc672a 100644 --- a/packages/create-astro/test/directory-step.test.js +++ b/packages/create-astro/test/directory-step.test.js @@ -1,54 +1,20 @@ -import { execa } from 'execa'; -import { fileURLToPath } from 'url'; -import { dirname, resolve } from 'path'; +import { resolve } from 'path'; import { promises, existsSync } from 'fs'; +import { PROMPT_MESSAGES, testDir, setup, promiseWithTimeout, timeout } from './utils.js'; -const __filename = fileURLToPath(import.meta.url); -const __dirname = dirname(__filename); - -const createAstroError = new Error( - 'Timed out waiting for create-astro to respond with expected output.' -); -const timeout = 5000; - -const instructions = { - directory: 'Where would you like to create your app?', - template: 'Which app template would you like to use?', -}; const inputs = { nonEmptyDir: './fixtures/select-directory/nonempty-dir', emptyDir: './fixtures/select-directory/empty-dir', nonexistentDir: './fixtures/select-directory/banana-dir', }; -function promiseWithTimeout(testFn) { - return new Promise((resolve, reject) => { - const timeoutEvent = setTimeout(() => { - reject(createAstroError); - }, timeout); - function resolver() { - clearTimeout(timeoutEvent); - resolve(); - } - testFn(resolver); - }); -} - -function setup(args = []) { - const { stdout, stdin } = execa('../create-astro.mjs', args, { cwd: __dirname }); - return { - stdin, - stdout, - }; -} - describe('[create-astro] select directory', function () { this.timeout(timeout); it('should prompt for directory when none is provided', function () { return promiseWithTimeout((resolve) => { const { stdout } = setup(); stdout.on('data', (chunk) => { - if (chunk.includes(instructions.directory)) { + if (chunk.includes(PROMPT_MESSAGES.directory)) { resolve(); } }); @@ -58,21 +24,21 @@ describe('[create-astro] select directory', function () { return promiseWithTimeout((resolve) => { const { stdout } = setup([inputs.nonEmptyDir]); stdout.on('data', (chunk) => { - if (chunk.includes(instructions.directory)) { + if (chunk.includes(PROMPT_MESSAGES.directory)) { resolve(); } }); }); }); it('should proceed on an empty directory', async function () { - const resolvedEmptyDirPath = resolve(__dirname, inputs.emptyDir); + const resolvedEmptyDirPath = resolve(testDir, inputs.emptyDir); if (!existsSync(resolvedEmptyDirPath)) { await promises.mkdir(resolvedEmptyDirPath); } return promiseWithTimeout((resolve) => { const { stdout } = setup([inputs.emptyDir]); stdout.on('data', (chunk) => { - if (chunk.includes(instructions.template)) { + if (chunk.includes(PROMPT_MESSAGES.template)) { resolve(); } }); @@ -82,7 +48,7 @@ describe('[create-astro] select directory', function () { return promiseWithTimeout((resolve) => { const { stdout } = setup([inputs.nonexistentDir]); stdout.on('data', (chunk) => { - if (chunk.includes(instructions.template)) { + if (chunk.includes(PROMPT_MESSAGES.template)) { resolve(); } }); @@ -95,7 +61,7 @@ describe('[create-astro] select directory', function () { if (chunk.includes('Please clear contents or choose a different path.')) { resolve(); } - if (chunk.includes(instructions.directory)) { + if (chunk.includes(PROMPT_MESSAGES.directory)) { stdin.write(`${inputs.nonEmptyDir}\x0D`); } }); |