|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | +import { defu } from 'defu' |
| 3 | +import { defaultOptions } from '../../src/module' |
| 4 | +import type { ModuleOptions } from '../../src/types' |
| 5 | + |
| 6 | +/** |
| 7 | + * Test to demonstrate the connector merging bug in module.ts |
| 8 | + * |
| 9 | + * When a user configures MySQL in runtimeConfig, defu merges it with defaultOptions |
| 10 | + * which contains SQLite settings, resulting in MySQL config having a 'path' property. |
| 11 | + */ |
| 12 | +describe('Module connector merging bug', () => { |
| 13 | + it('should demonstrate how defu mixes MySQL and SQLite configs (OLD BEHAVIOR)', () => { |
| 14 | + // Simulate what the user has in their nuxt.config.ts |
| 15 | + const userRuntimeConfig = { |
| 16 | + connector: { |
| 17 | + name: 'mysql' as const, |
| 18 | + options: { |
| 19 | + host: 'localhost', |
| 20 | + port: 3306, |
| 21 | + user: '', |
| 22 | + password: '', |
| 23 | + database: '' |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + // This is the OLD way (before the fix) - directly merging with defaultOptions |
| 29 | + const runtimeConfigOptions = defu(userRuntimeConfig, {}, defaultOptions) as ModuleOptions |
| 30 | + |
| 31 | + // BUG: The merged config INCORRECTLY has a path property for MySQL |
| 32 | + console.log('Merged connector options (old way):', runtimeConfigOptions.connector.options) |
| 33 | + |
| 34 | + // This demonstrates the bug - path should be undefined for MySQL but isn't |
| 35 | + expect(runtimeConfigOptions.connector.options.path).toBe('./data/users.sqlite3') |
| 36 | + }) |
| 37 | + |
| 38 | + it('should show the correct way to merge without mixing connectors', () => { |
| 39 | + const userRuntimeConfig = { |
| 40 | + connector: { |
| 41 | + name: 'mysql' as const, |
| 42 | + options: { |
| 43 | + host: 'localhost', |
| 44 | + port: 3306, |
| 45 | + user: '', |
| 46 | + password: '', |
| 47 | + database: '' |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // FIX: Exclude connector from defaults before merging |
| 53 | + const { connector: _defaultConnector, ...defaultsWithoutConnector } = defaultOptions |
| 54 | + const runtimeConfigOptions = defu(userRuntimeConfig, {}, defaultsWithoutConnector) as ModuleOptions |
| 55 | + |
| 56 | + // Use the configured connector (don't merge with default) |
| 57 | + runtimeConfigOptions.connector = userRuntimeConfig.connector || defaultOptions.connector |
| 58 | + |
| 59 | + // This should pass - MySQL config should not have SQLite path |
| 60 | + expect(runtimeConfigOptions.connector.options.path).toBeUndefined() |
| 61 | + expect(runtimeConfigOptions.connector.name).toBe('mysql') |
| 62 | + expect(runtimeConfigOptions.connector.options.host).toBe('localhost') |
| 63 | + }) |
| 64 | +}) |
0 commit comments