From ea1f7a3b101cecf17414ad56f0aacb4cc0bb2123 Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Sat, 6 Jun 2026 00:31:43 +0000 Subject: [PATCH 1/2] [Tests] Remove filesystem mocks in docs generate.test.ts Refactored `packages/cli/src/cli/commands/docs/generate.test.ts` to avoid mocking the filesystem, using `inTemporaryDirectory` and `readFile` instead. Updated `packages/cli/src/cli/commands/docs/generate.ts` to use a dynamic `docsPath()` function to support `cwd()` mocking in tests. Improved path construction using `joinPath`. --- .../src/cli/commands/docs/generate.test.ts | 51 +++++++++++++------ .../cli/src/cli/commands/docs/generate.ts | 10 ++-- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/packages/cli/src/cli/commands/docs/generate.test.ts b/packages/cli/src/cli/commands/docs/generate.test.ts index 7dab0a8047..1a6316f564 100644 --- a/packages/cli/src/cli/commands/docs/generate.test.ts +++ b/packages/cli/src/cli/commands/docs/generate.test.ts @@ -1,8 +1,15 @@ import {CommandData, CommandWithMarkdown, extractCommandData, writeCommandFlagInterface} from './generate.js' -import {writeFile} from '@shopify/cli-kit/node/fs' +import {inTemporaryDirectory, readFile} from '@shopify/cli-kit/node/fs' +import {cwd, joinPath} from '@shopify/cli-kit/node/path' import {describe, test, vi, expect} from 'vitest' -vi.mock('@shopify/cli-kit/node/fs') +vi.mock('@shopify/cli-kit/node/path', async (importOriginal) => { + const actual = await importOriginal() + return { + ...actual, + cwd: vi.fn(), + } +}) const testCommand: CommandWithMarkdown = { aliases: [], @@ -45,12 +52,18 @@ describe('extractCommandData', () => { }) describe('writeCommandFlagInterface', () => { - test('calls writeFile with the correct content', async () => { - await writeCommandFlagInterface(testCommand, commandData) + test('writes the correct content to the file', async () => { + await inTemporaryDirectory(async (tmpDir) => { + // Given + vi.mocked(cwd).mockReturnValue(tmpDir) + const filePath = joinPath(tmpDir, 'docs-shopify.dev/commands/interfaces/topic-test-command.interface.ts') + + // When + await writeCommandFlagInterface(testCommand, commandData) - expect(writeFile).toHaveBeenCalledWith( - expect.stringContaining('docs-shopify.dev/commands/interfaces/topic-test-command.interface.ts'), - `// This is an autogenerated file. Don't edit this file manually. + // Then + const content = await readFile(filePath) + expect(content).toBe(`// This is an autogenerated file. Don't edit this file manually. /** * The following flags are available for the \`topic test-command\` command: * @publicDocs @@ -68,16 +81,22 @@ export interface topictestcommand { */ '-a, --flag2'?: '' } -`, - ) +`) + }) }) - test('calls writeFile with the correct content for a command with no flags', async () => { - await writeCommandFlagInterface({...testCommand, flags: {}}, commandData) + test('writes the correct content for a command with no flags', async () => { + await inTemporaryDirectory(async (tmpDir) => { + // Given + vi.mocked(cwd).mockReturnValue(tmpDir) + const filePath = joinPath(tmpDir, 'docs-shopify.dev/commands/interfaces/topic-test-command.interface.ts') + + // When + await writeCommandFlagInterface({...testCommand, flags: {}}, commandData) - expect(writeFile).toHaveBeenCalledWith( - expect.stringContaining('docs-shopify.dev/commands/interfaces/topic-test-command.interface.ts'), - `// This is an autogenerated file. Don't edit this file manually. + // Then + const content = await readFile(filePath) + expect(content).toBe(`// This is an autogenerated file. Don't edit this file manually. /** * The following flags are available for the \`topic test-command\` command: * @publicDocs @@ -85,7 +104,7 @@ export interface topictestcommand { export interface topictestcommand { } -`, - ) +`) + }) }) }) diff --git a/packages/cli/src/cli/commands/docs/generate.ts b/packages/cli/src/cli/commands/docs/generate.ts index b90da1a97d..653d9cb586 100644 --- a/packages/cli/src/cli/commands/docs/generate.ts +++ b/packages/cli/src/cli/commands/docs/generate.ts @@ -3,7 +3,7 @@ import {Command as oclifCommand} from '@oclif/core' import {mkdir, rmdir, writeFile} from '@shopify/cli-kit/node/fs' import {cwd, joinPath} from '@shopify/cli-kit/node/path' -const docsPath = joinPath(cwd(), '/docs-shopify.dev/commands') +const docsPath = () => joinPath(cwd(), '/docs-shopify.dev/commands') export type CommandWithMarkdown = oclifCommand.Loadable & {descriptionWithMarkdown?: string} @@ -21,8 +21,8 @@ export default class DocsGenerate extends Command { const commands = this.config.commands as CommandWithMarkdown[] // Remove all files and recreate the folder. To make sure we don't leave any orphaned files. - await rmdir(docsPath) - await mkdir(docsPath) + await rmdir(docsPath()) + await mkdir(docsPath()) // Short by length to ensure that we first generate the interfaces for the parent topics to detect hidden ones. const sortedCommands = commands @@ -96,6 +96,6 @@ export interface ${interfaceName} { ${flagsDetails} } ` - await mkdir(`${docsPath}/interfaces`) - await writeFile(`${docsPath}/interfaces/${fileName}.interface.ts`, commandContent) + await mkdir(joinPath(docsPath(), 'interfaces')) + await writeFile(joinPath(docsPath(), 'interfaces', `${fileName}.interface.ts`), commandContent) } From 097c7f63cde2762e438537d38f9bb623b05c682c Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Sat, 6 Jun 2026 00:46:17 +0000 Subject: [PATCH 2/2] [Tests] Fix CI failures and remove fs mocks in docs generate.test.ts - Fixed flaky stream tests in `packages/cli-kit/src/public/node/fs.test.ts` by wrapping them in Promises. - Refactored `packages/cli/src/cli/commands/docs/generate.ts` to use a dynamic `docsPath()` function, enabling `cwd()` mocking. - Refactored `packages/cli/src/cli/commands/docs/generate.test.ts` to use `inTemporaryDirectory` and real filesystem operations instead of mocking `fs`. - Improved path construction in `generate.ts` using `joinPath`. --- packages/cli-kit/src/public/node/fs.test.ts | 34 +++++++++++++-------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/packages/cli-kit/src/public/node/fs.test.ts b/packages/cli-kit/src/public/node/fs.test.ts index d30612b5b9..2289789278 100644 --- a/packages/cli-kit/src/public/node/fs.test.ts +++ b/packages/cli-kit/src/public/node/fs.test.ts @@ -254,12 +254,17 @@ describe('createFileReadStream', () => { await touchFile(filePath) await appendFile(filePath, content) const stream = createFileReadStream(filePath) - let data = '' - stream.on('data', (chunk) => { - data += chunk as string - }) - stream.on('end', () => { - expect(data).toBe(content) + + await new Promise((resolve, reject) => { + let data = '' + stream.on('data', (chunk) => { + data += chunk as string + }) + stream.on('end', () => { + expect(data).toBe(content) + resolve() + }) + stream.on('error', reject) }) }) }) @@ -272,12 +277,17 @@ describe('createFileReadStream', () => { await appendFile(filePath, content) const stream = createFileReadStream(filePath, {start: 1, end: 7}) - let data = '' - stream.on('data', (chunk) => { - data += chunk as string - }) - stream.on('end', () => { - expect(data).toBe('est-con') + + await new Promise((resolve, reject) => { + let data = '' + stream.on('data', (chunk) => { + data += chunk as string + }) + stream.on('end', () => { + expect(data).toBe('est-con') + resolve() + }) + stream.on('error', reject) }) }) })