Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove timer faking from functionWatcher tests #5151

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {testAppLinked, testFunctionExtension} from '../../../../../../models/app
import {runFunction} from '../../../../runner.js'
import {AppEventWatcher, EventType} from '../../../../../dev/app-events/app-event-watcher.js'
import {AbortController} from '@shopify/cli-kit/node/abort'
import {render} from '@shopify/cli-kit/node/testing/ui'
import {test, describe, vi, beforeEach, afterEach, expect} from 'vitest'
import {render, waitFor} from '@shopify/cli-kit/node/testing/ui'
import {test, describe, vi, expect} from 'vitest'
import React from 'react'
import {Writable} from 'stream'

Expand Down Expand Up @@ -47,8 +47,8 @@ const EXEC_RESPONSE = {
output: SELECTED_RUN.payload.output,
logs: SELECTED_RUN.payload.logs,
name: SELECTED_RUN.source,
size: 0,
memory_usage: 0,
size: 1,
memory_usage: 1,
instructions: SELECTED_RUN.payload.fuelConsumed,
}

Expand All @@ -57,20 +57,12 @@ const SECOND_EXEC_RESPONSE = {
output: SELECTED_RUN.payload.output,
logs: SELECTED_RUN.payload.logs,
name: SELECTED_RUN.source,
size: 1,
memory_usage: 1,
size: 2,
memory_usage: 2,
instructions: SELECTED_RUN.payload.fuelConsumed,
}

describe('useFunctionWatcher', () => {
beforeEach(() => {
vi.useFakeTimers()
})

afterEach(() => {
vi.clearAllTimers()
})

test('runs function once in watch mode without changes', async () => {
// Given
vi.mocked(runFunction).mockImplementation(runFunctionMockImplementation(EXEC_RESPONSE))
Expand All @@ -85,12 +77,21 @@ describe('useFunctionWatcher', () => {
appWatcher: new AppEventWatcher(APP),
}),
)
// needed to await the render
await vi.advanceTimersByTimeAsync(0)

await waitFor(
() => {},
() => hook.lastResult?.recentFunctionRuns[0].size === EXEC_RESPONSE.size,
)

// Then
expect(runFunction).toHaveBeenCalledOnce()
expect(hook.lastResult?.recentFunctionRuns[0]).toEqual({...EXEC_RESPONSE, type: 'functionRun'})
expect(hook.lastResult?.recentFunctionRuns[1]).toEqual({
...EXEC_RESPONSE,
type: 'functionRun',
size: 0,
memory_usage: 0,
})
})

test('file watcher onChange re-runs function', async () => {
Expand All @@ -113,13 +114,24 @@ describe('useFunctionWatcher', () => {
)

// needed to await the render
await vi.advanceTimersByTimeAsync(0)
await waitFor(
() => {},
() => hook.lastResult?.recentFunctionRuns[0].size === EXEC_RESPONSE.size,
)

expect(hook.lastResult?.recentFunctionRuns[0]).toEqual({...EXEC_RESPONSE, type: 'functionRun'})
expect(hook.lastResult?.recentFunctionRuns[1]).toEqual({...EXEC_RESPONSE, type: 'functionRun'})
expect(hook.lastResult?.recentFunctionRuns[1]).toEqual({
...EXEC_RESPONSE,
type: 'functionRun',
size: 0,
memory_usage: 0,
})

appWatcher.emit('all', event)
await vi.advanceTimersByTimeAsync(0)
await waitFor(
() => {},
() => hook.lastResult?.recentFunctionRuns[0].size === SECOND_EXEC_RESPONSE.size,
)

expect(hook.lastResult?.recentFunctionRuns[0]).toEqual({...SECOND_EXEC_RESPONSE, type: 'functionRun'})
expect(hook.lastResult?.recentFunctionRuns[1]).toEqual({...EXEC_RESPONSE, type: 'functionRun'})
Expand Down Expand Up @@ -151,10 +163,16 @@ describe('useFunctionWatcher', () => {
)

// needed to await the render
await vi.advanceTimersByTimeAsync(0)
await waitFor(
() => {},
() => hook.lastResult?.recentFunctionRuns[0].size === EXEC_RESPONSE.size,
)

appWatcher.emit('all', event)
await vi.advanceTimersByTimeAsync(0)
await waitFor(
() => {},
() => hook.lastResult?.error !== undefined,
)

// Then
expect(runFunction).toHaveBeenCalledOnce()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export function useFunctionWatcher({
setRecentFunctionRuns((recentFunctionRuns) => {
return [functionRun, recentFunctionRuns[0]]
})
setStatusMessage(`Watching for changes to ${selectedRun.source}...`)
setLogs((logs) => [...logs, functionRun])
setStatusMessage(`Watching for changes to ${selectedRun.source}...`)
}

const initialReplay = async () => {
Expand Down
1 change: 1 addition & 0 deletions packages/cli-kit/src/public/node/testing/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export {
getLastFrameAfterUnmount,
render,
Stdin,
waitFor,
waitForInputsToBeReady,
waitForContent,
sendInputAndWait,
Expand Down
Loading