oedotme / generouted

Generated file-based routes for Vite
https://stackblitz.com/github.com/oedotme/generouted/tree/main/explorer
MIT License
1.02k stars 47 forks source link

Vitest error when colocating .test.tsx file with component #137

Closed henrikvilhelmberglund closed 9 months ago

henrikvilhelmberglund commented 9 months ago

Describe the Bug

I would like to colocate my test files with my components but upon doing so I get a Vitest error saying it is not defined or something. This is a reasonable error because I'm running Vitest with globals: true to not have to import everything everywhere. The problem is that the .test.tsx file is run in the DOM when it's really only there for Vitest.

Is there a way to avoid running test files in the DOM somehow?

Generouted Version

v1.16.1

Your Example Website or App or Reproduction

https://stackblitz.com/github/henrikvilhelmberglund/react-vite-generouted-unocss-typescript-vitest-template/tree/colocate-tests?file=README.md

Steps to Reproduce the Bug or Issue

  1. Open repo. Result: Site doesn't load. (error in browser console)

Expected Behavior

Expected: site works fine. After moving the .test.tsx file to /tests the site works again.

Screenshots or Videos

No response

Platform

Win 10 64bit with latest Chrome/Brave

Additional context

No response

henrikvilhelmberglund commented 9 months ago

After using a custom routes.tsx with "!/src/pages/**/[\\w[-]*.test.{jsx,tsx}" in ROUTES it works again. 😊

henrikvilhelmberglund commented 9 months ago

I can't get it to work using the source options though (don't really want a custom routes.tsx), is this supposed to replace the default routes?

generouted({
      source: {
        routes: [
          "./src/pages/**/[\\w[-]*.{jsx,tsx}",
          "!**/(_app|404).*",
          "!./src/pages/**/[\\w[-]*.test.{jsx,tsx}",
        ],
        modals: "./src/pages/**/[+]*.{jsx,tsx}",
      },
    }),
oedotme commented 9 months ago

@henrikvilhelmberglund generouted Vite plugin option are specific to the type generation at router.ts. Also I checked your example and all seems to be working as expected.

henrikvilhelmberglund commented 9 months ago

@henrikvilhelmberglund generouted Vite plugin option are specific to the type generation at router.ts. Also I checked your example and all seems to be working as expected.

Ah ok, I will just use the custom router file then.

What do you mean that it works as expected though? The repo above (specifically the colocate-tests branch) shouldn't display anything.

oedotme commented 9 months ago

I mean the routing and type generation are both working as expecting with excluding the .test.tsx files from routing while co-locating them at src/pages.

I believe the problem with the tests has nothing to do with the routing or generouted. Also for the vitest config at include option, it doesn't include tsx files, after including them the tests ran for .tsx files.

henrikvilhelmberglund commented 9 months ago

If it's not generouted, what is making the file run in the browser then? Since both moving the test file out of /src and adding "!/src/pages/**/[\\w[-]*.test.{jsx,tsx}" in ROUTES fixes it made me suspect generouted. Interesting 😱

oedotme commented 9 months ago

Thanks for clarifying, by default any .tsx would be used by generouted unless you exclude it as you just mentioned.

henrikvilhelmberglund commented 9 months ago

Excluding it works fine. Thanks for taking a look! 😊