Skip to content

Jest cannot test "use server" file #53065

@mkizka

Description

@mkizka

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: linux
      Arch: x64
      Version: #1 SMP Fri Jan 27 02:56:13 UTC 2023
    Binaries:
      Node: 18.16.0
      npm: 9.5.1
      Yarn: 1.22.19
      pnpm: 8.5.1
    Relevant Packages:
      next: 13.4.12-canary.0
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 4.9.5
    Next.js Config:
      output: N/A


warn  - Latest canary version not detected, detected: "13.4.12-canary.0", newest: "13.4.12".
        Please try the latest canary version (`npm install next@canary`) to confirm the issue still exists before creating a new issue.

Which area(s) of Next.js are affected? (leave empty if unsure)

App Router, Jest (next/jest)

Link to the code that reproduces this issue or a replay of the bug

https://github.com/mkizka/nextjs-serveractions-jest

To Reproduce

git clone https://github.com/mkizka/nextjs-serveractions-jest
cd nextjs-serveractions-jest
yarn
yarn test

Describe the Bug

I got this message even though serverActions is true. This test passes if I remove "use server".

$ cat next.config.js
/** @type {import("next").NextConfig} */
module.exports = {
  reactStrictMode: true,
  experimental: {
    serverActions: true,
  },
};

$ yarn test
yarn run v1.22.19
$ jest
- warn You have enabled experimental feature (serverActions) in next.config.js.
- warn Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use at your own risk.

 FAIL  app/action.test.ts
  ● Test suite failed to run


      × To use Server Actions, please enable the feature flag in your Next.js config. Read more: https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions#convention
       ╭─[/home/ubuntu/nextjs-serveractions-jest/app/action.ts:1:1]
     1 │ "use server";
       · ────────────
     2 │
     3 │ export const action = () => {
     4 │   console.log("action called");
       ╰────

      3 | jest.spyOn(console, "log");
      4 |
    > 5 | test("action", () => {
        |                 ^
      6 |   action();
      7 |   expect(console.log).toHaveBeenCalledWith("action called");
      8 | });

      at Object.transformSync (node_modules/next/src/build/swc/index.ts:1000:25)
      at Object.<anonymous> (app/action.test.ts:5:17)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.15 s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Expected Behavior

The test succeeds.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

NEXT-1473

Metadata

Metadata

Assignees

Labels

TestingRelated to testing with Next.js.bugIssue was opened via the bug report template.linear: nextConfirmed issue that is tracked by the Next.js team.locked

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions