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

Tests report does not cover exported function from typescript. #3268

Closed
6 tasks done
nicolaefilat opened this issue Apr 28, 2023 · 3 comments
Closed
6 tasks done

Tests report does not cover exported function from typescript. #3268

nicolaefilat opened this issue Apr 28, 2023 · 3 comments
Labels
feat: coverage Issues and PRs related to the coverage feature

Comments

@nicolaefilat
Copy link

Describe the bug

I have a file called utils.ts in the folder src/logic/utils.ts that has a simple function that adds three numbers.

export function addThreeNumbers(a: number, b: number, c: number) {
    return a + b + c;
}

in the folder src/__tests__/unit/utils.test.ts I have a test for this sum function.

import { describe, it, expect } from 'vitest'
import {addThreeNumbers} from "src/logic/utils";


describe("Sum of three numbers test", () =>{
    it("1 + 2 + 3 = 6",()=>{
        expect(addThreeNumbers(1,2,3)).toBe(6)
    })
})

My vitest.config.ts file looks like this

export default mergeConfig(
    viteConfig,
    defineConfig({
        test: {
            environment: 'jsdom',
            exclude: [...configDefaults.exclude, 'e2e/*'],
            root: fileURLToPath(new URL('./', import.meta.url)),
            coverage: {
                all: true,
                provider: 'c8',
                // move coverage report to ./tests/unit/coverage
                reportsDirectory: './tests/unit/coverage',
                include: ['src/**/*.vue', "src/**/*.ts", "src/**/*.js"],
                exclude: ['src/router', "**/__tests__/**"]
            },
        }
    })
)

I run vitest run --coverage and I see that ✓ src/__tests__/unit/utils.test.ts (1) is ran but
in the report utils.ts has 0 statements covered.

Reproduction

I hope the files provided are enough to see if there is either a bug or I have a misconfiguration somewhere.

System Info

System:
    OS: Windows 10 10.0.22000
    CPU: (16) x64 AMD Ryzen 7 5700U with Radeon Graphics
    Memory: 5.40 GB / 15.35 GB
  Binaries:
    Node: 18.16.0 - C:\Program Files\nodejs\node.EXE
    npm: 9.6.5 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22000.120.0), Chromium (112.0.1722.58)
    Internet Explorer: 11.0.22000.120

    "vitest": "^0.30.1" (from packages.json)

Used Package Manager

npm

Validations

@AriPerkkio
Copy link
Member

Could you try removing the test.coverage.include and test.coverage.exclude and see if the file ends up in the report? And maybe even the test.root?

@AriPerkkio
Copy link
Member

I can reproduce this now.

When source file is imported using alias the vitenode.fetchCache does not contain full path. This gets filtered out in coverage provider.

import {addThreeNumbers} from "src/logic/utils";
// fetchCache contains entry for "src/logic/utils"
import {addThreeNumbers} from "../../logic/utils";
// fetchCache contains entry for "/x/y/z/repro/src/logic/utils.ts" - Includes the extension and full path

@AriPerkkio AriPerkkio added bug feat: coverage Issues and PRs related to the coverage feature labels May 5, 2023
@AriPerkkio
Copy link
Member

AriPerkkio commented May 5, 2023

The vitest.config.ts is missing alias. Add following and coverage report is OK:

    alias: {
      src: fileURLToPath(new URL("./src", import.meta.url)),
    }

Or as alternative import the utils.ts with relative path instead of relying on project root:

- import {addThreeNumbers} from "src/logic/utils";
+ import {addThreeNumbers} from "../../logic/utils";

@AriPerkkio AriPerkkio removed the bug label May 5, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Jun 3, 2023
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
Development

Successfully merging a pull request may close this issue.

2 participants