Unverified Commit 48093b87 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Allow jest to run with use server directive (#56148)

Disable server components and server actions SWC transform when it's running jest. You can only do basic DOM testing like what we described in #54891 instead the server action itself.

Closes #53065 
Closes NEXT-1473
parent 8f0f8236
...@@ -178,16 +178,16 @@ function getBaseSWCOptions({ ...@@ -178,16 +178,16 @@ function getBaseSWCOptions({
development development
), ),
}), }),
serverComponents: hasServerComponents serverComponents:
? { isServer: !!isServerLayer } hasServerComponents && !jest ? { isServer: !!isServerLayer } : undefined,
: undefined, serverActions:
serverActions: hasServerComponents hasServerComponents && !jest
? { ? {
// TODO-APP: When Server Actions is stable, we need to remove this flag. // TODO-APP: When Server Actions is stable, we need to remove this flag.
enabled: !!isServerActionsEnabled, enabled: !!isServerActionsEnabled,
isServer: !!isServerLayer, isServer: !!isServerLayer,
} }
: undefined, : undefined,
bundleTarget, bundleTarget,
} }
} }
......
'use server'
export async function action(data) {
console.log(data)
}
import { action } from './action'
export default function Page() {
return (
<button data-testid="log" onClick={action}>
log
</button>
)
}
/**
* @jest-environment jsdom
*/
import { render, screen } from '@testing-library/react'
import Page from './page'
it('works with client-only code', () => {
render(<Page />)
expect(screen.getByTestId('log')).toHaveTextContent('log')
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment