|
| 1 | +import path from 'node:path' |
| 2 | +import { readFileSync } from 'node:fs' |
| 3 | +import { describe, expect, it } from 'vitest' |
| 4 | +import { getConfig } from '../src' |
| 5 | +import type { Config } from '../src' |
| 6 | + |
| 7 | +describe('getConfig', () => { |
| 8 | + const testCases = [ |
| 9 | + { |
| 10 | + name: 'inline config', |
| 11 | + inlineConfig: { |
| 12 | + target: 'solid', |
| 13 | + autoCodeSplitting: false, |
| 14 | + enableRouteGeneration: true, |
| 15 | + } as Partial<Config>, |
| 16 | + withJson: false, |
| 17 | + }, |
| 18 | + { |
| 19 | + name: `inline config and relative paths`, |
| 20 | + inlineConfig: { |
| 21 | + target: 'solid', |
| 22 | + autoCodeSplitting: false, |
| 23 | + routesDirectory: 'src/paths', |
| 24 | + generatedRouteTree: 'src/tree/routeTree.gen.ts', |
| 25 | + enableRouteGeneration: true, |
| 26 | + } as Partial<Config>, |
| 27 | + withJson: false, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: `inline config and "./" form relative paths`, |
| 31 | + inlineConfig: { |
| 32 | + target: 'solid', |
| 33 | + autoCodeSplitting: true, |
| 34 | + routesDirectory: './src/paths', |
| 35 | + generatedRouteTree: './src/tree/routeTree.gen.ts', |
| 36 | + enableRouteGeneration: true, |
| 37 | + } as Partial<Config>, |
| 38 | + withJson: false, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: 'inline config and absolute paths', |
| 42 | + inlineConfig: { |
| 43 | + target: 'solid', |
| 44 | + autoCodeSplitting: false, |
| 45 | + routesDirectory: '/src/paths', |
| 46 | + generatedRouteTree: '/src/tree/routeTree.gen.ts', |
| 47 | + enableRouteGeneration: true, |
| 48 | + } as Partial<Config>, |
| 49 | + withJson: false, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: 'json config', |
| 53 | + inlineConfig: {} as Partial<Config>, |
| 54 | + withJson: true, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: 'combination of json and inline config', |
| 58 | + inlineConfig: { |
| 59 | + target: 'react', |
| 60 | + autoCodeSplitting: true, |
| 61 | + routesDirectory: './src/paths', |
| 62 | + generatedRouteTree: './src/tree/routeTree.gen.ts', |
| 63 | + enableRouteGeneration: true, |
| 64 | + } as Partial<Config>, |
| 65 | + withJson: true, |
| 66 | + }, |
| 67 | + ] |
| 68 | + |
| 69 | + it.each(testCases)('must resolve $name', ({ inlineConfig, withJson }) => { |
| 70 | + const rootPath = withJson ? 'withJson' : 'withoutJson' |
| 71 | + const root = path.resolve(import.meta.dirname, 'config', rootPath) |
| 72 | + |
| 73 | + const jsonConfig = withJson |
| 74 | + ? JSON.parse(readFileSync(path.resolve(root, 'tsr.config.json'), 'utf-8')) |
| 75 | + : undefined |
| 76 | + |
| 77 | + const routesPath = |
| 78 | + inlineConfig.routesDirectory ?? |
| 79 | + jsonConfig?.routesDirectory ?? |
| 80 | + 'src/routes' |
| 81 | + |
| 82 | + const routeTreePath = |
| 83 | + inlineConfig.generatedRouteTree ?? |
| 84 | + jsonConfig?.generatedRouteTree ?? |
| 85 | + 'src/routeTree.gen.ts' |
| 86 | + |
| 87 | + const routesDirectory = path.resolve( |
| 88 | + import.meta.dirname, |
| 89 | + 'config', |
| 90 | + rootPath, |
| 91 | + routesPath, |
| 92 | + ) |
| 93 | + |
| 94 | + const generatedRouteTree = path.resolve( |
| 95 | + import.meta.dirname, |
| 96 | + 'config', |
| 97 | + rootPath, |
| 98 | + routeTreePath, |
| 99 | + ) |
| 100 | + |
| 101 | + const resolvedConfig = getConfig(inlineConfig, root) |
| 102 | + |
| 103 | + expect(resolvedConfig).toEqual( |
| 104 | + expect.objectContaining({ |
| 105 | + ...jsonConfig, |
| 106 | + ...inlineConfig, |
| 107 | + routesDirectory, |
| 108 | + generatedRouteTree, |
| 109 | + }), |
| 110 | + ) |
| 111 | + }) |
| 112 | +}) |
0 commit comments