vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
10.33k stars 1.56k forks source link

Jest: cannot find module 'ai/rsc' #1649

Open mrasoahaingo opened 6 months ago

mrasoahaingo commented 6 months ago

Description

I tried to implement test using the generated app with

npx create-next-app@latest --example with-jest with-jest-app

Then I use the example here https://sdk.vercel.ai/docs/getting-started/nextjs-app-router

But when I launch

yarn test

I have this error

yarn test
 FAIL  app/page.test.tsx
  ● Test suite failed to run

    Cannot find module 'ai/rsc' from 'app/page.tsx'

    Require stack:
      app/page.tsx
      app/page.test.tsx

      12 |   return (
      13 |     <div className="flex flex-col w-full max-w-md py-24 mx-auto stretch">
    > 14 |       {data && <pre>{JSON.stringify(data, null, 2)}</pre>}
         |              ^
      15 |       {messages.map((m, i) => (
      16 |         <div key={i} className="whitespace-pre-wrap">
      17 |           {m.role === 'user' ? 'User: ' : 'AI: '}

      at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/resolver.js:427:11)
      at Object.<anonymous> (app/page.tsx:14:14)
      at Object.<anonymous> (app/page.test.tsx:9:54)

(node:3728) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.473 s
Ran all test suites related to changed files.

Watch Usage
 › Press a to run all tests.
 › Press f to run only failed tests.
 › Press p to filter by a filename regex pattern.
 › Press t to filter by a test name regex pattern.
 › Press q to quit watch mode.
 › Press Enter to trigger a test run.

✨  Done in 191.55s.

Thank you for your help

Code example

No response

Additional context

No response

Note: It works with the Vitest version

lekt9 commented 6 months ago

Getting the same issue here on plasmo

🔴 ERROR  | Build failed. To debug, run plasmo dev --verbose.
🔴 ERROR  | Failed to resolve 'ai/rsc' from './src/actions.tsx'
🔴 ERROR  | Cannot load file './rsc' from module 'ai'

Running plasmo@0.86.3, and tried ai version 3.0.0 to 3.0.2

dnehring commented 5 months ago

I was able to work-around the module resolution via jest config update in moduleNameMapper:

moduleNameMapper: { ... '^ai/rsc$': '<rootDir>/node_modules/ai/rsc/dist', }