-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcreateMiddleware.test.ts
More file actions
161 lines (144 loc) 路 4.64 KB
/
Copy pathcreateMiddleware.test.ts
File metadata and controls
161 lines (144 loc) 路 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { readFile, readdir } from 'node:fs/promises'
import path from 'node:path'
import { describe, expect, test, vi } from 'vitest'
import { StartCompiler } from '../../src/start-compiler/compiler'
// Default test options for StartCompiler
function getDefaultTestOptions(env: 'client' | 'server') {
const envName = env === 'client' ? 'client' : 'ssr'
return {
envName,
root: '/test',
framework: 'react' as const,
providerEnvName: 'ssr',
}
}
async function getFilenames() {
return await readdir(path.resolve(import.meta.dirname, './test-files'))
}
async function compile(opts: {
env: 'client' | 'server'
code: string
id: string
}) {
const compiler = new StartCompiler({
...opts,
...getDefaultTestOptions(opts.env),
loadModule: async () => {
// do nothing in test
},
lookupKinds: new Set(['Middleware']),
lookupConfigurations: [
{
libName: `@tanstack/react-start`,
rootExport: 'createMiddleware',
kind: 'Root',
},
{
libName: `@tanstack/react-start`,
rootExport: 'createStart',
kind: 'Root',
},
],
getKnownServerFns: () => ({}),
resolveId: async (id) => {
return id
},
})
const result = await compiler.compile({
code: opts.code,
id: opts.id,
})
return result
}
describe('createMiddleware compiles correctly', async () => {
const filenames = await getFilenames()
describe.each(filenames)('should handle "%s"', async (filename) => {
const file = await readFile(
path.resolve(import.meta.dirname, `./test-files/${filename}`),
)
const code = file.toString()
// Note: Middleware compilation only happens on the client
test(`should compile for ${filename} client`, async () => {
const result = await compile({ env: 'client', code, id: filename })
await expect(result!.code).toMatchFileSnapshot(
`./snapshots/client/${filename}`,
)
})
})
test('should use fast path for direct imports from known library (no extra resolveId calls)', async () => {
const code = `
import { createMiddleware } from '@tanstack/react-start'
const myMiddleware = createMiddleware().server(async ({ next }) => {
return next()
})`
const resolveIdMock = vi.fn(async (id: string) => id)
const compiler = new StartCompiler({
env: 'client',
...getDefaultTestOptions('client'),
loadModule: async () => {},
lookupKinds: new Set(['Middleware']),
lookupConfigurations: [
{
libName: '@tanstack/react-start',
rootExport: 'createMiddleware',
kind: 'Root',
},
],
getKnownServerFns: () => ({}),
resolveId: resolveIdMock,
})
await compiler.compile({
code,
id: 'test.ts',
})
// Direct known-library imports use the knownRootImports fast path, so they
// do not need resolveId.
expect(resolveIdMock).not.toHaveBeenCalled()
})
test('should use slow path for factory pattern (resolveId called for import resolution)', async () => {
// This simulates a factory pattern where createMiddleware is re-exported from a local file
const factoryCode = `
import { createFooMiddleware } from './factory'
const myMiddleware = createFooMiddleware().server(async ({ next }) => {
return next()
})`
const resolveIdMock = vi.fn(async (id: string) => id)
const compiler = new StartCompiler({
env: 'client',
...getDefaultTestOptions('client'),
loadModule: async (id) => {
// Simulate the factory module being loaded
if (id === './factory') {
compiler.ingestModule({
code: `
import { createMiddleware } from '@tanstack/react-start'
export const createFooMiddleware = createMiddleware
`,
id: './factory',
})
}
},
lookupKinds: new Set(['Middleware']),
lookupConfigurations: [
{
libName: '@tanstack/react-start',
rootExport: 'createMiddleware',
kind: 'Root',
},
],
getKnownServerFns: () => ({}),
resolveId: resolveIdMock,
})
await compiler.compile({
code: factoryCode,
id: 'test.ts',
})
// resolveId should only be called for './factory'. Direct known-library
// imports use the knownRootImports fast path.
//
// Note: The factory module's import from '@tanstack/react-start' ALSO uses
// the fast path (knownRootImports), so no additional resolveId call is needed there.
expect(resolveIdMock).toHaveBeenCalledTimes(1)
expect(resolveIdMock).toHaveBeenNthCalledWith(1, './factory', 'test.ts')
})
})