Skip to content

fix(coverage): c8 aliased paths#3306

Closed
AriPerkkio wants to merge 1 commit intovitest-dev:mainfrom
AriPerkkio:fix/c8-alias-path
Closed

fix(coverage): c8 aliased paths#3306
AriPerkkio wants to merge 1 commit intovitest-dev:mainfrom
AriPerkkio:fix/c8-alias-path

Conversation

@AriPerkkio
Copy link
Member

@AriPerkkio AriPerkkio commented May 5, 2023

vitenode.fetchCache contains file path without extension when module is imported using alias. This does not end up in coverage report due to missing extension.

@bolt-new-by-stackblitz
Copy link

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Comment on lines +292 to +293
const [moduleUrl] = await this.resolveUrl(modulePath)
const href = pathToFileURL(moduleUrl).href
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When an aliased modules is loaded the fsPath doesn't contain extension at all. @sheremet-va
should I make this change to fsPath instead so that everywhere it's used would be affected, e.g. stacktraces I think?

With alias, without PR changes:

import { addThreeNumbers } from "src/logic/utils";

{
  __filename: '/x/y/repro/src/logic/utils',
  id: 'src/logic/utils',
  fsPath: 'src/logic/utils'
}

Without alias, without PR changes, works OK:

import { addThreeNumbers } from "../../logic/utils";

{
  __filename: '/x/y/repro/src/logic/utils.ts',
  id: '/src/logic/utils.ts',
  fsPath: '/x/y/repro/src/logic/utils.ts'
}

With alias, with PR changes:

import { addThreeNumbers } from "src/logic/utils";

{
  __filename: '/x/y/repro/src/logic/utils.ts',
  id: 'src/logic/utils',
  fsPath: 'src/logic/utils'
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not even be allowed. Vitest should fail if the module is not resolved. I am coming up with a fix for this. Some people rely on tsconfig resolution when in practice we don't support it.

Copy link
Member Author

@AriPerkkio AriPerkkio May 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, the reproduction case is not using alias in vitest.config.ts. By adding following the coverage works OK:

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

So I guess in your fix Vitest will fail to load module (or warn?) if only tsconfig.json has alias but vitest.config.ts does not?

Copy link
Member

@sheremet-va sheremet-va May 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can finally stop supporting this weird edge case: #3307

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I guess in your fix Vitest will fail to load module (or warn?) if only tsconfig.json has alias but vitest.config.ts does not?

People rely on the fact that their modules are relative to the baseDir which is never true in Vite. Because of this, we have a lot of issues when the file is not resolved correctly, but it can still fetch module code.

@AriPerkkio AriPerkkio closed this May 5, 2023
@AriPerkkio AriPerkkio deleted the fix/c8-alias-path branch May 5, 2023 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tests report does not cover exported function from typescript.

2 participants