-
-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathserver-action.test.ts
More file actions
71 lines (66 loc) · 2.69 KB
/
Copy pathserver-action.test.ts
File metadata and controls
71 lines (66 loc) · 2.69 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
import { parseAstAsync } from 'vite'
import { describe, expect, it } from 'vitest'
import { transformServerActionServer } from './server-action'
const runtime = (value: string, name: string) =>
`wrap(${value}, ${JSON.stringify(name)})`
describe(transformServerActionServer, () => {
it('supports custom inline directives and runtimes', async () => {
const input = `async function cached() { "use cache" }`
const ast = await parseAstAsync(input)
const result = transformServerActionServer(input, ast, {
runtime,
directive: 'use cache',
inlineRuntime: (value, name) =>
`cache(${value}, ${JSON.stringify(name)})`,
})
expect(result.output.toString()).toContain('cache($$hoist_0_cached')
})
it('supports explicit module directives, filtering, and validation', async () => {
const input = `"use cache"; export const metadata = 1; export async function cached() {}`
const ast = await parseAstAsync(input)
const directive = ast.body[0]!
expect(directive.type).toBe('ExpressionStatement')
if (
directive.type !== 'ExpressionStatement' ||
directive.expression.type !== 'Literal'
)
throw new Error('expected directive')
const result = transformServerActionServer(input, ast, {
runtime,
moduleDirective: directive.expression,
moduleRuntime: (value, name) =>
`cache(${value}, ${JSON.stringify(name)})`,
filter: (name) => name !== 'metadata',
rejectNonAsyncModule: true,
})
expect(result.output.toString()).toContain('/* "use cache" */')
expect(result.output.toString()).toContain('cache(cached, "cached")')
expect(result.output.toString()).not.toContain('cache(metadata')
})
it('can preserve explicit module directives', async () => {
const input = `"use cache"; export async function cached() {}`
const ast = await parseAstAsync(input)
const statement = ast.body[0]!
if (
statement.type !== 'ExpressionStatement' ||
statement.expression.type !== 'Literal'
)
throw new Error('expected directive')
const result = transformServerActionServer(input, ast, {
runtime,
moduleDirective: statement.expression,
preserveModuleDirective: true,
})
expect(result.output.toString()).toContain('"use cache"')
})
it('can disable built-in use server module detection', async () => {
const input = `"use server"; export async function action() {}`
const ast = await parseAstAsync(input)
const result = transformServerActionServer(input, ast, {
runtime,
detectUseServerModule: false,
})
expect('names' in result ? result.names : result.exportNames).toEqual([])
expect(result.output.hasChanged()).toBe(false)
})
})