Skip to content

Commit

Permalink
Allow jest to run with use server directive (#56148)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
huozhi committed Sep 28, 2023
1 parent 8f0f823 commit 48093b8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
20 changes: 10 additions & 10 deletions packages/next/src/build/swc/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,16 @@ function getBaseSWCOptions({
development
),
}),
serverComponents: hasServerComponents
? { isServer: !!isServerLayer }
: undefined,
serverActions: hasServerComponents
? {
// TODO-APP: When Server Actions is stable, we need to remove this flag.
enabled: !!isServerActionsEnabled,
isServer: !!isServerLayer,
}
: undefined,
serverComponents:
hasServerComponents && !jest ? { isServer: !!isServerLayer } : undefined,
serverActions:
hasServerComponents && !jest
? {
// TODO-APP: When Server Actions is stable, we need to remove this flag.
enabled: !!isServerActionsEnabled,
isServer: !!isServerLayer,
}
: undefined,
bundleTarget,
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/production/jest/rsc/app/server-action/action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use server'

export async function action(data) {
console.log(data)
}
9 changes: 9 additions & 0 deletions test/production/jest/rsc/app/server-action/page.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { action } from './action'

export default function Page() {
return (
<button data-testid="log" onClick={action}>
log
</button>
)
}
10 changes: 10 additions & 0 deletions test/production/jest/rsc/app/server-action/page.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @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')
})

0 comments on commit 48093b8

Please sign in to comment.