-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
preview-errors.ts
339 lines (291 loc) · 11.6 KB
/
preview-errors.ts
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import { dedent } from 'ts-dedent';
import { StorybookError } from './storybook-error';
/**
* If you can't find a suitable category for your error, create one based on the package name/file
* path of which the error is thrown. For instance: If it's from `@storybook/client-logger`, then
* CLIENT-LOGGER
*
* Categories are prefixed by a logical grouping, e.g. PREVIEW_ or FRAMEWORK_ to prevent manager and
* preview errors from having the same category and error code.
*/
export enum Category {
BLOCKS = 'BLOCKS',
DOCS_TOOLS = 'DOCS-TOOLS',
PREVIEW_CLIENT_LOGGER = 'PREVIEW_CLIENT-LOGGER',
PREVIEW_CHANNELS = 'PREVIEW_CHANNELS',
PREVIEW_CORE_EVENTS = 'PREVIEW_CORE-EVENTS',
PREVIEW_INSTRUMENTER = 'PREVIEW_INSTRUMENTER',
PREVIEW_API = 'PREVIEW_API',
PREVIEW_REACT_DOM_SHIM = 'PREVIEW_REACT-DOM-SHIM',
PREVIEW_ROUTER = 'PREVIEW_ROUTER',
PREVIEW_THEMING = 'PREVIEW_THEMING',
RENDERER_HTML = 'RENDERER_HTML',
RENDERER_PREACT = 'RENDERER_PREACT',
RENDERER_REACT = 'RENDERER_REACT',
RENDERER_SERVER = 'RENDERER_SERVER',
RENDERER_SVELTE = 'RENDERER_SVELTE',
RENDERER_VUE = 'RENDERER_VUE',
RENDERER_VUE3 = 'RENDERER_VUE3',
RENDERER_WEB_COMPONENTS = 'RENDERER_WEB-COMPONENTS',
FRAMEWORK_NEXTJS = 'FRAMEWORK_NEXTJS',
ADDON_VITEST = 'ADDON_VITEST',
}
export class MissingStoryAfterHmrError extends StorybookError {
constructor(public data: { storyId: string }) {
super({
category: Category.PREVIEW_API,
code: 1,
message: dedent`
Couldn't find story matching id '${data.storyId}' after HMR.
- Did you just rename a story?
- Did you remove it from your CSF file?
- Are you sure a story with the id '${data.storyId}' exists?
- Please check the values in the stories field of your main.js config and see if they would match your CSF File.
- Also check the browser console and terminal for potential error messages.`,
});
}
}
export class ImplicitActionsDuringRendering extends StorybookError {
constructor(public data: { phase: string; name: string; deprecated: boolean }) {
super({
category: Category.PREVIEW_API,
code: 2,
documentation:
'https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#using-implicit-actions-during-rendering-is-deprecated-for-example-in-the-play-function',
message: dedent`
We detected that you use an implicit action arg while ${data.phase} of your story.
${data.deprecated ? `\nThis is deprecated and won't work in Storybook 8 anymore.\n` : ``}
Please provide an explicit spy to your args like this:
import { fn } from '@storybook/test';
...
args: {
${data.name}: fn()
}`,
});
}
}
export class CalledExtractOnStoreError extends StorybookError {
constructor() {
super({
category: Category.PREVIEW_API,
code: 3,
message: dedent`
Cannot call \`storyStore.extract()\` without calling \`storyStore.cacheAllCsfFiles()\` first.
You probably meant to call \`await preview.extract()\` which does the above for you.`,
});
}
}
export class MissingRenderToCanvasError extends StorybookError {
constructor() {
super({
category: Category.PREVIEW_API,
code: 4,
message: dedent`
Expected your framework's preset to export a \`renderToCanvas\` field.
Perhaps it needs to be upgraded for Storybook 7.0?`,
documentation:
'https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#mainjs-framework-field',
});
}
}
export class CalledPreviewMethodBeforeInitializationError extends StorybookError {
constructor(public data: { methodName: string }) {
super({
category: Category.PREVIEW_API,
code: 5,
message: dedent`
Called \`Preview.${data.methodName}()\` before initialization.
The preview needs to load the story index before most methods can be called. If you want
to call \`${data.methodName}\`, try \`await preview.initializationPromise;\` first.
If you didn't call the above code, then likely it was called by an addon that needs to
do the above.`,
});
}
}
export class StoryIndexFetchError extends StorybookError {
constructor(public data: { text: string }) {
super({
category: Category.PREVIEW_API,
code: 6,
message: dedent`
Error fetching \`/index.json\`:
${data.text}
If you are in development, this likely indicates a problem with your Storybook process,
check the terminal for errors.
If you are in a deployed Storybook, there may have been an issue deploying the full Storybook
build.`,
});
}
}
export class MdxFileWithNoCsfReferencesError extends StorybookError {
constructor(public data: { storyId: string }) {
super({
category: Category.PREVIEW_API,
code: 7,
message: dedent`
Tried to render docs entry ${data.storyId} but it is a MDX file that has no CSF
references, or autodocs for a CSF file that some doesn't refer to itself.
This likely is an internal error in Storybook's indexing, or you've attached the
\`attached-mdx\` tag to an MDX file that is not attached.`,
});
}
}
export class EmptyIndexError extends StorybookError {
constructor() {
super({
category: Category.PREVIEW_API,
code: 8,
message: dedent`
Couldn't find any stories in your Storybook.
- Please check your stories field of your main.js config: does it match correctly?
- Also check the browser console and terminal for error messages.`,
});
}
}
export class NoStoryMatchError extends StorybookError {
constructor(public data: { storySpecifier: string }) {
super({
category: Category.PREVIEW_API,
code: 9,
message: dedent`
Couldn't find story matching '${data.storySpecifier}'.
- Are you sure a story with that id exists?
- Please check your stories field of your main.js config.
- Also check the browser console and terminal for error messages.`,
});
}
}
export class MissingStoryFromCsfFileError extends StorybookError {
constructor(public data: { storyId: string }) {
super({
category: Category.PREVIEW_API,
code: 10,
message: dedent`
Couldn't find story matching id '${data.storyId}' after importing a CSF file.
The file was indexed as if the story was there, but then after importing the file in the browser
we didn't find the story. Possible reasons:
- You are using a custom story indexer that is misbehaving.
- You have a custom file loader that is removing or renaming exports.
Please check your browser console and terminal for errors that may explain the issue.`,
});
}
}
export class StoryStoreAccessedBeforeInitializationError extends StorybookError {
constructor() {
super({
category: Category.PREVIEW_API,
code: 11,
message: dedent`
Cannot access the Story Store until the index is ready.
It is not recommended to use methods directly on the Story Store anyway, in Storybook 9 we will
remove access to the store entirely`,
});
}
}
export class MountMustBeDestructuredError extends StorybookError {
constructor(public data: { playFunction: string }) {
super({
category: Category.PREVIEW_API,
code: 12,
message: dedent`
Incorrect use of mount in the play function.
To use mount in the play function, you must satisfy the following two requirements:
1. You *must* destructure the mount property from the \`context\` (the argument passed to your play function).
This makes sure that Storybook does not start rendering the story before the play function begins.
2. Your Storybook framework or builder must be configured to transpile to ES2017 or newer.
This is because destructuring statements and async/await usages are otherwise transpiled away,
which prevents Storybook from recognizing your usage of \`mount\`.
Note that Angular is not supported. As async/await is transpiled to support the zone.js polyfill.
More info: https://storybook.js.org/docs/writing-tests/interaction-testing#run-code-before-the-component-gets-rendered
Received the following play function:
${data.playFunction}`,
});
}
}
export class NoRenderFunctionError extends StorybookError {
constructor(public data: { id: string }) {
super({
category: Category.PREVIEW_API,
code: 14,
message: dedent`
No render function available for storyId '${data.id}'
`,
});
}
}
export class NoStoryMountedError extends StorybookError {
constructor() {
super({
category: Category.PREVIEW_API,
code: 15,
message: dedent`
No component is mounted in your story.
This usually occurs when you destructure mount in the play function, but forget to call it.
For example:
async play({ mount, canvasElement }) {
// 👈 mount should be called: await mount();
const canvas = within(canvasElement);
const button = await canvas.findByRole('button');
await userEvent.click(button);
};
Make sure to either remove it or call mount in your play function.
`,
});
}
}
export class NextJsSharpError extends StorybookError {
constructor() {
super({
category: Category.FRAMEWORK_NEXTJS,
code: 1,
documentation: 'https://storybook.js.org/docs/get-started/nextjs#faq',
message: dedent`
You are importing avif images, but you don't have sharp installed.
You have to install sharp in order to use image optimization features in Next.js.
`,
});
}
}
export class NextjsRouterMocksNotAvailable extends StorybookError {
constructor(public data: { importType: string }) {
super({
category: Category.FRAMEWORK_NEXTJS,
code: 2,
message: dedent`
Tried to access router mocks from "${data.importType}" but they were not created yet. You might be running code in an unsupported environment.
`,
});
}
}
export class UnknownArgTypesError extends StorybookError {
constructor(public data: { type: object; language: string }) {
super({
category: Category.DOCS_TOOLS,
code: 1,
documentation: 'https://github.com/storybookjs/storybook/issues/26606',
message: dedent`
There was a failure when generating detailed ArgTypes in ${data.language} for:
${JSON.stringify(data.type, null, 2)}
Storybook will fall back to use a generic type description instead.
This type is either not supported or it is a bug in the docgen generation in Storybook.
If you think this is a bug, please detail it as much as possible in the Github issue.
`,
});
}
}
export class UnsupportedViewportDimensionError extends StorybookError {
constructor(public data: { dimension: string; value: string }) {
super({
category: Category.ADDON_VITEST,
code: 1,
// TODO: Add documentation about viewports support
// documentation: '',
message: dedent`
Encountered an unsupported value "${data.value}" when setting the viewport ${data.dimension} dimension.
The Storybook plugin only supports values in the following units:
- px, vh, vw, em, rem and %.
You can either change the viewport for this story to use one of the supported units or skip the test by adding '!test' to the story's tags per https://storybook.js.org/docs/writing-stories/tags
`,
});
}
}