-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvitest.storybook.config.ts
More file actions
217 lines (196 loc) · 6.65 KB
/
Copy pathvitest.storybook.config.ts
File metadata and controls
217 lines (196 loc) · 6.65 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* Vitest configuration for Storybook story tests.
*
* @remarks
* `@storybook/addon-vitest`'s Vite plugin currently relies on micromatch to
* decide which modules to transform. On Windows, the plugin builds glob
* patterns using backslashes, which micromatch does **not** match, resulting in
* story modules never being transformed into runnable tests.
*
* To keep Storybook story tests working on Windows, we apply Storybook's
* `vitestTransform` ourselves for story files under `storybook/stories/**`.
*/
import type { Plugin, UserConfig } from "vite";
import { playwright } from "@vitest/browser-playwright";
import * as path from "node:path";
import { vitestTransform } from "storybook/internal/csf-tools";
import {
defineConfig,
type ViteUserConfig,
type ViteUserConfigExport,
} from "vitest/config";
import {
createStorybookBaseViteConfig,
createStorybookPlugins,
createStorybookReactPluginOptions,
ensureStorybookCoverageDirectory,
storybookCoverageDirectory,
} from "./storybook/viteSharedConfig";
const reactPluginOptions = createStorybookReactPluginOptions();
const sonarReportOutputPath = path.join(
storybookCoverageDirectory,
"sonar-report.xml"
);
const vitestJsonOutputPath = path.join(
storybookCoverageDirectory,
"test-results.json"
);
const normalizeViteId = (id: string): string => id.split("?")[0] ?? id;
interface VitestTransformResultLike {
readonly code: string;
readonly map: unknown;
}
const isVitestTransformResultLike = (
value: unknown
): value is VitestTransformResultLike => {
if (value === null || typeof value !== "object") {
return false;
}
const record = value as Record<string, unknown>;
return typeof record["code"] === "string" && "map" in record;
};
const createStorybookStoriesTransformPlugin = (): Plugin => {
const configDir = path.resolve(process.cwd(), ".storybook");
return {
enforce: "pre",
name: "uptime-watcher:storybook-vitest-transform",
async transform(code, id) {
const fileName = normalizeViteId(id);
const normalizedForMatch = fileName.replaceAll("\\\\", "/");
if (
!normalizedForMatch.includes("/storybook/stories/") ||
!normalizedForMatch.endsWith(".stories.tsx")
) {
return null;
}
const rawResult: unknown = await vitestTransform({
code,
configDir,
fileName,
previewLevelTags: [],
stories: ["../storybook/stories/**/*.stories.tsx"],
// Run all stories by default; individual stories can opt out
// via tags/parameters.
tagsFilter: {
exclude: [],
include: [],
skip: [],
},
});
if (!isVitestTransformResultLike(rawResult)) {
throw new TypeError(
"Unexpected Storybook vitestTransform output shape"
);
}
return {
code: rawResult.code,
};
},
};
};
const createStorybookVitestConfig = async (): Promise<ViteUserConfig> => {
await ensureStorybookCoverageDirectory();
const baseViteConfig = createStorybookBaseViteConfig();
const electronLogRendererStubPath = path.resolve(
process.cwd(),
"storybook/shims/electronLogRenderer.stub.ts"
);
return {
...baseViteConfig,
cacheDir: "./.cache/vitest/storybook",
optimizeDeps: {
...baseViteConfig.optimizeDeps,
include: [
...(baseViteConfig.optimizeDeps?.include ?? []),
// Storybook + vitest addon internals used by portable stories.
"@storybook/addon-vitest/internal/test-utils",
"storybook/actions",
"storybook/test",
"storybook/theming",
"storybook/viewport",
// Utility deps pulled in by Storybook/theming/actions.
"fast-deep-equal",
// Charts used by analytics stories.
"chart.js",
"chartjs-adapter-date-fns",
"chartjs-plugin-zoom",
// Icons referenced by various stories.
"react-icons/bi",
"react-icons/bs",
"react-icons/fi",
"react-icons/go",
"react-icons/hi2",
"react-icons/io5",
"react-icons/md",
"react-icons/ri",
"react-icons/si",
"react-icons/tb",
"react-icons/vsc",
],
},
plugins: createStorybookPlugins({
additionalPlugins: [createStorybookStoriesTransformPlugin()],
reactOptions: reactPluginOptions,
}),
publicDir: "public",
resolve: {
...baseViteConfig.resolve,
alias: {
...baseViteConfig.resolve?.alias,
"electron-log/renderer": electronLogRendererStubPath,
},
},
test: {
browser: {
enabled: true,
headless: true,
instances: [
{
browser: "chromium",
},
],
provider: playwright(),
},
coverage: {
provider: "v8",
reporter: [
"text",
"json",
"lcov",
],
reportsDirectory: storybookCoverageDirectory,
},
fileParallelism: false,
globals: true,
include: ["storybook/stories/**/*.stories.tsx"],
isolate: true,
maxWorkers: 1,
name: {
color: "blue",
label: "Storybook",
}, // Custom project name and color for Vitest
outputFile: {
json: vitestJsonOutputPath,
},
reporters: [
"dot",
"hanging-process",
[
"vitest-sonar-reporter",
{
outputFile: sonarReportOutputPath,
silent: true,
},
],
],
setupFiles: ["./storybook/vitest.setup.ts"],
},
};
};
/**
* Vitest configuration export for Storybook story tests.
*/
const storybookVitestConfig: ViteUserConfigExport = defineConfig(
createStorybookVitestConfig
);
export default storybookVitestConfig as UserConfig;