remix-run / remix

Build Better Websites. Create modern, resilient user experiences with web fundamentals.
https://remix.run
MIT License
29.79k stars 2.51k forks source link

Test setup/utils files importing server module gives: Server-only module referenced by client #9871

Open kentcdodds opened 2 months ago

kentcdodds commented 2 months ago

Reproduction

git clone https://github.com/epicweb-dev/epic-stack.git
cd epic-stack
git checkout vitest-config
npm install
npm run test

System Info

System:
    OS: macOS 14.5
    CPU: (10) arm64 Apple M1 Max
    Memory: 2.20 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.15.0 - ~/n/bin/node
    Yarn: 1.22.17 - ~/n/bin/yarn
    npm: 10.8.1 - ~/n/bin/npm
    pnpm: 8.15.6 - /opt/homebrew/bin/pnpm
    bun: 1.1.10 - ~/.bun/bin/bun
  Browsers:
    Brave Browser: 127.1.68.137
    Chrome: 127.0.6533.120
    Safari: 17.5

Used Package Manager

npm

Expected Behavior

I expect the tests to run without issue

Actual Behavior

I get the following error:

 FAIL  app/routes/users+/$username.test.tsx [ app/routes/users+/$username.test.tsx ]
Error: Server-only module referenced by client

    '#app/utils/env.server.ts' imported by 'tests/setup/setup-test-env.ts'

  See https://remix.run/docs/en/main/guides/vite#splitting-up-client-and-server-code

Test files are a special case.

I'm thinking anything out of the app directory should be ignored by this module import check. That would resolve my issue.

kentcdodds commented 2 months ago

Added context, I'm trying to move my vitest.config.ts into the vite.config.ts so we can share config between tests and the app (seems like a good idea). https://github.com/epicweb-dev/epic-stack/pull/822

This person had a similar issue: https://github.com/remix-run/remix/issues/8367#issuecomment-1875398995 however their solution was to disable the remix plugin during tests which I think is not a good idea since we want our tests to resemble our running application as much as possible.

kentcdodds commented 1 month ago

I've temporarily disabled the remix plugin in vite during testing, but I would definitely prefer to not have to do that.