Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

utils.test.ts

51 lines1.5 KB

1import { afterEach, describe, expect, test, vi } from 'vitest'
2​
3const originalPlatform = Object.getOwnPropertyDescriptor(process, 'platform')
4​
5async function importUtilsWithPlatform(platform: NodeJS.Platform) {
6 vi.resetModules()
7 Object.defineProperty(process, 'platform', {
8 ...originalPlatform,
9 value: platform,
10 })
11​
12 return await import('../src/utils')
14​
15afterEach(() => {
16 vi.resetModules()
17 if (originalPlatform) {
18 Object.defineProperty(process, 'platform', originalPlatform)
19 }
20})
21​
22describe('normalizePath', () => {
23 test('normalizes POSIX path segments on POSIX platforms', async () => {
24 const { normalizePath } = await importUtilsWithPlatform('linux')
25​
26 expect(normalizePath('/app//src/../routes/index.tsx')).toBe(
27 '/app/routes/index.tsx',
28 )
29 })
30​
31 test('normalizes empty paths like path.posix.normalize', async () => {
32 const { normalizePath } = await importUtilsWithPlatform('linux')
33​
34 expect(normalizePath('')).toBe('.')
35 })
36​
37 test('keeps Windows separators as normal characters on POSIX platforms', async () => {
38 const { normalizePath } = await importUtilsWithPlatform('linux')
39 const input = String.raw`C:\app\src\..\routes\index.tsx`
40​
41 expect(normalizePath(input)).toBe(input)
42 })
43​
44 test('converts Windows separators before POSIX normalization on Windows', async () => {
45 const { normalizePath } = await importUtilsWithPlatform('win32')
46​
47 expect(normalizePath(String.raw`C:\app\src\..\routes\index.tsx`)).toBe(
48 'C:/app/routes/index.tsx',
49 )
50 })
51})