|
| 1 | +import { parseAstAsync } from 'vite' |
| 2 | +import { describe, expect, test } from 'vitest' |
| 3 | +import { transformProxyExport } from './proxy-export' |
| 4 | +import { validateNonAsyncFunction } from './utils' |
| 5 | +import { transformWrapExport } from './wrap-export' |
| 6 | + |
| 7 | +describe(validateNonAsyncFunction, () => { |
| 8 | + // next.js's validation isn't entirely consistent. |
| 9 | + // for now we aim to make it at least as forgiving as next.js. |
| 10 | + |
| 11 | + const accepted = [ |
| 12 | + `export async function f() {}`, |
| 13 | + `export default async function f() {}`, |
| 14 | + `export const fn = async function fn() {}`, |
| 15 | + `export const fn = async () => {}`, |
| 16 | + `export const fn = async () => {}, fn2 = x`, |
| 17 | + `export const fn = x`, |
| 18 | + `export const fn = x({ x: y })`, |
| 19 | + `export const fn = x(async () => {})`, |
| 20 | + `export default x`, |
| 21 | + `const y = x; export { y }`, |
| 22 | + `export const fn = x(() => {})`, // rejected by next.js |
| 23 | + `export const testAction = actionClient.action(async () => { return { message: "Hello, world!" }; });`, |
| 24 | + ] |
| 25 | + |
| 26 | + const rejected = [ |
| 27 | + `export function f() {}`, |
| 28 | + `export default function f() {}`, |
| 29 | + `export const fn = function fn() {}`, |
| 30 | + `export const fn = () => {}`, |
| 31 | + `export const fn = x, fn2 = () => {}`, |
| 32 | + `export class Cls {}`, |
| 33 | + `export const Cls = class {}`, |
| 34 | + `export const Cls = class Foo {}`, |
| 35 | + ] |
| 36 | + |
| 37 | + test(transformWrapExport, async () => { |
| 38 | + const testTransform = async (input: string) => { |
| 39 | + const ast = await parseAstAsync(input) |
| 40 | + const result = transformWrapExport(input, ast, { |
| 41 | + runtime: (value, name) => |
| 42 | + `$$wrap(${value}, "<id>", ${JSON.stringify(name)})`, |
| 43 | + ignoreExportAllDeclaration: true, |
| 44 | + rejectNonAsyncFunction: true, |
| 45 | + }) |
| 46 | + return result.output.hasChanged() |
| 47 | + } |
| 48 | + |
| 49 | + for (const code of accepted) { |
| 50 | + await expect.soft(testTransform(code)).resolves.toBe(true) |
| 51 | + } |
| 52 | + for (const code of rejected) { |
| 53 | + await expect |
| 54 | + .soft(testTransform(code)) |
| 55 | + .rejects.toMatchInlineSnapshot( |
| 56 | + `[Error: unsupported non async function]`, |
| 57 | + ) |
| 58 | + } |
| 59 | + }) |
| 60 | + |
| 61 | + test(transformProxyExport, async () => { |
| 62 | + const testTransform = async (input: string) => { |
| 63 | + const ast = await parseAstAsync(input) |
| 64 | + const result = transformProxyExport(ast, { |
| 65 | + code: input, |
| 66 | + rejectNonAsyncFunction: true, |
| 67 | + runtime: (name) => `$$proxy("<id>", ${JSON.stringify(name)})`, |
| 68 | + }) |
| 69 | + return result.output.hasChanged() |
| 70 | + } |
| 71 | + |
| 72 | + for (const code of accepted) { |
| 73 | + await expect.soft(testTransform(code)).resolves.toBe(true) |
| 74 | + } |
| 75 | + for (const code of rejected) { |
| 76 | + await expect |
| 77 | + .soft(testTransform(code)) |
| 78 | + .rejects.toMatchInlineSnapshot( |
| 79 | + `[Error: unsupported non async function]`, |
| 80 | + ) |
| 81 | + } |
| 82 | + }) |
| 83 | +}) |
0 commit comments