From 04c74a4c63c5b959d5067145a2c416212eadfc1d Mon Sep 17 00:00:00 2001 From: "Angel M. Adames" Date: Mon, 26 Feb 2024 18:57:38 -0400 Subject: [PATCH] chore: Fix test for clean command (parseAsync) --- src/commands/clean/index.ts | 4 +--- test/commands/clean.test.ts | 19 +++++++++---------- test/helpers.ts | 10 ++++++++++ test/utils/object.test.ts | 9 ++++++--- 4 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 test/helpers.ts diff --git a/src/commands/clean/index.ts b/src/commands/clean/index.ts index feaf9ab..9e1527b 100644 --- a/src/commands/clean/index.ts +++ b/src/commands/clean/index.ts @@ -17,7 +17,6 @@ export const cleanCommand = () => { 'by DEMS. Resets your local development environment.', ) .addCommand(cleanDepsCommand()) - .addOption(sharedOptions.verbose().default(true)) .addOption(sharedOptions.force().default(false)) .addOption(sharedOptions.reposRoot().default(config.paths.repos_root)) .option('-e, --env-file ', '.env file', config.paths.repos_root) @@ -30,11 +29,10 @@ export const cleanCommand = () => { await deletePath({ path: options.reposRoot, force: options.force, - verbose: options.verbose, }); await deletePath({ path: options.envFile, - force: options.verbose, + force: options.force, }); log.success('Clean completed for current project.'); diff --git a/test/commands/clean.test.ts b/test/commands/clean.test.ts index 50f0963..f020066 100644 --- a/test/commands/clean.test.ts +++ b/test/commands/clean.test.ts @@ -1,29 +1,28 @@ -import { describe, expect, test } from 'bun:test'; -import { $ } from 'bun'; -import { projectConfig } from '../../src/config/project'; +import { describe, expect, test, beforeEach } from 'bun:test'; import { createFile, createPath, isDirectory, isFile, } from '../../src/utils/file-system'; +import { omitConsoleLogs } from '../helpers'; +import { cleanCommand } from '../../src/commands/clean'; -const REPOS_ROOT = './test/repos'; const ENV_FILE = './test/.env'; +const REPOS_ROOT = './test/repos'; -const prepare = () => { +beforeEach(() => { + omitConsoleLogs(); createPath({ path: REPOS_ROOT }); createFile({ file: ENV_FILE, content: 'KEY=VALUE' }); -}; +}); describe("Command: 'clean'", () => { - const config = projectConfig(); - prepare(); - test('Cleans directories using --force', async () => { expect(isDirectory(REPOS_ROOT)).toBeTrue(); expect(isFile(ENV_FILE)).toBeTrue(); - await $`./cli.ts clean -o ${REPOS_ROOT} -e ${ENV_FILE} --force --verbose false`; + const args = [...process.argv, '-o', REPOS_ROOT, '-e', ENV_FILE, '--force']; + await cleanCommand().parseAsync(args); expect(isDirectory(REPOS_ROOT)).toBeFalse(); expect(isFile(ENV_FILE)).toBeFalse(); }); diff --git a/test/helpers.ts b/test/helpers.ts new file mode 100644 index 0000000..8d24e81 --- /dev/null +++ b/test/helpers.ts @@ -0,0 +1,10 @@ +import { jest } from 'bun:test'; +import log from '../src/utils/log'; + +export const omitConsoleLogs = () => { + console.log = jest.fn(); + log.info = jest.fn(); + log.success = jest.fn(); + log.warning = jest.fn(); + log.error = jest.fn(); +}; diff --git a/test/utils/object.test.ts b/test/utils/object.test.ts index cb2bc94..a30c011 100644 --- a/test/utils/object.test.ts +++ b/test/utils/object.test.ts @@ -6,22 +6,25 @@ import { replaceKeyValue, replaceKeysInFile, } from '../../src/utils/object'; +import { omitConsoleLogs } from '../helpers'; + +omitConsoleLogs(); describe('Utils: object', () => { const testFile = '.env.test'; const testFileContent = 'KEY1=VALUE1\nKEY2=VALUE2'; test('Replaces a key value in a file', () => { - createFile({ file: testFile, content: testFileContent, verbose: false }); + createFile({ file: testFile, content: testFileContent }); expect(isFile(testFile)).toBeTrue(); - replaceKeyValue(testFile, 'KEY1', 'VALUE10', false); + replaceKeyValue(testFile, 'KEY1', 'VALUE10'); const replacedContent = fs.readFileSync(testFile, 'utf8'); expect(replacedContent).toContain('VALUE10'); }); test("Replaces various keys' values in a file", () => { - createFile({ file: testFile, content: testFileContent, verbose: false }); + createFile({ file: testFile, content: testFileContent }); expect(isFile(testFile)).toBeTrue(); replaceKeysInFile(