Skip to content

Commit

Permalink
tests: wait for node modules to load
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnavK-09 committed Jan 28, 2025
1 parent 44d574f commit 51a8634
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
3 changes: 2 additions & 1 deletion cli/dev/DevServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class DevServer {
this.fsKy = ky.create({
prefixUrl: `http://localhost:${port}`,
}) as any
this.typesHandler = new FilesystemTypesHandler(this.projectDir)
}

async start() {
Expand Down Expand Up @@ -81,7 +82,7 @@ export class DevServer {

this.upsertInitialFiles()

this.typesHandler = new FilesystemTypesHandler(this.projectDir)
this.typesHandler?.handleInitialTypeDependencies(this.componentFilePath)
}

async addEntrypoint() {
Expand Down
36 changes: 24 additions & 12 deletions tests/cli/dev/dev.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import { test, expect, afterEach, beforeEach, mock } from "bun:test"
import { test, expect, afterEach } from "bun:test"
import { DevServer } from "cli/dev/DevServer"
import { getTestFixture } from "tests/fixtures/get-test-fixture"
import fs from "node:fs"
import path from "node:path"

async function waitForFile(
filePath: string,
timeout: number = 5000,
interval: number = 500,
): Promise<boolean> {
const endTime = Date.now() + timeout
while (Date.now() < endTime) {
if (fs.existsSync(filePath)) {
return true
}
await new Promise((resolve) => setTimeout(resolve, interval))
}
return false
}

test("types are installed and refreshed when files change", async () => {
const { tempDirPath, devServerPort } = await getTestFixture({
vfs: {
Expand All @@ -21,14 +36,14 @@ test("types are installed and refreshed when files change", async () => {
})

await devServer.start()
await devServer.handleFileChangedOnFilesystem(`${tempDirPath}/snippet.tsx`)

// Verify initial type installation
// Wait for the initial type file to be installed
const typePath = path.join(
tempDirPath,
"node_modules/@tsci/seveibar.red-led/index.d.ts",
)
expect(fs.existsSync(typePath)).toBe(true)
const typeFileExists = await waitForFile(typePath)
expect(typeFileExists).toBe(true)

// Simulate file change with new import
const updatedContent = `
Expand All @@ -37,17 +52,14 @@ test("types are installed and refreshed when files change", async () => {
`
fs.writeFileSync(`${tempDirPath}/snippet.tsx`, updatedContent)

// Trigger file change handler
await devServer.handleFileChangedOnFilesystem(`${tempDirPath}/snippet.tsx`)

// Verify new types file still exists after update
// Wait for the new type file to be installed after update
const typePath2 = path.join(
tempDirPath,
"node_modules/@tsci/seveibar.smd-usb-c/index.d.ts",
)
expect(fs.existsSync(typePath2)).toBe(true)
const typeFileExists2 = await waitForFile(typePath2)
expect(typeFileExists2).toBe(true)

afterEach(async () => {
await devServer.stop()
})
// Stop the dev server after the test
await devServer.stop()
}, 10_000)

0 comments on commit 51a8634

Please sign in to comment.