Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import assert from 'node:assert';
import { readFile } from 'node:fs/promises';
import { createRequire } from 'node:module';
import { platform } from 'node:os';
import path from 'node:path';
import type {
BrowserConfigOptions,
Expand Down Expand Up @@ -173,6 +174,7 @@ async function loadResultFile(file: ResultFile): Promise<string> {

export function createVitestPlugins(pluginOptions: PluginOptions): VitestPlugins {
const { workspaceRoot, buildResultFiles, testFileToEntryPoint } = pluginOptions;
const isWindows = platform() === 'win32';

return [
{
Expand All @@ -184,6 +186,31 @@ export function createVitestPlugins(pluginOptions: PluginOptions): VitestPlugins
return id;
}

// Workaround for Vitest in Windows when a fully qualified absolute path is provided with
// a superfluous leading slash. This can currently occur with the `@vitest/coverage-v8` provider
// when it uses `removeStartsWith(url, FILE_PROTOCOL)` to convert a file URL resulting in
// `/D:/tmp_dir/...` instead of `D:/tmp_dir/...`.
if (id[0] === '/' && isWindows) {
const slicedId = id.slice(1);
if (path.isAbsolute(slicedId)) {
return slicedId;
}
}

if (importer && (id[0] === '.' || id[0] === '/')) {
let fullPath;
if (testFileToEntryPoint.has(importer)) {
fullPath = toPosixPath(path.join(workspaceRoot, id));
} else {
fullPath = toPosixPath(path.join(path.dirname(importer), id));
}

const relativePath = path.relative(workspaceRoot, fullPath);
if (buildResultFiles.has(toPosixPath(relativePath))) {
return fullPath;
}
}

// Determine the base directory for resolution.
let baseDir: string;
if (importer) {
Expand Down
16 changes: 6 additions & 10 deletions tests/legacy-cli/e2e/tests/vitest/larger-project-coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,12 @@ export default async function () {
const { stdout: jsdomStdout } = await ng('test', '--no-watch', '--coverage');
assert.match(jsdomStdout, expectedMessage, `Expected ${totalTests} tests to pass in JSDOM mode.`);

// TODO: Investigate why coverage-final.json is empty on Windows in JSDOM mode.
// For now, skip the coverage report check on Windows.
if (process.platform !== 'win32') {
// Assert that every generated file is in the coverage report by reading the JSON output.
const jsdomSummary = JSON.parse(await readFile(coverageJsonPath));
const jsdomSummaryKeys = Object.keys(jsdomSummary);
for (const file of generatedFiles) {
const found = jsdomSummaryKeys.some((key) => key.endsWith(file));
assert.ok(found, `Expected ${file} to be in the JSDOM coverage report.`);
}
// Assert that every generated file is in the coverage report by reading the JSON output.
const jsdomSummary = JSON.parse(await readFile(coverageJsonPath));
const jsdomSummaryKeys = Object.keys(jsdomSummary);
for (const file of generatedFiles) {
const found = jsdomSummaryKeys.some((key) => key.endsWith(file));
assert.ok(found, `Expected ${file} to be in the JSDOM coverage report.`);
}

// Setup for browser mode
Expand Down