vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
13.16k stars 1.19k forks source link

Vitest Runs Tests in node_modules, despite node_modules being excludedI #5943

Closed jwalkerinterpres closed 5 months ago

jwalkerinterpres commented 5 months ago

Describe the bug

I have the following test section in my vite.config.ts (the "exclude" section is the key part):

  test: {
      environment: 'jsdom',
      exclude: ['e2e/*', 'node_modules/*'],
      globals: true,
      // Fix for React Testing Library + Vite Test
      // @see https://stackoverflow.com/questions/77611978/invalid-chai-property-in-vitest
      setupFiles: ['./setupTests.ts']
      // The above just does: import '@testing-library/jest-dom';
  },

However, despite the above exclude, tests in my e2e and node_modules folders are run.

Example Tests (from excluded folders):

 ❯ e2e/tests/aichat.spec.ts (0)
 ❯ e2e/tests/campaigns.spec.ts (0)
 ❯ e2e/tests/techniques.spec.ts (0)
 ❯ node_modules/graphiql/src/components/__tests__/GraphiQL.spec
.tsx (31)

Example failure (originating in node_modules)

    at StorageContextProvider (file:///Users/jeremywalker/new-ui/interpres-new-ui/node_modules/@graphiql/react/dist/index.mjs:42:13)
    at GraphiQLProvider (file:///Users/jeremywalker/new-ui/interpres-new-ui/node_modules/@graphiql/react/dist/index.mjs:2893:13)
    at GraphiQL (/Users/jeremywalker/new-ui/interpres-new-ui/node_modules/graphiql/src/components/GraphiQL.tsx:18:3)
..

(There are lots more of the above.)

Clearly:

  exclude: ['e2e/*', 'node_modules/*'],

is not being applied.

Reproduction

Since this doesn't seem to have anything to do with my test files (the whole problem is that it's runing files besides mine), and I've provided my vite config, I'm not sure what else I can offer to help with reproduction, but I'd be happy to provide anything else.

System Info

System:
    OS: macOS 14.5
    CPU: (10) arm64 Apple M1 Max
    Memory: 273.34 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 21.2.0 - /usr/local/bin/node
    Yarn: 1.22.21 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
  Browsers:
    Chrome: 126.0.6478.62
    Edge: 126.0.2592.61
    Safari: 17.5
  npmPackages:
    @vitejs/plugin-react: ^4.2.0 => 4.2.0 
    @vitest/coverage-v8: ^0.34.6 => 0.34.6 
    vite: ^5.0.3 => 5.0.3 
    vitest: ^0.34.6 => 0.34.6

Used Package Manager

npm

Validations

sheremet-va commented 5 months ago

However, despite the above exclude, tests in my e2e and node_modules folders are run.

You have an error in you configuration. node_modules/* excludes only files in the first level - https://www.digitalocean.com/community/tools/glob?comments=true&glob=node_modules%2F%2A&matches=false&tests=node_modules%2Fgraphiql%2Fsrc%2Fcomponents%2F__tests__%2FGraphiQL.spec.

When **, the "globstar", is used it matches zero or more directories and subdirectories. This allows for recursive directory searching easily.

Default pattern correctly excludes node_modules: https://vitest.dev/config/#exclude

['**/node_modules/**', '**/dist/**', '**/cypress/**', '**/.{idea,git,cache,output,temp}/**', '**/{karma,rollup,webpack,vite,vitest,jest,ava,babel,nyc,cypress,tsup,build}.config.*']