forked from vitejs/vite-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.test.ts
More file actions
83 lines (76 loc) · 2.66 KB
/
utils.test.ts
File metadata and controls
83 lines (76 loc) · 2.66 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
import { parseAstAsync } from 'vite'
import { describe, expect, test } from 'vitest'
import { transformProxyExport } from './proxy-export'
import { validateNonAsyncFunction } from './utils'
import { transformWrapExport } from './wrap-export'
describe(validateNonAsyncFunction, () => {
// next.js's validation isn't entirely consistent.
// for now we aim to make it at least as forgiving as next.js.
const accepted = [
`export async function f() {}`,
`export default async function f() {}`,
`export const fn = async function fn() {}`,
`export const fn = async () => {}`,
`export const fn = async () => {}, fn2 = x`,
`export const fn = x`,
`export const fn = x({ x: y })`,
`export const fn = x(async () => {})`,
`export default x`,
`const y = x; export { y }`,
`export const fn = x(() => {})`, // rejected by next.js
`export const testAction = actionClient.action(async () => { return { message: "Hello, world!" }; });`,
]
const rejected = [
`export function f() {}`,
`export default function f() {}`,
`export const fn = function fn() {}`,
`export const fn = () => {}`,
`export const fn = x, fn2 = () => {}`,
`export class Cls {}`,
`export const Cls = class {}`,
`export const Cls = class Foo {}`,
]
test(transformWrapExport, async () => {
const testTransform = async (input: string) => {
const ast = await parseAstAsync(input)
const result = transformWrapExport(input, ast, {
runtime: (value, name) =>
`$$wrap(${value}, "<id>", ${JSON.stringify(name)})`,
ignoreExportAllDeclaration: true,
rejectNonAsyncFunction: true,
})
return result.output.hasChanged()
}
for (const code of accepted) {
await expect.soft(testTransform(code)).resolves.toBe(true)
}
for (const code of rejected) {
await expect
.soft(testTransform(code))
.rejects.toMatchInlineSnapshot(
`[Error: unsupported non async function]`,
)
}
})
test(transformProxyExport, async () => {
const testTransform = async (input: string) => {
const ast = await parseAstAsync(input)
const result = transformProxyExport(ast, {
code: input,
rejectNonAsyncFunction: true,
runtime: (name) => `$$proxy("<id>", ${JSON.stringify(name)})`,
})
return result.output.hasChanged()
}
for (const code of accepted) {
await expect.soft(testTransform(code)).resolves.toBe(true)
}
for (const code of rejected) {
await expect
.soft(testTransform(code))
.rejects.toMatchInlineSnapshot(
`[Error: unsupported non async function]`,
)
}
})
})