error.spec.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233
  1. /** Diagnostics preserve compiler locations and unknown loader failures. */
  2. import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
  3. import { tmpdir } from 'node:os'
  4. import { join } from 'node:path'
  5. import { Context } from '@deepseek-ai/cordis'
  6. import { expect, it, onTestFinished, vi } from 'vitest'
  7. import { handleError } from '../src/error.ts'
  8. it.each([null, 'failure', {}, { errors: null }, { errors: [null] }, { errors: [7] }, { errors: [{}] }, { errors: [{ text: 7 }] }])
  9. ('forwards an unrecognized error without dropping its diagnostic: %j', async (error) => {
  10. const ctx = new Context()
  11. onTestFinished(() => ctx.fiber.dispose())
  12. const warn = vi.spyOn(ctx.logger, 'warn')
  13. handleError(ctx, error)
  14. expect(warn).toHaveBeenCalledWith(error)
  15. })
  16. it('formats compiler source locations and reports unavailable source files', async () => {
  17. const root = mkdtempSync(join(tmpdir(), 'dsh-hmr-diagnostics-'))
  18. const file = join(root, 'broken.ts')
  19. writeFileSync(file, 'const broken = ;\n')
  20. const ctx = new Context()
  21. onTestFinished(async () => { await ctx.fiber.dispose(); rmSync(root, { recursive: true, force: true }) })
  22. const warn = vi.spyOn(ctx.logger, 'warn')
  23. handleError(ctx, { errors: [
  24. { text: 'without location' },
  25. { text: 'unexpected token', location: { file, line: 1, column: 15 } },
  26. { text: 'source disappeared', location: { file: join(root, 'missing.ts'), line: 1, column: 1 } },
  27. ] })
  28. expect(warn).toHaveBeenCalledWith('without location')
  29. expect(warn).toHaveBeenCalledWith(expect.stringContaining('unexpected token'))
  30. expect(warn).toHaveBeenCalledWith(expect.objectContaining({ code: 'ENOENT' }))
  31. })