Skip to content

Commit 9b80a4f

Browse files
Merge pull request #7449 from Shopify/refactor-notifier-test-filesystem-mock-2324320246412902882
[Tests] Refactor Notifier tests to remove filesystem mocks
2 parents 9f31416 + 22cb6dc commit 9b80a4f

1 file changed

Lines changed: 30 additions & 26 deletions

File tree

packages/theme/src/cli/utilities/notifier.test.ts

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import {Notifier} from './notifier.js'
22
import {vi, describe, expect, test} from 'vitest'
33
import {outputWarn} from '@shopify/cli-kit/node/output'
4+
import {inTemporaryDirectory, readFile} from '@shopify/cli-kit/node/fs'
5+
import {joinPath} from '@shopify/cli-kit/node/path'
46

5-
import fs from 'fs/promises'
6-
7-
vi.mock('fs/promises')
87
vi.mock('@shopify/cli-kit/node/output')
98

109
describe('Notifier', () => {
@@ -26,13 +25,16 @@ describe('Notifier', () => {
2625
})
2726

2827
test('updates file atime and mtime when path is not a URL', async () => {
29-
const path = 'theme.update'
30-
notifier = new Notifier(path)
31-
const fileName = 'announcement.liquid'
28+
await inTemporaryDirectory(async (tmpDir) => {
29+
const path = joinPath(tmpDir, 'theme.update')
30+
notifier = new Notifier(path)
31+
const fileName = 'announcement.liquid'
3232

33-
await notifier.notify(fileName)
33+
await notifier.notify(fileName)
3434

35-
expect(fs.writeFile).toHaveBeenCalledWith(path, fileName)
35+
const content = await readFile(path)
36+
expect(content).toEqual(fileName)
37+
})
3638
})
3739

3840
test('does not update if path is empty', async () => {
@@ -43,19 +45,22 @@ describe('Notifier', () => {
4345
await notifier.notify(fileName)
4446

4547
expect(fetchSpy).not.toHaveBeenCalled()
46-
expect(fs.appendFile).not.toHaveBeenCalled()
4748
})
4849

4950
test('does not notify file when path is URL', async () => {
50-
const url = 'https://example.com/notify'
51-
const mockFetch = vi.spyOn(global, 'fetch').mockResolvedValue(new Response())
52-
notifier = new Notifier(url)
53-
const fileName = 'announcement.liquid'
54-
55-
await notifier.notify(fileName)
56-
57-
expect(mockFetch).toHaveBeenCalled()
58-
expect(fs.appendFile).not.toHaveBeenCalled()
51+
await inTemporaryDirectory(async (tmpDir) => {
52+
const url = 'https://example.com/notify'
53+
const mockFetch = vi.spyOn(global, 'fetch').mockResolvedValue(new Response())
54+
notifier = new Notifier(url)
55+
const fileName = 'announcement.liquid'
56+
const path = joinPath(tmpDir, 'theme.update')
57+
58+
await notifier.notify(fileName)
59+
60+
expect(mockFetch).toHaveBeenCalled()
61+
const fileExists = await readFile(path).catch(() => null)
62+
expect(fileExists).toBeNull()
63+
})
5964
})
6065

6166
test('prints error if response is not successful', async () => {
@@ -85,15 +90,14 @@ describe('Notifier', () => {
8590
})
8691

8792
test('outputs error if file update fails', async () => {
88-
const invalidPath = 'dir/file:theme.update'
89-
vi.spyOn(fs, 'writeFile').mockRejectedValue(new Error('No such file or directory'))
90-
notifier = new Notifier(invalidPath)
91-
const fileName = 'announcement.liquid'
93+
await inTemporaryDirectory(async (tmpDir) => {
94+
const invalidPath = tmpDir
95+
notifier = new Notifier(invalidPath)
96+
const fileName = 'announcement.liquid'
9297

93-
await notifier.notify(fileName)
98+
await notifier.notify(fileName)
9499

95-
expect(outputWarn).toHaveBeenCalledWith(
96-
`Failed to notify filechange listener at ${invalidPath}: No such file or directory`,
97-
)
100+
expect(outputWarn).toHaveBeenCalledWith(expect.stringContaining(`Failed to notify filechange listener at ${tmpDir}`))
101+
})
98102
})
99103
})

0 commit comments

Comments
 (0)