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

hoisted does not work with istanbul coverage when overriding coverage excludes #6057

Open
6 tasks done
kwojcik opened this issue Jul 8, 2024 · 5 comments · May be fixed by #6184
Open
6 tasks done

hoisted does not work with istanbul coverage when overriding coverage excludes #6057

kwojcik opened this issue Jul 8, 2024 · 5 comments · May be fixed by #6184
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@kwojcik
Copy link

kwojcik commented Jul 8, 2024

Describe the bug

If you use istanbul coverage (required for browser mode), override the coverage excludes, and use vi.hoisted, then your test runs fail with cryptic syntax errors.

This worked on v2.0.0-beta.12 and is broken on v2.0.1.

 FAIL  test/basic.test.tsx [ test/basic.test.tsx ]
SyntaxError: Unexpected token ')'
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯

 Test Files  1 failed (1)
      Tests  no tests
   Start at  12:06:27
   Duration  0ms
export default defineConfig({
  test: {
    coverage: {
      provider: "istanbul",
      exclude: [],
    },
  },
});
import { vi } from "vitest";

const mocks = vi.hoisted(() => ({}));

Reproduction

https://github.com/kwojcik/vitestsourcemapbug/tree/hoistedBug

git checkout hoistedBug
npm install
npm run test

System Info

System:
    OS: macOS 13.6.7
    CPU: (12) arm64 Apple M2 Max
    Memory: 11.03 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.18.2 - ~/.asdf/installs/nodejs/18.18.2/bin/node
    Yarn: 1.22.21 - /opt/homebrew/bin/yarn
    npm: 9.8.1 - ~/.asdf/plugins/nodejs/shims/npm
    pnpm: 9.4.0 - /opt/homebrew/bin/pnpm
    Watchman: 2024.04.01.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 126.0.6478.127
    Safari: 16.6
  npmPackages:
    @vitest/coverage-istanbul: ^2.0.1 => 2.0.1
    @vitest/coverage-v8: ^2.0.1 => 2.0.1
    @vitest/ui: ^2.0.1 => 2.0.1
    vite: latest => 5.3.2
    vitest: ^2.0.1 => 2.0.1

Used Package Manager

npm

Validations

@kwojcik
Copy link
Author

kwojcik commented Jul 8, 2024

Found the workaround, you need to include coverageConfigDefaults.exclude. I was previously including configDefaults.exclude.

      exclude: ['stuff/*', ...coverageConfigDefaults.exclude],

@sheremet-va
Copy link
Member

It is expected that coverage picks up a test file if you override exclude because test files are not forcefully excluded anymore: #5997 (they excluded only if exclude is not overridden)

It is unexpected that it throws an error in vi.hoisted though.

@MKhasib
Copy link

MKhasib commented Jul 8, 2024

@sheremet-va How about excluding .vue files imported in .js test files from coverage report? how can we do that?
Please check this for details:
#6058 (comment)

@AriPerkkio
Copy link
Member

AriPerkkio commented Jul 9, 2024

It's expected that this file is instrumented when you disable all coverage.excludes and use the default coverage.includes: ['**'].

The reason why this errors comes up is that vitest:mocks plugin has bug:

return hoistMocks(code, id, this.parse)

// input
import { vi } from "vitest";
const mocks = (cov_29w8m57srg().s[0]++, vi.hoisted(() => {
  cov_29w8m57srg().f[0]++;
  cov_29w8m57srg().s[1]++;
  return {};
}));
// output
const mocks = (cov_29w8m57srg().s[0]++, );

@AriPerkkio AriPerkkio added p2-edge-case Bug, but has workaround or limited in scope (priority) and removed pending triage labels Jul 9, 2024
AriPerkkio added a commit to AriPerkkio/vitest that referenced this issue Jul 21, 2024
@AriPerkkio AriPerkkio linked a pull request Jul 21, 2024 that will close this issue
6 tasks
AriPerkkio added a commit to AriPerkkio/vitest that referenced this issue Jul 21, 2024
@Doeke
Copy link

Doeke commented Aug 12, 2024

In my case I ran into this issue of hoisted causing SyntaxError when running with istanbul coverage after updating my dependencies. What worked for me was adding test files to my existing test.coverage.exclude:

exclude: [..., '**/*.test.ts'],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants