Skip to content

test: add unit tests for build adapter#12

Open
Jack-sh1 wants to merge 3 commits into
devframes:mainfrom
Jack-sh1:test/adapters-build
Open

test: add unit tests for build adapter#12
Jack-sh1 wants to merge 3 commits into
devframes:mainfrom
Jack-sh1:test/adapters-build

Conversation

@Jack-sh1
Copy link
Copy Markdown

Summary

createBuild had zero test coverage. This PR adds unit tests covering:

  • Output directory creation and cleanup of stale files
  • SPA dist copying into outDir
  • __connection.json generation with backend: 'static'
  • __rpc-dump/ directory and index.json manifest writing
  • spa-loader.json conditional output (only when d.spa is defined)
  • d.setup() hook invocation with build mode context
  • Error when distDir is not provided

Test plan

  • All new tests pass locally via vitest
  • CI passes

createBuild had zero test coverage. Add tests for output directory
creation/cleanup, SPA dist copying, __connection.json generation,
__rpc-dump directory and manifest writing, spa-loader.json conditional
output, and setup hook invocation.
@netlify
Copy link
Copy Markdown

netlify Bot commented May 13, 2026

Deploy Preview for devfra ready!

Name Link
🔨 Latest commit 15c015b
🔍 Latest deploy log https://app.netlify.com/projects/devfra/deploys/6a0494a8723e0f000853970c
😎 Deploy Preview https://deploy-preview-12--devfra.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.


afterEach(async () => {
await fs.rm(outDir, { recursive: true, force: true })
await fs.rm(distDir, { recursive: true, force: true })
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use try-finally block or use await Promise.allSettled, if something is wrong deleting outDir then distDir won't be removed: it would be also nice if we can add some retry options:

// fs comes from `import fs from 'node:fs/promises`
    await fs.rm(tempPath, {
      recursive: true,
      force: true,
      maxRetries: 3,
      retryDelay: 100,
    }).catch((err) => {
      console.error(`Failed to cleanup sandbox at ${tempPath}:`, err)
    })

Ensures both outDir and distDir are cleaned up even if one removal
fails, and adds maxRetries/retryDelay for transient fs errors.
Comment thread packages/devframe/src/adapters/build.test.ts
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds Vitest unit coverage for the createBuild adapter to validate the static-build output structure and key generated metadata files.

Changes:

  • Introduces a new createBuild test suite covering outDir creation/cleanup, SPA copying, connection meta, and RPC dump output.
  • Adds assertions for conditional spa-loader.json emission and missing-distDir error behavior.
  • Verifies the setup() hook is invoked during the build flow.
Comments suppressed due to low confidence (3)

packages/devframe/src/adapters/build.test.ts:37

  • afterEach swallows cleanup failures and logs with console.warn, which can both hide real failures and violate no-console linting rules. Consider using fs.rm(..., { recursive: true, force: true }) without logging, or explicitly fail the test when cleanup fails (and if you need silence, stub console via vi.spyOn).
  afterEach(async () => {
    const rmOpts = { recursive: true, force: true, maxRetries: 3, retryDelay: 100 } as const
    await Promise.allSettled([
      fs.rm(outDir, rmOpts).catch((err) => {
        console.warn(`Failed to cleanup outDir at ${outDir}, manual deletion required:`, err)
      }),
      fs.rm(distDir, rmOpts).catch((err) => {
        console.warn(`Failed to cleanup distDir at ${distDir}, manual deletion required:`, err)
      }),
    ])

packages/devframe/src/adapters/build.test.ts:38

  • createBuild prints multiple console.log lines per invocation; since these tests call it many times, the test run will be very noisy. It’s better to stub console.log/console.error for this suite (e.g. in beforeEach with vi.spyOn(console, 'log').mockImplementation(() => {})) and restore in afterEach.
describe('createBuild', () => {
  const outDir = resolve('/tmp/devframe-build-test-out')
  const distDir = resolve('/tmp/test-spa-dist')

  beforeEach(async () => {
    // Create a fake SPA dist directory with an index.html
    await fs.mkdir(distDir, { recursive: true })
    await fs.writeFile(resolve(distDir, 'index.html'), '<html></html>')
  })

  afterEach(async () => {
    const rmOpts = { recursive: true, force: true, maxRetries: 3, retryDelay: 100 } as const
    await Promise.allSettled([
      fs.rm(outDir, rmOpts).catch((err) => {
        console.warn(`Failed to cleanup outDir at ${outDir}, manual deletion required:`, err)
      }),
      fs.rm(distDir, rmOpts).catch((err) => {
        console.warn(`Failed to cleanup distDir at ${distDir}, manual deletion required:`, err)
      }),
    ])
  })

packages/devframe/src/adapters/build.test.ts:104

  • The test name says it verifies build-mode context, but it only asserts setup was called. To actually cover the behavior, assert that setup received a context with mode: 'build' (and any other relevant fields) so the test fails if createBuild stops passing the correct context.
  it('calls d.setup with build mode context', async () => {
    const setup = vi.fn(async () => {})
    const d = stubDefinition({ setup })
    await createBuild(d, { outDir, distDir })
    expect(setup).toHaveBeenCalledOnce()
  })

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +18 to +26
describe('createBuild', () => {
const outDir = resolve('/tmp/devframe-build-test-out')
const distDir = resolve('/tmp/test-spa-dist')

beforeEach(async () => {
// Create a fake SPA dist directory with an index.html
await fs.mkdir(distDir, { recursive: true })
await fs.writeFile(resolve(distDir, 'index.html'), '<html></html>')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants