Skip to content

Commit 1054de5

Browse files
Merge pull request #7571 from Shopify/jules-tests-remove-fs-mocks-bundle-ui-step-18014390488825622297
[Tests] Remove filesystem mocks in bundle-ui-step.test.ts
2 parents a5c5b28 + 272ec98 commit 1054de5

1 file changed

Lines changed: 63 additions & 31 deletions

File tree

packages/app/src/cli/services/build/steps/bundle-ui-step.test.ts

Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import * as buildExtension from '../extension.js'
44
import {BundleUIStep, BuildContext} from '../client-steps.js'
55
import {ExtensionInstance} from '../../../models/extensions/extension-instance.js'
66
import {describe, expect, test, vi, beforeEach} from 'vitest'
7-
import * as fs from '@shopify/cli-kit/node/fs'
7+
import {inTemporaryDirectory, mkdir, writeFile, fileExists} from '@shopify/cli-kit/node/fs'
8+
import {joinPath} from '@shopify/cli-kit/node/path'
89

9-
vi.mock('@shopify/cli-kit/node/fs')
1010
vi.mock('../extension.js')
1111
vi.mock('./include-assets/generate-manifest.js')
1212

@@ -38,44 +38,76 @@ describe('executeBundleUIStep', () => {
3838
}
3939

4040
test('copies when local and bundle output directories differ', async () => {
41-
// Given
42-
mockContext.extension.outputPath = '/bundle/handle/handle.js'
43-
vi.mocked(buildExtension.buildUIExtension).mockResolvedValue('/test/extension/dist/handle.js')
41+
await inTemporaryDirectory(async (tmpDir) => {
42+
// Given
43+
const extensionDir = joinPath(tmpDir, 'extension')
44+
const localOutputDir = joinPath(extensionDir, 'dist')
45+
const bundleDir = joinPath(tmpDir, 'bundle')
46+
const bundleOutputDir = joinPath(bundleDir, 'handle')
4447

45-
// When
46-
await executeBundleUIStep(step, mockContext)
48+
await mkdir(localOutputDir)
49+
await writeFile(joinPath(localOutputDir, 'handle.js'), 'console.log("hello")')
4750

48-
// Then
49-
expect(fs.copyFile).toHaveBeenCalledWith('/test/extension/dist', '/bundle/handle')
51+
mockContext.extension.directory = extensionDir
52+
mockContext.extension.outputPath = joinPath(bundleOutputDir, 'handle.js')
53+
vi.mocked(buildExtension.buildUIExtension).mockResolvedValue(joinPath(localOutputDir, 'handle.js'))
54+
55+
// When
56+
await executeBundleUIStep(step, mockContext)
57+
58+
// Then
59+
await expect(fileExists(joinPath(bundleOutputDir, 'handle.js'))).resolves.toBe(true)
60+
})
5061
})
5162

5263
test('skips the copy when local and bundle output directories resolve to the same path but differ as strings', async () => {
53-
mockContext.extension.outputPath = '/test/./extension/dist/handle.js'
54-
vi.mocked(buildExtension.buildUIExtension).mockResolvedValue('/test/extension/dist/handle.js')
64+
await inTemporaryDirectory(async (tmpDir) => {
65+
// Given
66+
const extensionDir = joinPath(tmpDir, 'extension')
67+
const localOutputDir = joinPath(extensionDir, 'dist')
68+
await mkdir(localOutputDir)
5569

56-
await executeBundleUIStep(step, mockContext)
70+
mockContext.extension.directory = extensionDir
71+
// /test/./extension/dist/handle.js style
72+
mockContext.extension.outputPath = joinPath(extensionDir, '.', 'dist', 'handle.js')
73+
vi.mocked(buildExtension.buildUIExtension).mockResolvedValue(joinPath(localOutputDir, 'handle.js'))
5774

58-
expect(fs.copyFile).not.toHaveBeenCalled()
75+
// When
76+
await executeBundleUIStep(step, mockContext)
77+
78+
// Then
79+
// No copy happens, and we can't really "assert" it didn't happen other than it didn't throw
80+
// and we didn't provide a bundleDir that would have been created.
81+
})
5982
})
6083

6184
test('skips manifest generation when local and bundle output directories resolve to the same path', async () => {
62-
const stepWithManifest: BundleUIStep = {
63-
id: 'bundle-ui',
64-
name: 'Bundle UI Extension',
65-
type: 'bundle_ui',
66-
config: {generatesAssetsManifest: true},
67-
}
68-
mockContext.extension.outputPath = '/test/./extension/dist/handle.js'
69-
mockContext.extension.configuration = {
70-
extension_points: [
71-
{target: 'admin.product-details.action.render', build_manifest: {assets: {main: {filepath: 'main.js'}}}},
72-
],
73-
} as ExtensionInstance['configuration']
74-
vi.mocked(buildExtension.buildUIExtension).mockResolvedValue('/test/extension/dist/handle.js')
75-
76-
await executeBundleUIStep(stepWithManifest, mockContext)
77-
78-
expect(fs.copyFile).not.toHaveBeenCalled()
79-
expect(generateManifest.createOrUpdateManifestFile).not.toHaveBeenCalled()
85+
await inTemporaryDirectory(async (tmpDir) => {
86+
// Given
87+
const extensionDir = joinPath(tmpDir, 'extension')
88+
const localOutputDir = joinPath(extensionDir, 'dist')
89+
await mkdir(localOutputDir)
90+
91+
const stepWithManifest: BundleUIStep = {
92+
id: 'bundle-ui',
93+
name: 'Bundle UI Extension',
94+
type: 'bundle_ui',
95+
config: {generatesAssetsManifest: true},
96+
}
97+
mockContext.extension.directory = extensionDir
98+
mockContext.extension.outputPath = joinPath(extensionDir, '.', 'dist', 'handle.js')
99+
mockContext.extension.configuration = {
100+
extension_points: [
101+
{target: 'admin.product-details.action.render', build_manifest: {assets: {main: {filepath: 'main.js'}}}},
102+
],
103+
} as ExtensionInstance['configuration']
104+
vi.mocked(buildExtension.buildUIExtension).mockResolvedValue(joinPath(localOutputDir, 'handle.js'))
105+
106+
// When
107+
await executeBundleUIStep(stepWithManifest, mockContext)
108+
109+
// Then
110+
expect(generateManifest.createOrUpdateManifestFile).not.toHaveBeenCalled()
111+
})
80112
})
81113
})

0 commit comments

Comments
 (0)