diff --git a/packages/create-astro/test/directory-step.test.js b/packages/create-astro/test/directory-step.test.js index 26737ca94..15a0479c8 100644 --- a/packages/create-astro/test/directory-step.test.js +++ b/packages/create-astro/test/directory-step.test.js @@ -1,8 +1,6 @@ import path from 'path'; import { promises, existsSync } from 'fs'; -import { - PROMPT_MESSAGES, testDir, setup, promiseWithTimeout, timeout -} from './utils.js'; +import { PROMPT_MESSAGES, testDir, setup, promiseWithTimeout, timeout } from './utils.js'; const inputs = { nonEmptyDir: './fixtures/select-directory/nonempty-dir', diff --git a/packages/create-astro/test/typescript-step.test.js b/packages/create-astro/test/typescript-step.test.js index abec21646..57371fec1 100644 --- a/packages/create-astro/test/typescript-step.test.js +++ b/packages/create-astro/test/typescript-step.test.js @@ -2,9 +2,7 @@ import { expect } from 'chai'; import { deleteSync } from 'del'; import { existsSync, mkdirSync, readdirSync, readFileSync } from 'fs'; import path from 'path'; -import { - PROMPT_MESSAGES, testDir, setup, promiseWithTimeout, timeout -} from './utils.js'; +import { PROMPT_MESSAGES, testDir, setup, promiseWithTimeout, timeout } from './utils.js'; const inputs = { emptyDir: './fixtures/select-typescript/empty-dir', @@ -37,81 +35,107 @@ describe('[create-astro] select typescript', function () { afterEach(ensureEmptyDir); it('should prompt for typescript when none is provided', async function () { - return promiseWithTimeout((resolve, onStdout) => { - const { stdout } = setup([ - inputs.emptyDir, - '--template', 'minimal', - '--install', '0', - '--git', '0' - ]); - stdout.on('data', (chunk) => { - onStdout(chunk); - if (chunk.includes(PROMPT_MESSAGES.typescript)) { - resolve(); - } - }); - }, () => lastStdout); + return promiseWithTimeout( + (resolve, onStdout) => { + const { stdout } = setup([ + inputs.emptyDir, + '--template', + 'minimal', + '--install', + '0', + '--git', + '0', + ]); + stdout.on('data', (chunk) => { + onStdout(chunk); + if (chunk.includes(PROMPT_MESSAGES.typescript)) { + resolve(); + } + }); + }, + () => lastStdout + ); }); it('should not prompt for typescript when provided', async function () { - return promiseWithTimeout((resolve, onStdout) => { - const { stdout } = setup([ - inputs.emptyDir, - '--template', 'minimal', - '--install', '0', - '--git', '0', - '--typescript', 'base' - ]); - stdout.on('data', (chunk) => { - onStdout(chunk); - if (chunk.includes(PROMPT_MESSAGES.typescriptSucceed)) { - resolve(); - } - }); - }, () => lastStdout); + return promiseWithTimeout( + (resolve, onStdout) => { + const { stdout } = setup([ + inputs.emptyDir, + '--template', + 'minimal', + '--install', + '0', + '--git', + '0', + '--typescript', + 'base', + ]); + stdout.on('data', (chunk) => { + onStdout(chunk); + if (chunk.includes(PROMPT_MESSAGES.typescriptSucceed)) { + resolve(); + } + }); + }, + () => lastStdout + ); }); it('should use "strict" config when specified', async function () { - return promiseWithTimeout((resolve, onStdout) => { - let wrote = false; - const { stdout, stdin } = setup([ - inputs.emptyDir, - '--template', 'minimal', - '--install', '0', - '--git', '0' - ]); - stdout.on('data', (chunk) => { - onStdout(chunk); - if (!wrote && chunk.includes(PROMPT_MESSAGES.typescript)) { - stdin.write('\x1B\x5B\x42\x0D'); - wrote = true; - } - if (chunk.includes(PROMPT_MESSAGES.typescriptSucceed)) { - const tsConfigJson = getTsConfig(inputs.emptyDir); - expect(tsConfigJson).to.deep.equal({'extends': 'astro/tsconfigs/strict'}); - resolve(); - } - }); - }, () => lastStdout); + return promiseWithTimeout( + (resolve, onStdout) => { + let wrote = false; + const { stdout, stdin } = setup([ + inputs.emptyDir, + '--template', + 'minimal', + '--install', + '0', + '--git', + '0', + ]); + stdout.on('data', (chunk) => { + onStdout(chunk); + if (!wrote && chunk.includes(PROMPT_MESSAGES.typescript)) { + stdin.write('\x1B\x5B\x42\x0D'); + wrote = true; + } + if (chunk.includes(PROMPT_MESSAGES.typescriptSucceed)) { + const tsConfigJson = getTsConfig(inputs.emptyDir); + expect(tsConfigJson).to.deep.equal({ extends: 'astro/tsconfigs/strict' }); + resolve(); + } + }); + }, + () => lastStdout + ); }); it('should create tsconfig.json when missing', async function () { - return promiseWithTimeout((resolve, onStdout) => { - const { stdout } = setup([ - inputs.emptyDir, - '--template', 'cassidoo/shopify-react-astro', - '--install', '0', - '--git', '0', - '--typescript', 'base' - ]); - stdout.on('data', (chunk) => { - onStdout(chunk); - if (chunk.includes(PROMPT_MESSAGES.typescriptSucceed)) { - const tsConfigJson = getTsConfig(inputs.emptyDir); - expect(tsConfigJson).to.deep.equal({'extends': 'astro/tsconfigs/base'}); - resolve(); - } - }); - }, () => lastStdout); + return promiseWithTimeout( + (resolve, onStdout) => { + const { stdout } = setup([ + inputs.emptyDir, + '--template', + 'cassidoo/shopify-react-astro', + '--install', + '0', + '--git', + '0', + '--typescript', + 'base', + ]); + stdout.on('data', (chunk) => { + onStdout(chunk); + if (chunk.includes(PROMPT_MESSAGES.typescriptSucceed)) { + const tsConfigJson = getTsConfig(inputs.emptyDir); + expect(tsConfigJson).to.deep.equal({ extends: 'astro/tsconfigs/base' }); + resolve(); + } + }); + }, + () => lastStdout + ); }); }); diff --git a/packages/create-astro/test/utils.js b/packages/create-astro/test/utils.js index 9be444b5b..0a83a4f59 100644 --- a/packages/create-astro/test/utils.js +++ b/packages/create-astro/test/utils.js @@ -8,18 +8,17 @@ 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.'; + 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) { + function onStdout(chunk) { lastStdout = stripAnsi(chunk.toString()).trim() || lastStdout; } @@ -30,7 +29,7 @@ export function promiseWithTimeout(testFn) { clearTimeout(timeoutEvent); resolve(); } - + testFn(resolver, onStdout); }); } @@ -39,7 +38,7 @@ export const PROMPT_MESSAGES = { directory: 'Where would you like to create your new project?', template: 'Which template would you like to use?', typescript: 'How would you like to setup TypeScript?', - typescriptSucceed: 'Next steps' + typescriptSucceed: 'Next steps', }; export function setup(args = []) {