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

code-splitter-shared-split-target.test.ts

59 lines1.7 KB

1import { describe, expect, it, vi } from 'vitest'
2import {
3 compileRouteModules,
4 expectValidModules,
5 head,
6} from './regression-helpers'
7import type * as RouterUtils from '@tanstack/router-utils'
8​
9// `findReferencedIdentifiers` comes from babel-dead-code-elimination, which
10// can resolve its own copy of @babel/traverse (pnpm installs two when the
11// versions differ). The compiler's later `babel.traverse` then cannot reuse
12// the scope that pass cached. Clearing the cache after the pass behaves the
13// same way in a single-copy install.
14vi.mock('@tanstack/router-utils', async (importOriginal) => {
15 const actual = await importOriginal<typeof RouterUtils>()
16 const { traverse } = await import('@babel/core')
17 return {
18 ...actual,
19 findReferencedIdentifiers: (
20 ...args: Parameters<typeof actual.findReferencedIdentifiers>
21 ) => {
22 const refs = actual.findReferencedIdentifiers(...args)
23 traverse.cache.clear()
24 return refs
25 },
26 }
27})
28​
29describe('a split route option whose value is a shared binding', () => {
30 it.each(['notFoundComponent', 'errorComponent'])(
31 'exports the shared import as %s',
32 async (option) => {
33 const { modules, sharedBindings } = compileRouteModules(`${head}
34export const Route = createFileRoute('/posts/$id')({
35 component: PostRoute,
36 ${option}: Fallback,
37})
38​
39function Fallback() {
40 return <p>Fallback</p>
42​
43function PostRoute() {
44 const post = usePost()
45 if (!post) return <Fallback />
46 return <p>{post.title}</p>
48​
49declare function usePost(): { title: string } | undefined
50`)
51​
52 expect(sharedBindings).toEqual(['Fallback'])
53 expect(modules[`virtual ${option}`]).toContain(
54 `export { Fallback as ${option} }`,
55 )
56 await expectValidModules(modules)
57 },
58 )
59})