vitest.setup.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { statSync } from 'fs'
  2. import { config } from 'dotenv'
  3. import './test/extensions'
  4. if (!process.env.CI) {
  5. // Use keys from studio .env.local for local tests
  6. const envPath = '../../apps/studio/.env.local'
  7. statSync(envPath)
  8. config({ path: envPath })
  9. }
  10. // Modify fetch to support wasm file URLs
  11. globalThis.fetch = async (input: string | URL | Request, init?: RequestInit) => {
  12. if (input instanceof Request) {
  13. return fetch(input, init)
  14. }
  15. const url = new URL(input)
  16. if (url.protocol === 'file:' && url.pathname.endsWith('.wasm')) {
  17. return fetchFileNode(input, 'application/wasm')
  18. }
  19. return fetch(input, init)
  20. }
  21. async function fetchFileNode(input: string | URL, type: string) {
  22. const fs = await import('node:fs')
  23. const { Readable } = await import('node:stream')
  24. const { fileURLToPath } = await import('node:url')
  25. const path = fileURLToPath(input)
  26. const nodeStream = fs.createReadStream(path)
  27. const stream = Readable.toWeb(nodeStream) as ReadableStream<Uint8Array>
  28. return new Response(stream, { headers: { 'Content-Type': type } })
  29. }