From cd9904c313a7cdcd1e8355514b2c16acceb377a1 Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Fri, 3 Jul 2026 00:28:06 +0000 Subject: [PATCH] [Tests] Replace filesystem mocks in asset-ignore.test.ts Refactor `packages/theme/src/cli/utilities/asset-ignore.test.ts` to replace manual mocks of `@shopify/cli-kit/node/fs` with real operations using `inTemporaryDirectory` and `writeFile`. This adheres to the automated testing protocol and improves test reliability. - Replace `vi.mock('@shopify/cli-kit/node/fs', ...)` with real imports. - Use `inTemporaryDirectory` for `getPatternsFromShopifyIgnore` tests. - Create real `.shopifyignore` files using `writeFile`. - Fix a duplicate test case description. --- .../src/cli/utilities/asset-ignore.test.ts | 73 ++++++++----------- 1 file changed, 32 insertions(+), 41 deletions(-) diff --git a/packages/theme/src/cli/utilities/asset-ignore.test.ts b/packages/theme/src/cli/utilities/asset-ignore.test.ts index c3940e28001..83f811e6930 100644 --- a/packages/theme/src/cli/utilities/asset-ignore.test.ts +++ b/packages/theme/src/cli/utilities/asset-ignore.test.ts @@ -1,18 +1,9 @@ import {applyIgnoreFilters, getPatternsFromShopifyIgnore} from './asset-ignore.js' -import {ReadOptions, fileExists, readFile} from '@shopify/cli-kit/node/fs' -import {test, describe, beforeEach, vi, expect} from 'vitest' +import {inTemporaryDirectory, writeFile} from '@shopify/cli-kit/node/fs' +import {joinPath} from '@shopify/cli-kit/node/path' +import {test, describe, vi, expect} from 'vitest' import {renderWarning} from '@shopify/cli-kit/node/ui' -vi.mock('@shopify/cli-kit/node/fs', async () => { - const originalFs: any = await vi.importActual('@shopify/cli-kit/node/fs') - return { - ...originalFs, - matchGlob: originalFs.matchGlob, - readFile: vi.fn(), - fileExists: vi.fn(), - } -}) - vi.mock('@shopify/cli-kit/node/ui', () => ({ renderWarning: vi.fn(), })) @@ -31,28 +22,24 @@ describe('asset-ignore', () => { {key: 'templates/customers/account.json', checksum: '7777777777777777777777777777777'}, ] - /** - * Explicitly defining the type of 'readFile' as it returns either - * 'Promise' or 'Promise', which are ambiguous to the - * TypeScript language server. - */ - const readFileFn = readFile as (path: string, options?: ReadOptions) => Promise - - beforeEach(() => { - vi.mocked(fileExists).mockResolvedValue(true) - vi.mocked(readFileFn).mockResolvedValue('') - }) - describe('getPatternsFromShopifyIgnore', () => { test('returns an empty array when the .shopifyignore file does not exist', async () => { - // Given - vi.mocked(fileExists).mockResolvedValue(false) - await expect(getPatternsFromShopifyIgnore('tmp')).resolves.toEqual([]) + await inTemporaryDirectory(async (tmpDir) => { + // Given/When + const patterns = await getPatternsFromShopifyIgnore(tmpDir) + + // Then + expect(patterns).toEqual([]) + }) }) - test('returns an empty array when the .shopifyignore file does not exist', async () => { - vi.mocked(fileExists).mockResolvedValue(true) - vi.mocked(readFileFn).mockResolvedValue(` + test('returns an array of patterns when the .shopifyignore file exists', async () => { + await inTemporaryDirectory(async (tmpDir) => { + // Given + const shopifyIgnorePath = joinPath(tmpDir, '.shopifyignore') + await writeFile( + shopifyIgnorePath, + ` # assets/basic.css assets/complex.css assets/*.png @@ -60,18 +47,22 @@ describe('asset-ignore', () => { templates/* config/*_data.json .*settings_schema.json - `) - - await expect(getPatternsFromShopifyIgnore('tmp')).resolves.toEqual([ - 'assets/complex.css', - 'assets/*.png', - 'sections/*', - 'templates/*', - 'config/*_data.json', - '.*settings_schema.json', - ]) + `, + ) + + // When + const patterns = await getPatternsFromShopifyIgnore(tmpDir) - expect(readFileFn).toHaveBeenLastCalledWith('tmp/.shopifyignore', {encoding: 'utf8'}) + // Then + expect(patterns).toEqual([ + 'assets/complex.css', + 'assets/*.png', + 'sections/*', + 'templates/*', + 'config/*_data.json', + '.*settings_schema.json', + ]) + }) }) })