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

coverage.thresholds.autoUpdate fails when defineConfig takes a function #4695

Closed
6 tasks done
wjperkins opened this issue Dec 7, 2023 · 2 comments · Fixed by #4959
Closed
6 tasks done

coverage.thresholds.autoUpdate fails when defineConfig takes a function #4695

wjperkins opened this issue Dec 7, 2023 · 2 comments · Fixed by #4959
Labels
feat: coverage Issues and PRs related to the coverage feature

Comments

@wjperkins
Copy link

Describe the bug

When using coverage.thresholds.autoUpdate in a config defined with a function, as shown in https://vitejs.dev/config/#conditional-config, e.g:

export default defineConfig((config) => {
  // do something with config...

  return {
    test: {
      coverage: {
        enabled: true,
        provider: 'istanbul',
        thresholds: {
          lines: 50,
          autoUpdate: true,
        },
      },
    },
  };
});

The tests run successfully, and the coverage report is generated. However, it fails to update the coverage threshold and returns an unhandled error MagicastError: Casting "ArrowFunctionExpression" is not supported.

If the config is defined with a simple object rather than a function, the autoUpdate behaviour works as expected.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-zwszje?file=vite.config.ts

System Info

System:                                                      
    OS: Windows 10 10.0.19045                                  
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
    Memory: 5.18 GB / 15.39 GB                                 
  Binaries:                                                    
    Node: 20.9.0 - C:\Program Files\nodejs\node.EXE            
    Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 10.1.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.3636.0), Chromium (119.0.2151.97)
    Internet Explorer: 11.0.19041.3636
  npmPackages:
    @vitejs/plugin-react: ^4.2.1 => 4.2.1
    @vitest/coverage-v8: ^1.0.1 => 1.0.1
    @vitest/ui: ^1.0.1 => 1.0.1
    vite: ^5.0.6 => 5.0.6
    vitest: ^1.0.1 => 1.0.1

Used Package Manager

npm

Validations

@AriPerkkio
Copy link
Member

Yep, this is not supported at the moment. Only a simple use case is covered here:

read: () => configModule.exports.default.$type === 'function-call'
? configModule.exports.default.$args[0]
: configModule.exports.default,

We should extend the support for all common patterns like this one. But anything more complex can get tricky to resolve based on AST.

@AriPerkkio AriPerkkio added bug feat: coverage Issues and PRs related to the coverage feature and removed pending triage labels Dec 7, 2023
@AriPerkkio
Copy link
Member

Magicast doesn't support arrow functions: unjs/magicast#37. We'll need to add those first.

import { parseModule } from "magicast";

const mod = parseModule(`
import { defineConfig } from "vitest/config";

export default defineConfig((config) => ({
  test: {
    coverage: {
      enabled: true,
      provider: "istanbul",
      thresholds: {
        autoUpdate: true,
        lines: 1,
      },
    },
  },
}));
`);

console.log(mod.exports.default.$args[0]);
> file:///root/repro/node_modules/.pnpm/[email protected]/node_modules/magicast/dist/index.mjs:8529
>       throw new MagicastError(`Casting "${node.type}" is not supported`, {
>             ^
> 
> MagicastError: Casting "ArrowFunctionExpression" is not supported
> 
>   2 | import { defineConfig } from "vitest/config";
>   3 | 
>   4 | export default defineConfig((config) => ({
>                                   ^
>   5 |   test: {
>   6 |     coverage: {
>   7 |       enabled: true,
> 
>     at proxify (file:///root/repro/node_modules/.pnpm/[email protected]/node_modules/magicast/dist/index.mjs:8529:13)
>     at Object.get (file:///root/repro/node_modules/.pnpm/[email protected]/node_modules/magicast/dist/index.mjs:8286:18)
>     at Object.get (file:///root/repro/node_modules/.pnpm/[email protected]/node_modules/magicast/dist/index.mjs:8036:26)
>     at file:///root/repro/magicast-repro.mjs:20:38

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: coverage Issues and PRs related to the coverage feature
Projects
None yet
2 participants