dev-web.spec.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { mkdtemp, readFile, rm, symlink, writeFile } from 'node:fs/promises'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import { expect, it } from 'vitest'
  5. import type { TsdownBundle } from 'tsdown'
  6. import { watchClientPlugins } from './dev-web.ts'
  7. it('rebuilds a client-plugin bundle after its source changes', async () => {
  8. const root = await mkdtemp(join(tmpdir(), 'dsh-dev-web-watch-'))
  9. let bundles: TsdownBundle[] = []
  10. try {
  11. await symlink(join(import.meta.dirname, '..', 'node_modules'), join(root, 'node_modules'), 'dir')
  12. await writeFile(join(root, 'package.json'), JSON.stringify({ name: '@dsh-test/dev-web-watch', private: true, type: 'module' }))
  13. await writeFile(join(root, 'tsdown.config.ts'), `
  14. import { defineConfig } from 'tsdown'
  15. export default defineConfig({
  16. entry: { client: 'src.ts' }, outDir: 'lib', format: 'cjs', platform: 'browser', dts: false, clean: false,
  17. outputOptions: { entryFileNames: 'client.js' },
  18. })
  19. `)
  20. const sourcePath = join(root, 'src.ts')
  21. const bundlePath = join(root, 'lib/client.js')
  22. await writeFile(sourcePath, 'export const version = "watch-v1"\n')
  23. bundles = await watchClientPlugins(root, ['.'], 50)
  24. expect(await readFile(bundlePath, 'utf8')).toContain('watch-v1')
  25. await new Promise(resolve => setTimeout(resolve, 1_000))
  26. await writeFile(sourcePath, `export const version = "watch-v2-${'x'.repeat(100)}"\n`)
  27. await expect.poll(async () => (await readFile(bundlePath, 'utf8')).includes('watch-v2-'), {
  28. timeout: 10_000,
  29. }).toBe(true)
  30. } finally {
  31. for (const bundle of bundles) await bundle[Symbol.asyncDispose]()
  32. await rm(root, { recursive: true, force: true })
  33. }
  34. }, 20_000)