Skip to content

Commit

Permalink
feat(cli): add global command mock for tests and enhance filterOption…
Browse files Browse the repository at this point in the history
…s to use dynamic source
  • Loading branch information
Red-Asuka committed Nov 15, 2024
1 parent 9b3e9cf commit 912e553
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion cli/src/__tests__/utils/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cli/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const removeUselessOptions = (
*/
const filterOptions = <T extends OptionsType>(source: OptionValueSource, opts: T): Partial<T> => {
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<T>
}

Expand Down

0 comments on commit 912e553

Please sign in to comment.