|
| 1 | +import { expect, describe, vi, it, beforeEach } from 'vitest'; |
| 2 | +import { ExtensionRunner } from '../../src'; |
| 3 | +import { createSafariRunner } from '../../src/core/runners/safari'; |
| 4 | +import { createManualRunner } from '../../src/core/runners/manual'; |
| 5 | +import { createWebExtRunner } from '../../src/core/runners/web-ext'; |
| 6 | +import { createWslRunner } from '../../src/core/runners/wsl'; |
| 7 | +import { TestProject } from '../utils'; |
| 8 | +import { wxt } from '../../src/core/wxt'; |
| 9 | + |
| 10 | +// Globals for modifying mock behaviors |
| 11 | + |
| 12 | +let isWsl = false; |
| 13 | +let importWebExtRunnerError: Error | undefined = undefined; |
| 14 | + |
| 15 | +// Mock runners to create constants for checking equality |
| 16 | + |
| 17 | +type TestExtensionRunner = { name: string } & ExtensionRunner; |
| 18 | + |
| 19 | +function createMockExtensionRunner(name: string): TestExtensionRunner { |
| 20 | + return { |
| 21 | + name, |
| 22 | + closeBrowser: () => Promise.resolve(), |
| 23 | + openBrowser: () => Promise.resolve(), |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +vi.mock('../../src/core/runners/safari', () => { |
| 28 | + const runner = createMockExtensionRunner('safari'); |
| 29 | + return { createSafariRunner: () => runner }; |
| 30 | +}); |
| 31 | +const safariRunner = createSafariRunner(); |
| 32 | + |
| 33 | +vi.mock('../../src/core/runners/manual', () => { |
| 34 | + const runner = createMockExtensionRunner('manual'); |
| 35 | + return { createManualRunner: () => runner }; |
| 36 | +}); |
| 37 | +const manualRunner = createManualRunner(); |
| 38 | + |
| 39 | +vi.mock('../../src/core/runners/web-ext', () => { |
| 40 | + const runner = createMockExtensionRunner('web-ext'); |
| 41 | + return { |
| 42 | + createWebExtRunner: () => { |
| 43 | + if (!importWebExtRunnerError) return runner; |
| 44 | + else throw importWebExtRunnerError; |
| 45 | + }, |
| 46 | + }; |
| 47 | +}); |
| 48 | +const webExtRunner = createWebExtRunner(); |
| 49 | + |
| 50 | +vi.mock('../../src/core/runners/wsl', () => { |
| 51 | + const runner = createMockExtensionRunner('wsl'); |
| 52 | + return { createWslRunner: () => runner }; |
| 53 | +}); |
| 54 | +const wslRunner = createWslRunner(); |
| 55 | + |
| 56 | +// Other mocks |
| 57 | + |
| 58 | +vi.mock('is-wsl', () => ({ |
| 59 | + get default() { |
| 60 | + return isWsl; |
| 61 | + }, |
| 62 | +})); |
| 63 | + |
| 64 | +/** |
| 65 | + * Imitate a real module not found error - needs the correct `code` property. |
| 66 | + */ |
| 67 | +class ModuleNotFoundError extends Error { |
| 68 | + code = 'ERR_MODULE_NOT_FOUND'; |
| 69 | + |
| 70 | + constructor(mod: string) { |
| 71 | + super(`Cannot find package '${mod}' imported from ...`); |
| 72 | + this.name = 'ModuleNotFoundError'; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +describe('Runners', () => { |
| 77 | + beforeEach(() => { |
| 78 | + isWsl = false; |
| 79 | + importWebExtRunnerError = undefined; |
| 80 | + }); |
| 81 | + |
| 82 | + describe('build', () => { |
| 83 | + const command = 'build'; |
| 84 | + |
| 85 | + it('should use the manual runner as a placeholder since the runner is not used during builds', async () => { |
| 86 | + await TestProject.simple().registerWxt(command); |
| 87 | + |
| 88 | + expect(wxt.config.runner).toBe(manualRunner); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('dev', () => { |
| 93 | + const command = 'serve'; |
| 94 | + |
| 95 | + describe('inside WSL', () => { |
| 96 | + beforeEach(() => { |
| 97 | + isWsl = true; |
| 98 | + }); |
| 99 | + |
| 100 | + it('should use the WSL runner', async () => { |
| 101 | + await TestProject.simple().registerWxt(command); |
| 102 | + |
| 103 | + expect(wxt.config.runner).toBe(wslRunner); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('web-ext is installed', () => { |
| 108 | + it('should use the web-ext runner', async () => { |
| 109 | + await TestProject.simple().registerWxt(command); |
| 110 | + |
| 111 | + expect(wxt.config.runner).toBe(webExtRunner); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('disabled', () => { |
| 115 | + it('should use the manual runner', async () => { |
| 116 | + await TestProject.simple().registerWxt(command, { |
| 117 | + webExt: { disabled: true }, |
| 118 | + }); |
| 119 | + |
| 120 | + expect(wxt.config.runner).toBe(manualRunner); |
| 121 | + }); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + describe('web-ext is not installed', () => { |
| 126 | + beforeEach(() => { |
| 127 | + importWebExtRunnerError = new ModuleNotFoundError('web-ext'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should use the manual runner', async () => { |
| 131 | + await TestProject.simple().registerWxt(command); |
| 132 | + |
| 133 | + expect(wxt.config.runner).toBe(manualRunner); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('some other error when importing the web-ext runner', () => { |
| 138 | + beforeEach(() => { |
| 139 | + importWebExtRunnerError = Error('test'); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should throw the error', async () => { |
| 143 | + await expect(TestProject.simple().registerWxt(command)).rejects.toThrow( |
| 144 | + importWebExtRunnerError, |
| 145 | + ); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + describe('targeting safari', () => { |
| 150 | + it('should use the safari runner', async () => { |
| 151 | + await TestProject.simple().registerWxt(command, { |
| 152 | + browser: 'safari', |
| 153 | + }); |
| 154 | + |
| 155 | + expect(wxt.config.runner).toBe(safariRunner); |
| 156 | + }); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
0 commit comments