|
| 1 | +import { describe, expect, mock, spyOn, test, beforeEach, afterEach } from 'bun:test'; |
| 2 | + |
| 3 | +// Mock dependencies before imports |
| 4 | +mock.module('fs-extra', () => ({ |
| 5 | + default: { |
| 6 | + access: async () => {}, |
| 7 | + }, |
| 8 | +})); |
| 9 | + |
| 10 | +mock.module('i18next', () => ({ |
| 11 | + default: { |
| 12 | + init: () => {}, |
| 13 | + t: (key: string) => key, |
| 14 | + }, |
| 15 | +})); |
| 16 | + |
| 17 | +import { checkPlugins } from '../src/utils/check-plugin'; |
| 18 | +import * as pluginConfig from '../src/utils/plugin-config'; |
| 19 | + |
| 20 | +describe('checkPlugins', () => { |
| 21 | + test('should detect plugins concurrently (simulated)', async () => { |
| 22 | + const mockPlugins = [ |
| 23 | + { |
| 24 | + name: 'plugin1', |
| 25 | + bundleParams: { p1: true }, |
| 26 | + detect: async () => { |
| 27 | + await new Promise(resolve => setTimeout(resolve, 100)); |
| 28 | + return true; |
| 29 | + } |
| 30 | + }, |
| 31 | + { |
| 32 | + name: 'plugin2', |
| 33 | + bundleParams: { p2: true }, |
| 34 | + detect: async () => { |
| 35 | + await new Promise(resolve => setTimeout(resolve, 100)); |
| 36 | + return true; |
| 37 | + } |
| 38 | + }, |
| 39 | + { |
| 40 | + name: 'plugin3', |
| 41 | + bundleParams: { p3: true }, |
| 42 | + detect: async () => { |
| 43 | + await new Promise(resolve => setTimeout(resolve, 100)); |
| 44 | + return true; |
| 45 | + } |
| 46 | + } |
| 47 | + ]; |
| 48 | + |
| 49 | + // Replacing the plugins array in plugin-config |
| 50 | + const originalPlugins = [...pluginConfig.plugins]; |
| 51 | + (pluginConfig.plugins as any).splice(0, pluginConfig.plugins.length, ...mockPlugins); |
| 52 | + |
| 53 | + const start = Date.now(); |
| 54 | + const result = await checkPlugins(); |
| 55 | + const end = Date.now(); |
| 56 | + const duration = end - start; |
| 57 | + |
| 58 | + console.log(`Duration with optimized implementation: ${duration}ms`); |
| 59 | + |
| 60 | + expect(result).toEqual({ |
| 61 | + sentry: false, // default |
| 62 | + sourcemap: false, // default |
| 63 | + p1: true, |
| 64 | + p2: true, |
| 65 | + p3: true |
| 66 | + } as any); |
| 67 | + |
| 68 | + // Now it's concurrent, so we expect around 100ms. |
| 69 | + // We'll allow some buffer, but it should definitely be less than 250ms. |
| 70 | + expect(duration).toBeLessThan(250); |
| 71 | + |
| 72 | + // Restore original plugins |
| 73 | + (pluginConfig.plugins as any).splice(0, pluginConfig.plugins.length, ...originalPlugins); |
| 74 | + }); |
| 75 | +}); |
0 commit comments