diff --git a/src/__tests__/cli.test.ts b/src/__tests__/cli.test.ts new file mode 100644 index 0000000..65dbc3c --- /dev/null +++ b/src/__tests__/cli.test.ts @@ -0,0 +1,24 @@ +import { test, vi, expect, beforeEach } from 'vitest'; +import packageJson from '../../package.json'; + +beforeEach(() => { + vi.resetModules(); +}); + +test('outputs version with --version flag', async () => { + const writeSpy = vi.spyOn(process.stdout, 'write').mockReturnValue(true); + const exitSpy = vi + .spyOn(process, 'exit') + .mockImplementation(() => undefined as never); + + process.argv = ['node', 'belt', '--version']; + const { default: runCli } = await import('../cli'); + runCli(); + + expect(writeSpy).toHaveBeenCalledWith( + expect.stringContaining(packageJson.version), + ); + + writeSpy.mockRestore(); + exitSpy.mockRestore(); +}); diff --git a/src/cli.ts b/src/cli.ts index 09daa43..3f8b595 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,6 +1,7 @@ import { program } from 'commander'; import buildAction from './util/buildAction'; import printWelcome from './util/print/printWelcome'; +import packageJson from '../package.json'; export default function runCli() { program @@ -8,6 +9,7 @@ export default function runCli() { .description( 'Perform React Native and Expo setup and redundant tasks without your pants falling down!', ) + .version(packageJson.version) .showHelpAfterError(); program @@ -62,6 +64,6 @@ export default function runCli() { ) .action(buildAction(import('./commands/notifications'))); - printWelcome(); program.parse(); + printWelcome(); } diff --git a/src/util/print/__tests__/printWelcome.test.ts b/src/util/print/__tests__/printWelcome.test.ts index 55b7f19..0aff54a 100644 --- a/src/util/print/__tests__/printWelcome.test.ts +++ b/src/util/print/__tests__/printWelcome.test.ts @@ -1,8 +1,15 @@ -import { test, vi } from 'vitest'; +import { test, vi, expect } from 'vitest'; +import chalk from 'chalk'; import printWelcome from '../printWelcome'; +import print from '../../print'; vi.mock('../../print', () => ({ default: vi.fn() })); -test('doesnt error', () => { +test('prints welcome message', () => { printWelcome(); + expect(print).toHaveBeenNthCalledWith(1, chalk.bold('\n\n\tšŸ‘– Belt šŸ‘–\n')); + expect(print).toHaveBeenNthCalledWith( + 2, + 'Perform project setup and redundant tasks\n without your pants falling down!\n\n', + ); }); diff --git a/tsconfig.json b/tsconfig.json index c32294a..6a75fd1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,7 +10,8 @@ "outDir": "./build", "skipLibCheck": true, "types": ["node"], - "typeRoots": ["./src/types", "./node_modules/@types"] + "typeRoots": ["./src/types", "./node_modules/@types"], + "resolveJsonModule": true }, "ts-node": { "esm": true,