From b3b576fce51783615855b5cff079394ab0da24c3 Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Thu, 2 Jul 2026 00:53:09 +0000 Subject: [PATCH] [Tests] Replace filesystem mocks with real temp directories in store result tests Refactor `packages/store/src/cli/services/store/execute/result.test.ts` to use `inTemporaryDirectory` and `readFile` instead of mocking `@shopify/cli-kit/node/fs`. This ensures that file operations are verified against the actual filesystem and avoids brittle mocks. - Removed `vi.mock('@shopify/cli-kit/node/fs')` - Updated tests to use `inTemporaryDirectory` for file output verification - Verified that `renderSuccess` is called with the correct dynamic path --- .../cli/services/store/execute/result.test.ts | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/packages/store/src/cli/services/store/execute/result.test.ts b/packages/store/src/cli/services/store/execute/result.test.ts index efc60970e66..6368cbc28e4 100644 --- a/packages/store/src/cli/services/store/execute/result.test.ts +++ b/packages/store/src/cli/services/store/execute/result.test.ts @@ -1,10 +1,10 @@ import {writeOrOutputStoreExecuteResult} from './result.js' import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest' -import {writeFile} from '@shopify/cli-kit/node/fs' +import {inTemporaryDirectory, readFile} from '@shopify/cli-kit/node/fs' +import {joinPath} from '@shopify/cli-kit/node/path' import {renderSuccess} from '@shopify/cli-kit/node/ui' import {mockAndCaptureOutput} from '@shopify/cli-kit/node/testing/output' -vi.mock('@shopify/cli-kit/node/fs') vi.mock('@shopify/cli-kit/node/ui') function captureStandardStreams() { @@ -42,12 +42,19 @@ describe('writeOrOutputStoreExecuteResult', () => { }) test('writes results to a file when outputFile is provided', async () => { - await writeOrOutputStoreExecuteResult({data: {shop: {name: 'Test shop'}}}, '/tmp/results.json') - - expect(writeFile).toHaveBeenCalledWith('/tmp/results.json', expect.stringContaining('Test shop')) - expect(renderSuccess).toHaveBeenCalledWith({ - headline: 'Operation succeeded.', - body: 'Results written to /tmp/results.json', + await inTemporaryDirectory(async (tmpDir) => { + // Given + const resultsPath = joinPath(tmpDir, 'results.json') + + // When + await writeOrOutputStoreExecuteResult({data: {shop: {name: 'Test shop'}}}, resultsPath) + + // Then + await expect(readFile(resultsPath)).resolves.toContain('Test shop') + expect(renderSuccess).toHaveBeenCalledWith({ + headline: 'Operation succeeded.', + body: `Results written to ${resultsPath}`, + }) }) }) @@ -86,9 +93,16 @@ describe('writeOrOutputStoreExecuteResult', () => { }) test('suppresses success rendering when writing a file in json mode', async () => { - await writeOrOutputStoreExecuteResult({data: {shop: {name: 'Test shop'}}}, '/tmp/results.json', 'json') + await inTemporaryDirectory(async (tmpDir) => { + // Given + const resultsPath = joinPath(tmpDir, 'results.json') - expect(writeFile).toHaveBeenCalledWith('/tmp/results.json', expect.stringContaining('Test shop')) - expect(renderSuccess).not.toHaveBeenCalled() + // When + await writeOrOutputStoreExecuteResult({data: {shop: {name: 'Test shop'}}}, resultsPath, 'json') + + // Then + await expect(readFile(resultsPath)).resolves.toContain('Test shop') + expect(renderSuccess).not.toHaveBeenCalled() + }) }) })