|
| 1 | +import { test, expect, beforeEach, afterEach, vi } from "vitest"; |
| 2 | +import { paraglideVitePlugin } from "../bundler-plugins/vite.js"; |
| 3 | +import consola from "consola"; |
| 4 | +import { memfs } from "memfs"; |
| 5 | +import { |
| 6 | + loadProjectInMemory, |
| 7 | + newProject, |
| 8 | + saveProjectToDirectory, |
| 9 | +} from "@inlang/sdk"; |
| 10 | + |
| 11 | +let originalNodeEnv: string | undefined; |
| 12 | + |
| 13 | +beforeEach(() => { |
| 14 | + originalNodeEnv = process.env.NODE_ENV; |
| 15 | + |
| 16 | + // Mock logging methods to suppress error messages in tests |
| 17 | + consola.mockTypes(() => vi.fn()); |
| 18 | +}); |
| 19 | + |
| 20 | +afterEach(() => { |
| 21 | + if (originalNodeEnv !== undefined) { |
| 22 | + process.env.NODE_ENV = originalNodeEnv; |
| 23 | + } else { |
| 24 | + delete process.env.NODE_ENV; |
| 25 | + } |
| 26 | +}); |
| 27 | + |
| 28 | +test("vite plugin does not throw when compilation is successful", async () => { |
| 29 | + // Create and save a viable project to the virtual file system |
| 30 | + const project = await loadProjectInMemory({ |
| 31 | + blob: await newProject({ |
| 32 | + settings: { |
| 33 | + baseLocale: "en", |
| 34 | + locales: ["en", "de", "fr"], |
| 35 | + }, |
| 36 | + }), |
| 37 | + }); |
| 38 | + |
| 39 | + const fs = memfs().fs as unknown as typeof import("node:fs"); |
| 40 | + |
| 41 | + await saveProjectToDirectory({ |
| 42 | + project, |
| 43 | + path: "/project.inlang", |
| 44 | + fs: fs.promises, |
| 45 | + }); |
| 46 | + |
| 47 | + const plugin = paraglideVitePlugin({ |
| 48 | + project: "/project.inlang", |
| 49 | + outdir: "/test-output", |
| 50 | + fs: fs, |
| 51 | + }) as any; |
| 52 | + |
| 53 | + const mockContext = { |
| 54 | + addWatchFile: () => {}, |
| 55 | + }; |
| 56 | + |
| 57 | + await expect(plugin.buildStart?.call(mockContext)).resolves.toBeUndefined(); |
| 58 | +}); |
| 59 | + |
| 60 | +test("vite plugin does not throw on compilation errors in development", async () => { |
| 61 | + process.env.NODE_ENV = "development"; |
| 62 | + |
| 63 | + // Use memfs with no project (simulates missing project) |
| 64 | + const fs = memfs().fs as unknown as typeof import("node:fs"); |
| 65 | + |
| 66 | + const plugin = paraglideVitePlugin({ |
| 67 | + project: "/non-existent-project.inlang", |
| 68 | + outdir: "/test-output", |
| 69 | + fs: fs, |
| 70 | + }) as any; |
| 71 | + |
| 72 | + const mockContext = { |
| 73 | + addWatchFile: () => {}, |
| 74 | + }; |
| 75 | + |
| 76 | + // In development mode - should catch errors and NOT throw |
| 77 | + await expect(plugin.buildStart?.call(mockContext)).resolves.toBeUndefined(); |
| 78 | +}); |
| 79 | + |
| 80 | +test("vite plugin throws on compilation errors at build time", async () => { |
| 81 | + process.env.NODE_ENV = "production"; |
| 82 | + |
| 83 | + // Use memfs with no project (simulates missing project) |
| 84 | + const fs = memfs().fs as unknown as typeof import("node:fs"); |
| 85 | + |
| 86 | + const plugin = paraglideVitePlugin({ |
| 87 | + project: "/non-existent-project.inlang", |
| 88 | + outdir: "/test-output", |
| 89 | + fs: fs, |
| 90 | + }) as any; |
| 91 | + |
| 92 | + const mockContext = { |
| 93 | + addWatchFile: () => {}, |
| 94 | + }; |
| 95 | + |
| 96 | + // In production mode - should throw the error |
| 97 | + await expect(plugin.buildStart?.call(mockContext)).rejects.toThrow(); |
| 98 | +}); |
0 commit comments