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

waku processes .test.ts files during development, causing issues with bun:test #1264

Open
mrsekut opened this issue Feb 24, 2025 · 8 comments
Labels
help wanted Extra attention is needed

Comments

@mrsekut
Copy link

mrsekut commented Feb 24, 2025

Description

While using waku with bun, I noticed that .test.ts files are being processed when running bun run dev. This leads to an error if the test files use bun:test, as Vite attempts to resolve it during import analysis.

However, test files are typically not needed during runtime and should not be loaded in the first place. This behavior suggests that vite is unintentionally including .test.ts files when resolving dependencies.

This is not a critical issue, as switching from bun:test to vitest resolves the problem. I wanted to share this behavior in case it is unintended and might be worth addressing.


Steps to Reproduce

  1. In a waku + bun project, create a test file (e.g., sample.test.ts):
    import { expect, test } from 'bun:test';
    
    test('sample test', async () => {
      expect(1).toBe(1);
    });
  2. Run:
    bun run dev
    
  3. The following error occurs:
    Error: Failed to resolve import "bun:test" from "src/sample.test.ts". Does the file exist?
    

Expected Behavior

  • .test.ts files should not be processed during development server startup, as they are only needed in test environments.
  • vite should not attempt to resolve bun:test during the import-analysis phase.

Possible Causes

  • vite's optimizeDeps.entries might be unintentionally including .test.ts files.
  • The processModule function in dev-server-impl.js could be processing .test.ts files when it shouldn't.
  • The willBeHandled logic might not be properly filtering out .test.ts files.

Additional Notes

  • Switching to vitest instead of bun:test resolved the issue, so this is not blocking my work.
  • This issue may not affect many users, but I wanted to share it in case it's an unintended behavior that could be improved.

Let me know if you need any more details. Thanks!

@dai-shi
Copy link
Member

dai-shi commented Feb 24, 2025

Thanks for reporting! The possible causes sound like a good hint.
I'm interested in what the root cause is.

Would anyone like to dig into it?

@dai-shi dai-shi added the help wanted Extra attention is needed label Feb 25, 2025
@hamlim
Copy link
Contributor

hamlim commented Feb 25, 2025

I might be able to take a look at this sometime this week!

@hamlim
Copy link
Contributor

hamlim commented Feb 25, 2025

Oddly, I don't see this error:

Error: Failed to resolve import "bun:test" from "src/sample.test.ts". Does the file exist?

When trying to replicate this issue.

I created a reproduction repo here: https://github.com/hamlim/waku-bun-test-bug-reproduction

The app seemed to work fine in dev mode when the test file was only within src, moving it into src/pages seemed to throw an error when loading the app however.

@mrsekut could you possibly create a simplified reproduction and share it? (or see what might be different between mine linked above and yours?)

@hamlim
Copy link
Contributor

hamlim commented Mar 13, 2025

@scottykaye and I were looking into this issue the other day, but haven't made much progress, outlining the things we've tried here in case others have any ideas!

  • Attempt to exclude in vite configs using esbuild.exclude
  • Attempt to ignore in vite configs using optimizeDeps.entries pattern that is prefixed with !, e.g. !**/*.test.*
  • Attempt to ignore in vite configs using build.rollupOptions.exclude (I think this is right - could be wrong)

Our hunch is that we want to configure any vite process to ignore any *.test.* file (may also need to expand to capture other patterns like *.spec.* as well, but testing just with .test for the time being), so far we haven't found an easy way to manage that though.

Maybe there are plugins that are implicitly including .test. files?

Short term also - I think if the test file is moved to an __tests__/ directory, I believe Vite will ignore it

@dai-shi
Copy link
Member

dai-shi commented Mar 14, 2025

Just to confirm, is this dev only issue? aka waku dev?

@hamlim
Copy link
Contributor

hamlim commented Mar 14, 2025

I haven't tested build yet, I'd assume it runs into the same issue though

@scottykaye
Copy link

I ran it using both dev and build commands and the issue persists for both environments.

@mrsekut
Copy link
Author

mrsekut commented Mar 14, 2025

@mrsekut could you possibly create a simplified reproduction and share it? (or see what might be different between mine linked above and yours?)

Sorry for the delayed response—I didn’t notice the mention earlier.

I tried creating a new waku project from scratch, and I couldn't reproduce the issue in that case. However, when I tested the original project where the bug occurred, I was still able to reproduce the issue.

To help with debugging, I stripped down the problematic project to a minimal reproducible example:
https://github.com/mrsekut/waku-bun-test-bug-reproduction
(test is in src/features/.)

Steps to reproduce:

  1. Clone the repository
  2. Run bun install
  3. Run bun run dev
  4. Open localhost in the browser

The bug still occurs in this setup.

I haven’t yet investigated the exact differences between this and a fresh project, but this might help in pinpointing the root cause.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants