import { describe, it, expect, vi } from 'vitest'; import { yarnSpinnerVite } from '../../src/plugins/vite'; import { resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; const __dirname = fileURLToPath(new URL('.', import.meta.url)); describe('yarnSpinnerVite', () => { // Fixtures are in tests/fixtures/, not tests/plugins/fixtures/ const projectPath = resolve(__dirname, '../fixtures/simple/.yarnproject'); it('should return a Vite plugin with correct name', () => { const plugin = yarnSpinnerVite(); expect(plugin.name).toBe('yarn-spinner-loader'); }); it('should have a load function', () => { const plugin = yarnSpinnerVite(); expect(plugin.load).toBeDefined(); expect(typeof plugin.load).toBe('function'); }); it('should load .yarnproject files and return exported code', async () => { const plugin = yarnSpinnerVite(); const loadFn = plugin.load as Function; const mockContext = { error: vi.fn(), }; const result = await loadFn.call(mockContext, projectPath); // If error was called, the file wasn't found or was invalid if (mockContext.error.mock.calls.length > 0) { throw new Error(mockContext.error.mock.calls[0][0]); } expect(result).toBeDefined(); expect(typeof result).toBe('string'); expect(result).toContain('export default'); expect(result).toContain('project'); expect(result).toContain('yarnFiles'); }); it('should return null for non-.yarnproject files', async () => { const plugin = yarnSpinnerVite(); const loadFn = plugin.load as Function; const result = await loadFn.call({}, 'some-file.ts'); expect(result).toBeNull(); }); it('should handle errors by calling this.error', async () => { const plugin = yarnSpinnerVite(); const loadFn = plugin.load as Function; const mockThis = { error: vi.fn(), }; await loadFn.call(mockThis, 'nonexistent/.yarnproject'); expect(mockThis.error).toHaveBeenCalled(); }); it('should accept loadOptions and pass them to loadYarnProject', async () => { const plugin = yarnSpinnerVite({ loadOptions: { baseDir: resolve(__dirname, '../fixtures/simple'), }, }); const loadFn = plugin.load as Function; const mockContext = { error: vi.fn(), }; const result = await loadFn.call(mockContext, projectPath); expect(mockContext.error).not.toHaveBeenCalled(); expect(result).toBeDefined(); expect(typeof result).toBe('string'); expect(result).toContain('export default'); }); it('should have a transform function', () => { const plugin = yarnSpinnerVite(); expect(plugin.transform).toBeDefined(); expect(typeof plugin.transform).toBe('function'); }); it('transform should return empty export for .yarnproject files', () => { const plugin = yarnSpinnerVite(); const transformFn = plugin.transform as Function; const result = transformFn.call({}, 'console.log("test")', projectPath); expect(result).toEqual({ code: 'export default {};', map: null, }); }); it('transform should return null for non-.yarnproject files', () => { const plugin = yarnSpinnerVite(); const transformFn = plugin.transform as Function; const result = transformFn.call({}, 'console.log("test")', 'some-file.ts'); expect(result).toBeNull(); }); });