Skip to content

Commit 7660ce4

Browse files
authored
Merge pull request #7701 from Shopify/tests-remove-fs-mocks-dev-override-1740446454515-8455914361906458317
[Tests] Remove filesystem mocks in dev-override.test.ts
2 parents 078f031 + d04c2a7 commit 7660ce4

1 file changed

Lines changed: 173 additions & 155 deletions

File tree

packages/theme/src/cli/services/dev-override.test.ts

Lines changed: 173 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import {createThemePreview, updateThemePreview} from '../utilities/theme-preview
55
import {describe, expect, test, vi} from 'vitest'
66
import {renderSuccess} from '@shopify/cli-kit/node/ui'
77
import {collectedLogs, clearCollectedLogs} from '@shopify/cli-kit/node/output'
8-
import {fileExistsSync, readFile} from '@shopify/cli-kit/node/fs'
8+
import {inTemporaryDirectory, writeFile} from '@shopify/cli-kit/node/fs'
9+
import {joinPath} from '@shopify/cli-kit/node/path'
910

1011
vi.mock('../utilities/theme-environment/dev-server-session.js')
1112
vi.mock('../utilities/theme-previews/preview.js')
1213
vi.mock('./dev.js', () => ({openURLSafely: vi.fn()}))
1314
vi.mock('@shopify/cli-kit/node/ui')
14-
vi.mock('@shopify/cli-kit/node/fs')
1515

1616
const adminSession = {token: 'token', storeFqdn: 'store.myshopify.com'}
1717
const mockSession = {
@@ -25,187 +25,205 @@ const expectedPreviewId = 'abc123'
2525

2626
describe('devWithOverrideFile', () => {
2727
test('throws when override file does not exist', async () => {
28-
// Given
29-
vi.mocked(fileExistsSync).mockReturnValue(false)
30-
31-
// When/Then
32-
await expect(
33-
devWithOverrideFile({adminSession, overrideJson: '/missing.json', themeId: '123', open: false}),
34-
).rejects.toThrow('Override file not found: /missing.json')
28+
await inTemporaryDirectory(async (tmpDir) => {
29+
// Given
30+
const overrideJson = joinPath(tmpDir, 'missing.json')
31+
32+
// When/Then
33+
await expect(
34+
devWithOverrideFile({adminSession, overrideJson, themeId: '123', open: false}),
35+
).rejects.toThrow(`Override file not found: ${overrideJson}`)
36+
})
3537
})
3638

3739
test('creates a preview when no previewIdentifier is provided', async () => {
38-
// Given
39-
vi.mocked(fileExistsSync).mockReturnValue(true)
40-
vi.mocked(readFile).mockResolvedValue(Buffer.from(JSON.stringify({templates: {}})))
41-
vi.mocked(fetchDevServerSession).mockResolvedValue(mockSession)
42-
vi.mocked(createThemePreview).mockResolvedValue({url: expectedPreviewUrl, preview_identifier: expectedPreviewId})
43-
const expectedThemeId = '789'
44-
45-
// When
46-
await devWithOverrideFile({adminSession, overrideJson: '/overrides.json', themeId: expectedThemeId, open: false})
47-
48-
// Then
49-
expect(fetchDevServerSession).toHaveBeenCalledWith(expectedThemeId, adminSession, undefined)
50-
51-
// Then
52-
expect(createThemePreview).toHaveBeenCalledWith(
53-
expect.objectContaining({
54-
session: mockSession,
55-
themeId: expectedThemeId,
56-
}),
57-
)
58-
expect(updateThemePreview).not.toHaveBeenCalled()
59-
expect(renderSuccess).toHaveBeenCalledWith(
60-
expect.objectContaining({
61-
body: [
62-
{
63-
list: {
64-
title: 'Preview is ready',
65-
items: [{link: {url: expectedPreviewUrl}}, `Preview ID: ${expectedPreviewId}`],
40+
await inTemporaryDirectory(async (tmpDir) => {
41+
// Given
42+
const overrideJson = joinPath(tmpDir, 'overrides.json')
43+
await writeFile(overrideJson, JSON.stringify({templates: {}}))
44+
vi.mocked(fetchDevServerSession).mockResolvedValue(mockSession)
45+
vi.mocked(createThemePreview).mockResolvedValue({url: expectedPreviewUrl, preview_identifier: expectedPreviewId})
46+
const expectedThemeId = '789'
47+
48+
// When
49+
await devWithOverrideFile({adminSession, overrideJson, themeId: expectedThemeId, open: false})
50+
51+
// Then
52+
expect(fetchDevServerSession).toHaveBeenCalledWith(expectedThemeId, adminSession, undefined)
53+
54+
// Then
55+
expect(createThemePreview).toHaveBeenCalledWith(
56+
expect.objectContaining({
57+
session: mockSession,
58+
themeId: expectedThemeId,
59+
}),
60+
)
61+
expect(updateThemePreview).not.toHaveBeenCalled()
62+
expect(renderSuccess).toHaveBeenCalledWith(
63+
expect.objectContaining({
64+
body: [
65+
{
66+
list: {
67+
title: 'Preview is ready',
68+
items: [{link: {url: expectedPreviewUrl}}, `Preview ID: ${expectedPreviewId}`],
69+
},
6670
},
67-
},
68-
],
69-
}),
70-
)
71+
],
72+
}),
73+
)
74+
})
7175
})
7276

7377
test('updates a preview when previewIdentifier is provided', async () => {
74-
// Given
75-
vi.mocked(fileExistsSync).mockReturnValue(true)
76-
vi.mocked(readFile).mockResolvedValue(Buffer.from(JSON.stringify({templates: {}})))
77-
vi.mocked(fetchDevServerSession).mockResolvedValue(mockSession)
78-
vi.mocked(updateThemePreview).mockResolvedValue({url: expectedPreviewUrl, preview_identifier: expectedPreviewId})
79-
const expectedThemeId = '789'
80-
81-
// When
82-
await devWithOverrideFile({
83-
adminSession,
84-
overrideJson: '/overrides.json',
85-
themeId: expectedThemeId,
86-
previewIdentifier: expectedPreviewId,
87-
open: false,
88-
json: false,
89-
})
90-
91-
// Then
92-
expect(updateThemePreview).toHaveBeenCalledWith(
93-
expect.objectContaining({
94-
session: mockSession,
78+
await inTemporaryDirectory(async (tmpDir) => {
79+
// Given
80+
const overrideJson = joinPath(tmpDir, 'overrides.json')
81+
await writeFile(overrideJson, JSON.stringify({templates: {}}))
82+
vi.mocked(fetchDevServerSession).mockResolvedValue(mockSession)
83+
vi.mocked(updateThemePreview).mockResolvedValue({url: expectedPreviewUrl, preview_identifier: expectedPreviewId})
84+
const expectedThemeId = '789'
85+
86+
// When
87+
await devWithOverrideFile({
88+
adminSession,
89+
overrideJson,
9590
themeId: expectedThemeId,
9691
previewIdentifier: expectedPreviewId,
97-
}),
98-
)
99-
expect(createThemePreview).not.toHaveBeenCalled()
100-
expect(renderSuccess).toHaveBeenCalledWith(
101-
expect.objectContaining({
102-
body: [
103-
{
104-
list: {
105-
title: 'Preview updated',
106-
items: [{link: {url: expectedPreviewUrl}}, `Preview ID: ${expectedPreviewId}`],
92+
open: false,
93+
json: false,
94+
})
95+
96+
// Then
97+
expect(updateThemePreview).toHaveBeenCalledWith(
98+
expect.objectContaining({
99+
session: mockSession,
100+
themeId: expectedThemeId,
101+
previewIdentifier: expectedPreviewId,
102+
}),
103+
)
104+
expect(createThemePreview).not.toHaveBeenCalled()
105+
expect(renderSuccess).toHaveBeenCalledWith(
106+
expect.objectContaining({
107+
body: [
108+
{
109+
list: {
110+
title: 'Preview updated',
111+
items: [{link: {url: expectedPreviewUrl}}, `Preview ID: ${expectedPreviewId}`],
112+
},
107113
},
108-
},
109-
],
110-
}),
111-
)
114+
],
115+
}),
116+
)
117+
})
112118
})
113119

114120
test('throws when override file contains invalid JSON', async () => {
115-
// Given
116-
vi.mocked(fileExistsSync).mockReturnValue(true)
117-
vi.mocked(readFile).mockResolvedValue(Buffer.from('not valid json'))
118-
119-
// When/Then
120-
const error = await devWithOverrideFile({
121-
adminSession,
122-
overrideJson: '/bad.json',
123-
themeId: '123',
124-
open: false,
125-
json: false,
126-
}).catch((err) => err)
127-
expect(error.message).toBe('Failed to parse override file: /bad.json')
128-
expect(error.tryMessage).toMatch(/not valid json/i)
121+
await inTemporaryDirectory(async (tmpDir) => {
122+
// Given
123+
const overrideJson = joinPath(tmpDir, 'bad.json')
124+
await writeFile(overrideJson, 'not valid json')
125+
126+
// When/Then
127+
const error = await devWithOverrideFile({
128+
adminSession,
129+
overrideJson,
130+
themeId: '123',
131+
open: false,
132+
json: false,
133+
}).catch((err) => err)
134+
expect(error.message).toBe(`Failed to parse override file: ${overrideJson}`)
135+
expect(error.tryMessage).toMatch(/not valid json/i)
136+
})
129137
})
130138

131139
test('opens the preview URL when open is true', async () => {
132-
// Given
133-
vi.mocked(fileExistsSync).mockReturnValue(true)
134-
vi.mocked(readFile).mockResolvedValue(Buffer.from(JSON.stringify({templates: {}})))
135-
vi.mocked(fetchDevServerSession).mockResolvedValue(mockSession)
136-
vi.mocked(createThemePreview).mockResolvedValue({url: expectedPreviewUrl, preview_identifier: expectedPreviewId})
137-
138-
// When
139-
await devWithOverrideFile({adminSession, overrideJson: '/overrides.json', themeId: '789', open: true})
140-
141-
// Then
142-
expect(openURLSafely).toHaveBeenCalledWith(expectedPreviewUrl, 'theme preview')
140+
await inTemporaryDirectory(async (tmpDir) => {
141+
// Given
142+
const overrideJson = joinPath(tmpDir, 'overrides.json')
143+
await writeFile(overrideJson, JSON.stringify({templates: {}}))
144+
vi.mocked(fetchDevServerSession).mockResolvedValue(mockSession)
145+
vi.mocked(createThemePreview).mockResolvedValue({url: expectedPreviewUrl, preview_identifier: expectedPreviewId})
146+
147+
// When
148+
await devWithOverrideFile({adminSession, overrideJson, themeId: '789', open: true})
149+
150+
// Then
151+
expect(openURLSafely).toHaveBeenCalledWith(expectedPreviewUrl, 'theme preview')
152+
})
143153
})
144154

145155
test('does not open the preview URL when open is false', async () => {
146-
// Given
147-
vi.mocked(fileExistsSync).mockReturnValue(true)
148-
vi.mocked(readFile).mockResolvedValue(Buffer.from(JSON.stringify({templates: {}})))
149-
vi.mocked(fetchDevServerSession).mockResolvedValue(mockSession)
150-
vi.mocked(createThemePreview).mockResolvedValue({url: expectedPreviewUrl, preview_identifier: expectedPreviewId})
151-
152-
// When
153-
await devWithOverrideFile({adminSession, overrideJson: '/overrides.json', themeId: '789', open: false})
154-
155-
// Then
156-
expect(openURLSafely).not.toHaveBeenCalled()
156+
await inTemporaryDirectory(async (tmpDir) => {
157+
// Given
158+
const overrideJson = joinPath(tmpDir, 'overrides.json')
159+
await writeFile(overrideJson, JSON.stringify({templates: {}}))
160+
vi.mocked(fetchDevServerSession).mockResolvedValue(mockSession)
161+
vi.mocked(createThemePreview).mockResolvedValue({url: expectedPreviewUrl, preview_identifier: expectedPreviewId})
162+
163+
// When
164+
await devWithOverrideFile({adminSession, overrideJson, themeId: '789', open: false})
165+
166+
// Then
167+
expect(openURLSafely).not.toHaveBeenCalled()
168+
})
157169
})
158170

159171
test('passes password to fetchDevServerSession when provided', async () => {
160-
// Given
161-
vi.mocked(fileExistsSync).mockReturnValue(true)
162-
vi.mocked(readFile).mockResolvedValue(Buffer.from(JSON.stringify({templates: {}})))
163-
vi.mocked(fetchDevServerSession).mockResolvedValue(mockSession)
164-
vi.mocked(createThemePreview).mockResolvedValue({url: expectedPreviewUrl, preview_identifier: expectedPreviewId})
165-
166-
// When
167-
await devWithOverrideFile({
168-
adminSession,
169-
overrideJson: '/overrides.json',
170-
themeId: '789',
171-
open: false,
172-
json: false,
173-
password: 'shptka_abc123',
172+
await inTemporaryDirectory(async (tmpDir) => {
173+
// Given
174+
const overrideJson = joinPath(tmpDir, 'overrides.json')
175+
await writeFile(overrideJson, JSON.stringify({templates: {}}))
176+
vi.mocked(fetchDevServerSession).mockResolvedValue(mockSession)
177+
vi.mocked(createThemePreview).mockResolvedValue({url: expectedPreviewUrl, preview_identifier: expectedPreviewId})
178+
179+
// When
180+
await devWithOverrideFile({
181+
adminSession,
182+
overrideJson,
183+
themeId: '789',
184+
open: false,
185+
json: false,
186+
password: 'shptka_abc123',
187+
})
188+
189+
// Then
190+
expect(fetchDevServerSession).toHaveBeenCalledWith('789', adminSession, 'shptka_abc123')
174191
})
175-
176-
// Then
177-
expect(fetchDevServerSession).toHaveBeenCalledWith('789', adminSession, 'shptka_abc123')
178192
})
179193

180194
test('outputs JSON when json flag is true', async () => {
181-
// Given
182-
vi.mocked(fileExistsSync).mockReturnValue(true)
183-
vi.mocked(readFile).mockResolvedValue(Buffer.from(JSON.stringify({templates: {}})))
184-
vi.mocked(fetchDevServerSession).mockResolvedValue(mockSession)
185-
vi.mocked(createThemePreview).mockResolvedValue({url: expectedPreviewUrl, preview_identifier: expectedPreviewId})
186-
clearCollectedLogs()
187-
188-
// When
189-
await devWithOverrideFile({adminSession, overrideJson: '/overrides.json', themeId: '789', open: false, json: true})
190-
191-
// Then
192-
const expectedJson = JSON.stringify({url: expectedPreviewUrl, preview_identifier: expectedPreviewId})
193-
expect(collectedLogs.info).toContainEqual(expectedJson)
194-
expect(renderSuccess).not.toHaveBeenCalled()
195+
await inTemporaryDirectory(async (tmpDir) => {
196+
// Given
197+
const overrideJson = joinPath(tmpDir, 'overrides.json')
198+
await writeFile(overrideJson, JSON.stringify({templates: {}}))
199+
vi.mocked(fetchDevServerSession).mockResolvedValue(mockSession)
200+
vi.mocked(createThemePreview).mockResolvedValue({url: expectedPreviewUrl, preview_identifier: expectedPreviewId})
201+
clearCollectedLogs()
202+
203+
// When
204+
await devWithOverrideFile({adminSession, overrideJson, themeId: '789', open: false, json: true})
205+
206+
// Then
207+
const expectedJson = JSON.stringify({url: expectedPreviewUrl, preview_identifier: expectedPreviewId})
208+
expect(collectedLogs.info).toContainEqual(expectedJson)
209+
expect(renderSuccess).not.toHaveBeenCalled()
210+
})
195211
})
196212

197213
test('renders success body by default when json flag is omitted', async () => {
198-
// Given
199-
vi.mocked(fileExistsSync).mockReturnValue(true)
200-
vi.mocked(readFile).mockResolvedValue(Buffer.from(JSON.stringify({templates: {}})))
201-
vi.mocked(fetchDevServerSession).mockResolvedValue(mockSession)
202-
vi.mocked(createThemePreview).mockResolvedValue({url: expectedPreviewUrl, preview_identifier: expectedPreviewId})
203-
clearCollectedLogs()
204-
205-
// When
206-
await devWithOverrideFile({adminSession, overrideJson: '/overrides.json', themeId: '789', open: false})
207-
208-
// Then
209-
expect(renderSuccess).toHaveBeenCalled()
214+
await inTemporaryDirectory(async (tmpDir) => {
215+
// Given
216+
const overrideJson = joinPath(tmpDir, 'overrides.json')
217+
await writeFile(overrideJson, JSON.stringify({templates: {}}))
218+
vi.mocked(fetchDevServerSession).mockResolvedValue(mockSession)
219+
vi.mocked(createThemePreview).mockResolvedValue({url: expectedPreviewUrl, preview_identifier: expectedPreviewId})
220+
clearCollectedLogs()
221+
222+
// When
223+
await devWithOverrideFile({adminSession, overrideJson, themeId: '789', open: false})
224+
225+
// Then
226+
expect(renderSuccess).toHaveBeenCalled()
227+
})
210228
})
211229
})

0 commit comments

Comments
 (0)