Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacroldan committed Nov 29, 2024
1 parent 80ab0cd commit 642bebd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
1 change: 1 addition & 0 deletions packages/app/src/cli/models/app/app.test-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ function defaultFunctionConfiguration(): FunctionConfigType {
type: 'product_discounts',
build: {
command: 'echo "hello world"',
watch: ['src/**/*.rs'],
},
api_version: '2022-07',
configuration_ui: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '../../../models/app/app.test-data.js'
import {ExtensionInstance} from '../../../models/extensions/extension-instance.js'
import {loadApp, reloadApp} from '../../../models/app/loader.js'
import {AppInterface} from '../../../models/app/app.js'
import {AppLinkedInterface} from '../../../models/app/app.js'
import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest'
import {AbortSignal, AbortController} from '@shopify/cli-kit/node/abort'
import {flushPromises} from '@shopify/cli-kit/node/promises'
Expand Down Expand Up @@ -260,7 +260,6 @@ describe('app-event-watcher', () => {

const mockManager = new MockESBuildContextManager()
const mockFileWatcher = new MockFileWatcher(app, outputOptions, [fileWatchEvent])

const watcher = new AppEventWatcher(app, 'url', buildOutputPath, mockManager, mockFileWatcher)
const emitSpy = vi.spyOn(watcher, 'emit')
await watcher.start({stdout, stderr, signal: abortController.signal})
Expand Down Expand Up @@ -290,6 +289,7 @@ describe('app-event-watcher', () => {
extensionEvents: expect.arrayContaining(extensionEvents),
startTime: expect.anything(),
path: expect.anything(),
appWasReloaded: needsAppReload,
})

const initialEvents = app.realExtensions.map((eve) => ({
Expand Down Expand Up @@ -426,7 +426,7 @@ class MockFileWatcher extends FileWatcher {
private readonly events: WatcherEvent[]
private listener?: (events: WatcherEvent[]) => void

constructor(app: AppInterface, options: OutputContextOptions, events: WatcherEvent[]) {
constructor(app: AppLinkedInterface, options: OutputContextOptions, events: WatcherEvent[]) {
super(app, options)
this.events = events
}
Expand Down
45 changes: 24 additions & 21 deletions packages/app/src/cli/services/dev/app-events/file-watcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {FileWatcher, OutputContextOptions, WatcherEvent} from './file-watcher.js'
import {
testApp,
testAppAccessConfigExtension,
testAppConfigExtensions,
testAppLinked,
testFunctionExtension,
testUIExtension,
} from '../../../models/app/app.test-data.js'
import {flushPromises} from '@shopify/cli-kit/node/promises'
Expand All @@ -12,10 +12,12 @@ import chokidar from 'chokidar'
import {AbortSignal} from '@shopify/cli-kit/node/abort'
import {inTemporaryDirectory, mkdir, writeFile} from '@shopify/cli-kit/node/fs'
import {joinPath} from '@shopify/cli-kit/node/path'
import {sleep} from '@shopify/cli-kit/node/system'

const extension1 = await testUIExtension({type: 'ui_extension', handle: 'h1', directory: '/extensions/ui_extension_1'})
const extension1B = await testUIExtension({type: 'ui_extension', handle: 'h2', directory: '/extensions/ui_extension_1'})
const extension2 = await testUIExtension({type: 'ui_extension', directory: '/extensions/ui_extension_2'})
const functionExtension = await testFunctionExtension({dir: '/extensions/my-function'})
const posExtension = await testAppConfigExtensions()
const appAccessExtension = await testAppAccessConfigExtension()

Expand All @@ -30,7 +32,7 @@ interface TestCaseSingleEvent {
name: string
fileSystemEvent: string
path: string
expectedEvent: WatcherEvent
expectedEvent?: WatcherEvent
}

/**
Expand Down Expand Up @@ -127,6 +129,12 @@ const singleEventTestCases: TestCaseSingleEvent[] = [
startTime: expect.any(Array),
},
},
{
name: 'change in function extension is ignored if not in watch list',
fileSystemEvent: 'change',
path: '/extensions/my-function/src/cargo.lock',
expectedEvent: undefined,
},
]

const multiEventTestCases: TestCaseMultiEvent[] = [
Expand Down Expand Up @@ -167,8 +175,8 @@ const multiEventTestCases: TestCaseMultiEvent[] = [
]

const outputOptions: OutputContextOptions = {stdout: process.stdout, stderr: process.stderr, signal: new AbortSignal()}
const defaultApp = testApp({
allExtensions: [extension1, extension1B, extension2, posExtension, appAccessExtension],
const defaultApp = testAppLinked({
allExtensions: [extension1, extension1B, extension2, posExtension, appAccessExtension, functionExtension],
directory: '/',
configuration: {scopes: '', extension_directories: ['/extensions'], path: '/shopify.app.toml'},
})
Expand Down Expand Up @@ -205,17 +213,7 @@ describe('file-watcher events', () => {

// Then
expect(watchSpy).toHaveBeenCalledWith([joinPath(dir, '/shopify.app.toml'), joinPath(dir, '/extensions')], {
ignored: [
'**/node_modules/**',
'**/.git/**',
'**/*.test.*',
'**/dist/**',
'**/*.swp',
'**/generated/**',
joinPath(dir, '/extensions/ext1/a_folder'),
joinPath(dir, '/extensions/ext1/a_file.txt'),
joinPath(dir, '/extensions/ext1/**/nested/**'),
],
ignored: ['**/node_modules/**', '**/.git/**', '**/*.test.*', '**/dist/**', '**/*.swp', '**/generated/**'],
ignoreInitial: true,
persistent: true,
})
Expand Down Expand Up @@ -244,12 +242,17 @@ describe('file-watcher events', () => {
await flushPromises()

// use waitFor to so that we can test the debouncers and timeouts
await vi.waitFor(
() => {
expect(onChange).toHaveBeenCalledWith([expectedEvent])
},
{timeout: 2000, interval: 100},
)
if (expectedEvent) {
await vi.waitFor(
() => {
expect(onChange).toHaveBeenCalledWith([expectedEvent])
},
{timeout: 2000, interval: 100},
)
} else {
await sleep(0.01)
expect(onChange).not.toHaveBeenCalled()
}
},
)

Expand Down

0 comments on commit 642bebd

Please sign in to comment.