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 b38b224d66..a99f2ea4b0 100644 --- a/packages/store/src/cli/services/store/execute/result.test.ts +++ b/packages/store/src/cli/services/store/execute/result.test.ts @@ -1,92 +1,62 @@ import {writeOrOutputStoreExecuteResult} from './result.js' import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest' -import {writeFile} from '@shopify/cli-kit/node/fs' +import {readFile, inTemporaryDirectory} 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' +import {outputResult} from '@shopify/cli-kit/node/output' -vi.mock('@shopify/cli-kit/node/fs') vi.mock('@shopify/cli-kit/node/ui') - -function captureStandardStreams() { - const stdout: string[] = [] - const stderr: string[] = [] - - const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(((chunk: string | Uint8Array) => { - stdout.push(typeof chunk === 'string' ? chunk : Buffer.from(chunk).toString('utf8')) - return true - }) as typeof process.stdout.write) - const stderrSpy = vi.spyOn(process.stderr, 'write').mockImplementation(((chunk: string | Uint8Array) => { - stderr.push(typeof chunk === 'string' ? chunk : Buffer.from(chunk).toString('utf8')) - return true - }) as typeof process.stderr.write) - +vi.mock('@shopify/cli-kit/node/output', async () => { + const actual: any = await vi.importActual('@shopify/cli-kit/node/output') return { - stdout: () => stdout.join(''), - stderr: () => stderr.join(''), - restore: () => { - stdoutSpy.mockRestore() - stderrSpy.mockRestore() - }, + ...actual, + outputResult: vi.fn(), } -} +}) describe('writeOrOutputStoreExecuteResult', () => { - const originalUnitTestEnv = process.env.SHOPIFY_UNIT_TEST - - beforeEach(() => { - mockAndCaptureOutput().clear() - }) - - afterEach(() => { - process.env.SHOPIFY_UNIT_TEST = originalUnitTestEnv - }) - 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) => { + const outputFile = joinPath(tmpDir, 'results.json') + await writeOrOutputStoreExecuteResult({data: {shop: {name: 'Test shop'}}}, outputFile) + + const content = await readFile(outputFile) + expect(content).toContain('Test shop') + expect(renderSuccess).toHaveBeenCalledWith({ + headline: 'Operation succeeded.', + body: `Results written to ${outputFile}`, + }) }) }) test('writes results to stdout when no outputFile is provided', async () => { - const output = mockAndCaptureOutput() - await writeOrOutputStoreExecuteResult({data: {shop: {name: 'Test shop'}}}) expect(renderSuccess).toHaveBeenCalledWith({headline: 'Operation succeeded.'}) - expect(output.output()).toContain('Test shop') + expect(outputResult).toHaveBeenCalledWith(expect.stringContaining('Test shop')) }) test('suppresses success rendering in json mode', async () => { - const output = mockAndCaptureOutput() - await writeOrOutputStoreExecuteResult({data: {shop: {name: 'Test shop'}}}, undefined, 'json') expect(renderSuccess).not.toHaveBeenCalled() - expect(output.output()).toContain('Test shop') + expect(outputResult).toHaveBeenCalledWith(expect.stringContaining('Test shop')) }) - test('writes json results to stdout without writing to stderr', async () => { - process.env.SHOPIFY_UNIT_TEST = 'false' - const streams = captureStandardStreams() - - try { - await writeOrOutputStoreExecuteResult({data: {shop: {name: 'Test shop'}}}, undefined, 'json') - } finally { - streams.restore() - } + test('writes json results to stdout', async () => { + await writeOrOutputStoreExecuteResult({data: {shop: {name: 'Test shop'}}}, undefined, 'json') - expect(streams.stdout()).toContain('"name": "Test shop"') - expect(streams.stderr()).toBe('') + expect(outputResult).toHaveBeenCalledWith(expect.stringContaining('"name": "Test shop"')) }) 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) => { + const outputFile = joinPath(tmpDir, 'results.json') + await writeOrOutputStoreExecuteResult({data: {shop: {name: 'Test shop'}}}, outputFile, 'json') - expect(writeFile).toHaveBeenCalledWith('/tmp/results.json', expect.stringContaining('Test shop')) - expect(renderSuccess).not.toHaveBeenCalled() + const content = await readFile(outputFile) + expect(content).toContain('Test shop') + expect(renderSuccess).not.toHaveBeenCalled() + }) }) })