Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow jest to run with use server directive #56148

Merged
merged 8 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/next-swc/crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ where
&file.name,
config.clone(),
comments,
opts.bundle_target.clone(),
)),
None => Either::Right(noop()),
},
Expand Down
11 changes: 9 additions & 2 deletions packages/next-swc/crates/core/src/server_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub fn server_actions<C: Comments>(
file_name: &FileName,
config: Config,
comments: C,
bundle_target: JsWord,
) -> impl VisitMut + Fold {
as_folder(ServerActions {
config,
Expand All @@ -52,6 +53,8 @@ pub fn server_actions<C: Comments>(
annotations: Default::default(),
extra_items: Default::default(),
export_actions: Default::default(),

bundle_target: bundle_target.to_string(),
})
}

Expand Down Expand Up @@ -81,6 +84,8 @@ struct ServerActions<C: Comments> {
annotations: Vec<Stmt>,
extra_items: Vec<ModuleItem>,
export_actions: Vec<String>,

bundle_target: String,
}

impl<C: Comments> ServerActions<C> {
Expand All @@ -103,9 +108,10 @@ impl<C: Comments> ServerActions<C> {
remove_directive,
&mut is_action_fn,
self.config.enabled,
self.bundle_target != "default",
);

if is_action_fn && !self.config.is_server {
if is_action_fn && !self.config.is_server && self.bundle_target != "default" {
HANDLER.with(|handler| {
handler
.struct_span_err(
Expand Down Expand Up @@ -1343,6 +1349,7 @@ fn remove_server_directive_index_in_fn(
remove_directive: bool,
is_action_fn: &mut bool,
enabled: bool,
should_error: bool,
) {
let mut is_directive = true;

Expand All @@ -1355,7 +1362,7 @@ fn remove_server_directive_index_in_fn(
if value == "use server" {
if is_directive {
*is_action_fn = true;
if !enabled {
if !enabled && should_error {
HANDLER.with(|handler| {
handler
.struct_span_err(
Expand Down
2 changes: 2 additions & 0 deletions packages/next-swc/crates/core/tests/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ fn react_server_actions_server_errors(input: PathBuf) {
enabled: true
},
tr.comments.as_ref().clone(),
String::from("default").into(),
)
)
},
Expand Down Expand Up @@ -214,6 +215,7 @@ fn react_server_actions_client_errors(input: PathBuf) {
enabled: true
},
tr.comments.as_ref().clone(),
String::from("default").into(),
)
)
},
Expand Down
2 changes: 2 additions & 0 deletions packages/next-swc/crates/core/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ fn server_actions_server_fixture(input: PathBuf) {
enabled: true
},
_tr.comments.as_ref().clone(),
String::from("default").into(),
)
)
},
Expand All @@ -457,6 +458,7 @@ fn server_actions_client_fixture(input: PathBuf) {
enabled: true
},
_tr.comments.as_ref().clone(),
String::from("default").into(),
)
)
},
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/app-dir/hello-world/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from 'react'

export default function Page() {
return <p>hello world</p>
}
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 function action(data) {
huozhi marked this conversation as resolved.
Show resolved Hide resolved
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')
})
Loading