Vitest : File is not defined
Les tests fonctionnent en local mais échouent en CI.
Symptôme
Section titled “Symptôme”$ vitest runfailed to load config from /workspace/Mindlet/backend/vitest.config.tsReferenceError: File is not defined at .../undici/lib/web/webidl/index.js:533:48Analyse
Section titled “Analyse”flowchart TD
A["bun run test"] --> B["vitest run<br/>(shebang: #!/usr/bin/env node)"]
B --> C["Charge vitest.config.ts"]
C --> D["Import @cloudflare/vitest-pool-workers"]
D --> E["Charge undici (dépendance)"]
E --> F{"Node.js version ?"}
F -->|"≥ 20"| G["✅ File existe"]
F -->|"< 20"| H["❌ File undefined → crash"]
Chaîne d’événements :
bun run testexécute le script"test": "vitest run"- Le binaire vitest a un shebang
#!/usr/bin/env node→ s’exécute sous Node.js - Vitest charge la config qui importe
@cloudflare/vitest-pool-workers/config - Cela charge
undici(dépendance de wrangler/miniflare) undiciutilise la globaleFile, disponible depuis Node.js 20- Le runner CI
ubuntu-latesta Node.js 18.x → crash
Solution
Section titled “Solution”Ajouter Node.js 22 dans le workflow CI :
jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: "22" - uses: oven-sh/setup-bun@v1 with: bun-version: latest - run: bun install --frozen-lockfile - run: bun run testArchitecture du pipeline de tests
Section titled “Architecture du pipeline de tests”flowchart TD
A["bun run test"] --> B["vitest run<br/>(Node.js)"]
B --> C["Charge vitest.config.ts"]
C --> D["defineWorkersConfig()"]
C --> E["readD1Migrations()"]
B --> F["Démarre workerd<br/>(runtime Cloudflare)"]
F --> G["Bind DB (D1)"]
F --> H["Bind TEST_MIGRATIONS"]
B --> I["setupFiles:<br/>test/apply-migrations.ts"]
I --> J["applyD1Migrations()"]
B --> K["Exécute les tests<br/>dans workerd"]
Points clés
Section titled “Points clés”| Élément | Rôle |
|---|---|
| Bun | Package manager (bun install, bun.lock) |
| Node.js | Runtime pour Vitest (via shebang) |
| workerd | Runtime Cloudflare Workers pour les tests |
| miniflare | Simulateur local de l’environnement Workers |
Important : Même avec Bun comme package manager, Vitest nécessite Node.js. Les deux doivent être configurés dans le CI.