c8 icon indicating copy to clipboard operation
c8 copied to clipboard

"as const" is marked as uncovered in coverage reports

Open viveleroi opened this issue 2 years ago • 5 comments

In typescript, as const is being flagged as uncovered even though it's part of typescript and not testable as far as I'm aware.

export const QueryModeValues = ['button', 'live'] as const
  • Version: c8 7.12.0 with Vitest 0.29.2
  • Platform: Node 18.9.1 on Windows 11

Ignoring the line works fine but we'd prefer to not have to ignore these lines everywhere.

viveleroi avatar Mar 15 '23 21:03 viveleroi

@viveleroi c8 doesn't work with TS, because Node.js doesn't support TS. So, please provide .js source which is not covered, or complete setup (tsconfig, etc.) to reproduce it.

koshic avatar Mar 19 '23 16:03 koshic

It's marking the as const portion of this code snipped as "function not covered":

chrome_T4AMQ2uhcn

export const QueryModeValues = ['button', 'live'] as const
type QueryModes = (typeof QueryModeValues)[number]

We're using vite and configs are pretty standard, only real changes are the base url, paths, includes. Our tsconfig.json:

{
  "compilerOptions": {
    "target": "ESNext",
    "useDefineForClassFields": true,
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": false,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",
    "baseUrl": "app",
    "paths": {
      "@/*": ["./app/*"]
    }
  },
  "include": ["app", "./tests/components/setup.ts", ".storybook/*.ts"],
  "references": [{ "path": "./tsconfig.node.json" }]
}

viveleroi avatar Mar 19 '23 18:03 viveleroi

@viveleroi it's a bit funny because you have 'noEmit: true' in tsconfig ) Sourcemaps are not enabled too. Ok, lets add all necessary things:

{
  "compilerOptions": {
    "target": "ESNext",
    "useDefineForClassFields": true,
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": false,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "react-jsx",
    "sourceMap": true
  }
}

Then create 1.ts:

export const QueryModeValues = ["button", "live"] as const;

// some code to ensure that source maps are avalid
const x: string = "lalala";

console.log(x);

Run 'yarn tsc && yarn c8 --all --include '*.js' --reporter text --reporter html node 1.js' (latest package versions, latest node)

Results: image image

100% coverage, as you can see

PS forget to use '--enable-source-maps' node option because I have it enabled globally.

koshic avatar Mar 19 '23 18:03 koshic

I don't appreciate being told my issue is funny, these configs are essentially unchanged (paths only, I didn't touch emit) from what vite created so don't act like this is my mistake. Vitest is our test runner and they're using c8 for coverage, I'm not going to run this tool outside of those packages.

viveleroi avatar Mar 19 '23 19:03 viveleroi

Sure, I mean only provided configuration is funny, not the problem itself - which is may be very annoying and frustrating.

BTW, we found one fact: in given setup c8 works correctly with 'as const' statements. Obvious assumption - the issue is somewhere between your code and c8.

You can try to create vitest issue, or provide minimal repo here. Without your particular setup it's impossible to find issue 'somewhere around vitest'.

koshic avatar Mar 19 '23 19:03 koshic