-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathvitest.shared.config.ts
More file actions
157 lines (144 loc) · 4.91 KB
/
Copy pathvitest.shared.config.ts
File metadata and controls
157 lines (144 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { defineConfig } from "vitest/config";
import { resolve } from "node:path";
import { readFileSync } from "node:fs";
import { VerboseReporter } from "vitest/node";
/**
* vitest reporter that does not output "serialized error" to console which may contain secrets
*/
export class AzureSDKReporter extends VerboseReporter {
/**
* the `verbose` flag is used by VerboseReporter solely to control whether the serialized error should be output, so all we need to do
* is set it to false
*/
protected verbose = false;
}
export function isInDevopsPipeline() {
return process.env["SYSTEM_TEAMPROJECTID"] !== undefined;
}
/**
* Reads the package name from a package.json in the provided root directory.
*/
export function packageNameFrom(rootDir: string): string {
const pkgJsonPath = resolve(rootDir, "package.json");
const pkg = JSON.parse(readFileSync(pkgJsonPath, "utf-8"));
return pkg.name as string;
}
/**
* Creates standard alias mappings for tests given a root directory and outputs.
* - Maps the package name (from package.json) to the given distDir/indexFile
* - Maps "$internal/..." to the same distDir
*/
export function makeAliases(
rootDir: string,
options: {
distDir: string; // e.g. "./src", "./dist/esm", "./dist/browser"
indexFile?: string; // e.g. "index.ts" or "index.js" (default: index.js)
packageName?: string; // optional override
internalPattern?: RegExp; // optional override for the internal alias pattern
},
) {
const {
distDir,
indexFile = "index.js",
packageName = packageNameFrom(rootDir),
internalPattern = /^\$internal\/(.*)$/,
} = options;
return [
{
find: packageName,
replacement: resolve(rootDir, `${distDir}/${indexFile}`),
},
{
find: internalPattern,
replacement: resolve(rootDir, `${distDir}/$1`),
},
] as const;
}
function shouldCollectCoverage(rootDir: string) {
const publishCodeCoverage =
(process.env["PublishCodeCoverage"] ?? process.env["PUBLISHCODECOVERAGE"] ?? "")
.toString()
.toLowerCase();
const ciCoverageEnabled = publishCodeCoverage === "true" || publishCodeCoverage === "1";
return (
process.env["TEST_MODE"] === "live" ||
ciCoverageEnabled ||
rootDir.includes("/sdk/core/") ||
rootDir.includes("\\sdk\\core\\")
);
}
function getCoverageProjectRoot(rootDir: string): string {
return process.env["SYSTEM_DEFAULTWORKINGDIRECTORY"] ?? rootDir;
}
function makeNodeAliases(rootDir: string) {
const [dist, indexFile] = isInDevopsPipeline() ? ["dist/esm", "index.js"] : ["src", "index.ts"];
return makeAliases(rootDir, { distDir: `./${dist}`, indexFile });
}
/**
* Vite plugin that works around a processedIds cache bug in Vite's import analysis.
* The __vitest__ environment defaults to noExternal: [], which lets Vite cache the first
* externalization decision for a bare specifier across all importers. When multiple versions
* of @azure/core-lro coexist (v2 from published deps, v3 from workspace), the first resolution
* gets cached and subsequent importers get the wrong version. Adding core-lro to noExternal
* forces Vite to transform (not externalize) each import, resolving per-importer correctly.
* See https://github.com/vitest-dev/vitest/issues/10028
*/
export function fixCoreLroExternalization() {
return {
name: "fix-core-lro-externalization",
configEnvironment(name: string, config: { resolve?: { noExternal?: string[] | boolean } }) {
if (name === "__vitest__") {
config.resolve ??= {};
if (Array.isArray(config.resolve.noExternal)) {
config.resolve.noExternal.push("@azure/core-lro");
} else if (!config.resolve.noExternal) {
config.resolve.noExternal = ["@azure/core-lro"];
}
}
},
};
}
export default defineConfig({
plugins: [fixCoreLroExternalization()],
test: {
testTimeout: 1200000,
hookTimeout: 1200000,
reporters: [new AzureSDKReporter(), "junit"],
outputFile: {
junit: "test-results.xml",
},
fakeTimers: {
toFake: ["setTimeout", "Date"],
},
watch: false,
include: ["test/**/*.spec.ts"],
exclude: [
"test/**/browser/*.spec.ts",
"test/**/react-native/**",
"test/snippets.spec.ts",
"test/integration/**/*.spec.ts",
"test/stress/**/*.ts",
],
alias: [...makeNodeAliases(process.cwd())],
coverage: {
enabled: shouldCollectCoverage(process.cwd()),
include: ["src/**/*.ts"],
exclude: [
"src/**/*-browser.mts",
"src/**/*-react-native.mts",
"vitest*.config.ts",
"samples-dev/**/*.ts",
"test/snippets.spec.ts",
],
provider: "istanbul",
reporter: [
"text",
["cobertura", { file: "cobertura-coverage.xml", projectRoot: getCoverageProjectRoot(process.cwd()) }],
"html",
],
reportsDirectory: "coverage",
},
},
});