1
0
mirror of synced 2025-11-08 12:57:47 +00:00

test(CLI): cover all the things

This commit is contained in:
Jason Dreyzehner
2018-03-10 16:37:42 -05:00
parent 18e71a16aa
commit 1e2c5930f0
2 changed files with 55 additions and 7 deletions

View File

@@ -13,6 +13,7 @@ import {
install,
Placeholders
} from '../tasks';
import { completeSpinner } from '../typescript-starter';
test('errors if outdated', async t => {
nock.disableNetConnect();
@@ -119,6 +120,19 @@ test('getUserInfo: suppresses errors and returns empty strings', async t => {
});
});
test('getUserInfo: returns results properly', async t => {
const mock = ((async () => {
return {
stdout: 'result'
};
}) as any) as ExecaStatic;
const result = await getUserInfo(mock)();
t.deepEqual(result, {
gitEmail: 'result',
gitName: 'result'
});
});
test('initialCommit: throws generated errors', async t => {
const error = await t.throws(
initialCommit(mockErr(1))('deadbeef', 'fail', 'name', 'bitjson@github.com')
@@ -126,10 +140,21 @@ test('initialCommit: throws generated errors', async t => {
t.is(error.code, 1);
});
test('initialCommit: attempts to commit', async t => {
// tslint:disable-next-line:no-let
t.plan(4);
const mock = ((async () => {
t.pass();
}) as any) as ExecaStatic;
t.true(
await initialCommit(mock)('commit', 'dir', 'name', 'valid@example.com')
);
});
test("initialCommit: don't attempt to commit if user.name/email is not set", async t => {
// tslint:disable-next-line:no-let
let calls = 0;
const errorIf3 = ((() => {
const errorIf3 = ((async () => {
calls++;
calls === 1 ? t.pass() : calls === 2 ? t.pass() : t.fail();
}) as any) as ExecaStatic;
@@ -144,7 +169,7 @@ test("initialCommit: don't attempt to commit if user.name/email is not set", asy
});
test('install: uses the correct runner', async t => {
const mock = (((runner: Runner) => {
const mock = ((async (runner: Runner) => {
runner === Runner.Yarn ? t.pass() : t.fail();
}) as any) as ExecaStatic;
await install(mock)(true, Runner.Yarn, 'pass');
@@ -154,3 +179,15 @@ test('install: throws pretty error on failure', async t => {
const error = await t.throws(install(mockErr())(true, Runner.Npm, 'fail'));
t.is(error.message, "Installation failed. You'll need to install manually.");
});
test('completeSpinner: resolves spinners properly', async t => {
t.plan(2);
const never = () => {
t.fail();
};
const check = (confirm?: string) => (result?: string) => {
confirm ? t.is(confirm, result) : t.pass();
};
completeSpinner({ succeed: check(), fail: never }, true);
completeSpinner({ succeed: never, fail: check('message') }, false, 'message');
});

View File

@@ -144,11 +144,22 @@ export async function typescriptStarter(
await tasks.install(install, runner, projectPath);
const spinner7 = ora(`Initializing git repository`).start();
(await tasks.initialCommit(commitHash, projectPath, gitName, gitEmail))
? spinner7.succeed()
: spinner7.fail(
"Git config user.name and user.email are not configured. You'll need to `git commit` yourself."
);
completeSpinner(
spinner7,
await tasks.initialCommit(commitHash, projectPath, gitName, gitEmail),
"Git config user.name and user.email are not configured. You'll need to `git commit` yourself."
);
console.log(`\n${chalk.blue.bold(`Created ${name} 🎉`)}\n`);
}
export function completeSpinner(
spinner: {
readonly succeed: (text?: string) => any;
readonly fail: (text?: string) => any;
},
success: boolean,
message?: string
): void {
success ? spinner.succeed() : spinner.fail(message);
}