diff --git a/packages/theme/src/cli/utilities/asset-ignore.test.ts b/packages/theme/src/cli/utilities/asset-ignore.test.ts index c3940e28001..bb6a1d10da3 100644 --- a/packages/theme/src/cli/utilities/asset-ignore.test.ts +++ b/packages/theme/src/cli/utilities/asset-ignore.test.ts @@ -1,18 +1,8 @@ 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 {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 +21,21 @@ 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 + // No file created + + // When/Then + await expect(getPatternsFromShopifyIgnore(tmpDir)).resolves.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 the patterns when the .shopifyignore file exists', async () => { + await inTemporaryDirectory(async (tmpDir) => { + // Given + const content = ` # assets/basic.css assets/complex.css assets/*.png @@ -60,18 +43,19 @@ 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', - ]) + ` + await writeFile(`${tmpDir}/.shopifyignore`, content) - expect(readFileFn).toHaveBeenLastCalledWith('tmp/.shopifyignore', {encoding: 'utf8'}) + // When/Then + await expect(getPatternsFromShopifyIgnore(tmpDir)).resolves.toEqual([ + 'assets/complex.css', + 'assets/*.png', + 'sections/*', + 'templates/*', + 'config/*_data.json', + '.*settings_schema.json', + ]) + }) }) })