From 356f265173f437124b2330f8d9c214da094981e9 Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Sat, 11 Jul 2026 00:32:18 +0000 Subject: [PATCH] [Tests] Add unit tests for global-context.ts - Implement unit tests for getCurrentCommandId and setCurrentCommandId. - Add and export _resetGlobalContext for test isolation. --- .../src/public/node/global-context.test.ts | 28 +++++++++++++++++++ .../cli-kit/src/public/node/global-context.ts | 8 ++++++ 2 files changed, 36 insertions(+) create mode 100644 packages/cli-kit/src/public/node/global-context.test.ts diff --git a/packages/cli-kit/src/public/node/global-context.test.ts b/packages/cli-kit/src/public/node/global-context.test.ts new file mode 100644 index 00000000000..0b9720c1d92 --- /dev/null +++ b/packages/cli-kit/src/public/node/global-context.test.ts @@ -0,0 +1,28 @@ +import {getCurrentCommandId, setCurrentCommandId, _resetGlobalContext} from './global-context.js' +import {describe, expect, test, beforeEach} from 'vitest' + +describe('global-context', () => { + beforeEach(() => { + _resetGlobalContext() + }) + + test('getCurrentCommandId returns an empty string by default', () => { + // When + const got = getCurrentCommandId() + + // Then + expect(got).toBe('') + }) + + test('setCurrentCommandId updates the current command ID', () => { + // Given + const commandId = 'my-command' + + // When + setCurrentCommandId(commandId) + const got = getCurrentCommandId() + + // Then + expect(got).toBe(commandId) + }) +}) diff --git a/packages/cli-kit/src/public/node/global-context.ts b/packages/cli-kit/src/public/node/global-context.ts index b21aa1c48cd..6211dc4b154 100644 --- a/packages/cli-kit/src/public/node/global-context.ts +++ b/packages/cli-kit/src/public/node/global-context.ts @@ -31,3 +31,11 @@ export function getCurrentCommandId(): string { export function setCurrentCommandId(commandId: string): void { getGlobalContext().currentCommandId = commandId } + +/** + * Reset the global context. + * Useful for tests. + */ +export function _resetGlobalContext(): void { + _globalContext = undefined +}