From dd4969626aaf3f2f16d3e1cbeb461513e2df6401 Mon Sep 17 00:00:00 2001 From: Red-Asuka Date: Fri, 15 Nov 2024 16:06:30 +0800 Subject: [PATCH] feat(cli): add global command mock for tests and enhance filterOptions to use dynamic source --- cli/src/__tests__/utils/options.test.ts | 8 +++++++- cli/src/utils/options.ts | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/cli/src/__tests__/utils/options.test.ts b/cli/src/__tests__/utils/options.test.ts index 88175f4a7..f980c64c9 100644 --- a/cli/src/__tests__/utils/options.test.ts +++ b/cli/src/__tests__/utils/options.test.ts @@ -3,12 +3,18 @@ import { handleSaveOptions, handleLoadOptions } from '../../utils/options' import { existsSync, unlinkSync } from 'fs' import { join } from 'path' -import { expect, afterAll, describe, it } from '@jest/globals' +import { expect, afterAll, describe, it, jest, beforeAll } from '@jest/globals' const testFilePath = join(__dirname, 'test-options.json') const defaultPath = join(process.cwd(), 'mqttx-cli-options.json') describe('options', () => { + beforeAll(() => { + global.command = { + getOptionValueSource: jest.fn().mockReturnValue('cli'), + } as any + }) + afterAll(() => { if (existsSync(testFilePath)) { unlinkSync(testFilePath) diff --git a/cli/src/utils/options.ts b/cli/src/utils/options.ts index f85c35eb5..996216975 100644 --- a/cli/src/utils/options.ts +++ b/cli/src/utils/options.ts @@ -35,7 +35,7 @@ const removeUselessOptions = ( */ const filterOptions = (source: OptionValueSource, opts: T): Partial => { return Object.fromEntries( - Object.entries(opts).filter(([key]) => globalThis.command.getOptionValueSource(key) === 'cli'), + Object.entries(opts).filter(([key]) => globalThis.command.getOptionValueSource(key) === source), ) as Partial }