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

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

Open
kentcdodds opened this issue Aug 15, 2024 · 2 comments

Comments

@kentcdodds
Copy link
Member

kentcdodds commented Aug 15, 2024

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
Copy link
Member Author

kentcdodds commented Aug 15, 2024

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). epicweb-dev/epic-stack#822

This person had a similar issue: #8367 (comment) 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
Copy link
Member Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants