-
-
Notifications
You must be signed in to change notification settings - Fork 482
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: HiDeoo <494699+HiDeoo@users.noreply.github.com>
- Loading branch information
Showing
12 changed files
with
414 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@astrojs/starlight": patch | ||
--- | ||
|
||
Improves error messages shown by Starlight for configuration errors. |
189 changes: 189 additions & 0 deletions
189
packages/starlight/__tests__/basics/config-errors.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
import { expect, test } from 'vitest'; | ||
import { parseWithFriendlyErrors } from '../../utils/error-map'; | ||
import { StarlightConfigSchema, type StarlightUserConfig } from '../../utils/user-config'; | ||
|
||
function parseStarlightConfigWithFriendlyErrors(config: StarlightUserConfig) { | ||
return parseWithFriendlyErrors( | ||
StarlightConfigSchema, | ||
config, | ||
'Invalid config passed to starlight integration' | ||
); | ||
} | ||
|
||
test('parses valid config successfully', () => { | ||
const data = parseStarlightConfigWithFriendlyErrors({ title: '' }); | ||
expect(data).toMatchInlineSnapshot(` | ||
{ | ||
"components": { | ||
"Banner": "@astrojs/starlight/components/Banner.astro", | ||
"ContentPanel": "@astrojs/starlight/components/ContentPanel.astro", | ||
"EditLink": "@astrojs/starlight/components/EditLink.astro", | ||
"FallbackContentNotice": "@astrojs/starlight/components/FallbackContentNotice.astro", | ||
"Footer": "@astrojs/starlight/components/Footer.astro", | ||
"Head": "@astrojs/starlight/components/Head.astro", | ||
"Header": "@astrojs/starlight/components/Header.astro", | ||
"Hero": "@astrojs/starlight/components/Hero.astro", | ||
"LanguageSelect": "@astrojs/starlight/components/LanguageSelect.astro", | ||
"LastUpdated": "@astrojs/starlight/components/LastUpdated.astro", | ||
"MarkdownContent": "@astrojs/starlight/components/MarkdownContent.astro", | ||
"MobileMenuFooter": "@astrojs/starlight/components/MobileMenuFooter.astro", | ||
"MobileMenuToggle": "@astrojs/starlight/components/MobileMenuToggle.astro", | ||
"MobileTableOfContents": "@astrojs/starlight/components/MobileTableOfContents.astro", | ||
"PageFrame": "@astrojs/starlight/components/PageFrame.astro", | ||
"PageSidebar": "@astrojs/starlight/components/PageSidebar.astro", | ||
"PageTitle": "@astrojs/starlight/components/PageTitle.astro", | ||
"Pagination": "@astrojs/starlight/components/Pagination.astro", | ||
"Search": "@astrojs/starlight/components/Search.astro", | ||
"Sidebar": "@astrojs/starlight/components/Sidebar.astro", | ||
"SiteTitle": "@astrojs/starlight/components/SiteTitle.astro", | ||
"SkipLink": "@astrojs/starlight/components/SkipLink.astro", | ||
"SocialIcons": "@astrojs/starlight/components/SocialIcons.astro", | ||
"TableOfContents": "@astrojs/starlight/components/TableOfContents.astro", | ||
"ThemeProvider": "@astrojs/starlight/components/ThemeProvider.astro", | ||
"ThemeSelect": "@astrojs/starlight/components/ThemeSelect.astro", | ||
"TwoColumnContent": "@astrojs/starlight/components/TwoColumnContent.astro", | ||
}, | ||
"customCss": [], | ||
"defaultLocale": { | ||
"dir": "ltr", | ||
"label": "English", | ||
"lang": "en", | ||
"locale": undefined, | ||
}, | ||
"disable404Route": false, | ||
"editLink": {}, | ||
"favicon": { | ||
"href": "/favicon.svg", | ||
"type": "image/svg+xml", | ||
}, | ||
"head": [], | ||
"isMultilingual": false, | ||
"lastUpdated": false, | ||
"locales": undefined, | ||
"pagefind": true, | ||
"pagination": true, | ||
"tableOfContents": { | ||
"maxHeadingLevel": 3, | ||
"minHeadingLevel": 2, | ||
}, | ||
"title": "", | ||
"titleDelimiter": "|", | ||
} | ||
`); | ||
}); | ||
|
||
test('errors if title is missing', () => { | ||
expect(() => | ||
parseStarlightConfigWithFriendlyErrors({} as any) | ||
).toThrowErrorMatchingInlineSnapshot( | ||
` | ||
"[AstroUserError]: | ||
Invalid config passed to starlight integration | ||
Hint: | ||
**title**: Required" | ||
` | ||
); | ||
}); | ||
|
||
test('errors if title value is not a string', () => { | ||
expect(() => | ||
parseStarlightConfigWithFriendlyErrors({ title: 5 } as any) | ||
).toThrowErrorMatchingInlineSnapshot( | ||
` | ||
"[AstroUserError]: | ||
Invalid config passed to starlight integration | ||
Hint: | ||
**title**: Expected type \`"string"\`, received \`"number"\`" | ||
` | ||
); | ||
}); | ||
|
||
test('errors with bad social icon config', () => { | ||
expect(() => | ||
parseStarlightConfigWithFriendlyErrors({ title: 'Test', social: { unknown: '' } as any }) | ||
).toThrowErrorMatchingInlineSnapshot( | ||
` | ||
"[AstroUserError]: | ||
Invalid config passed to starlight integration | ||
Hint: | ||
**social.unknown**: Invalid enum value. Expected 'twitter' | 'mastodon' | 'github' | 'gitlab' | 'bitbucket' | 'discord' | 'gitter' | 'codeberg' | 'codePen' | 'youtube' | 'threads' | 'linkedin' | 'twitch' | 'microsoftTeams' | 'instagram' | 'stackOverflow' | 'x.com' | 'telegram' | 'rss' | 'facebook' | 'email' | 'reddit' | 'patreon' | 'slack' | 'matrix' | 'openCollective', received 'unknown' | ||
**social.unknown**: Invalid url" | ||
` | ||
); | ||
}); | ||
|
||
test('errors with bad logo config', () => { | ||
expect(() => | ||
parseStarlightConfigWithFriendlyErrors({ title: 'Test', logo: { html: '' } as any }) | ||
).toThrowErrorMatchingInlineSnapshot( | ||
` | ||
"[AstroUserError]: | ||
Invalid config passed to starlight integration | ||
Hint: | ||
**logo**: Did not match union. | ||
> Expected type \`{ src: string } | { dark: string; light: string }\` | ||
> Received \`{ "html": "" }\`" | ||
` | ||
); | ||
}); | ||
|
||
test('errors with bad head config', () => { | ||
expect(() => | ||
parseStarlightConfigWithFriendlyErrors({ | ||
title: 'Test', | ||
head: [{ tag: 'unknown', attrs: { prop: null }, content: 20 } as any], | ||
}) | ||
).toThrowErrorMatchingInlineSnapshot( | ||
` | ||
"[AstroUserError]: | ||
Invalid config passed to starlight integration | ||
Hint: | ||
**head.0.tag**: Invalid enum value. Expected 'title' | 'base' | 'link' | 'style' | 'meta' | 'script' | 'noscript' | 'template', received 'unknown' | ||
**head.0.attrs.prop**: Did not match union. | ||
> Expected type \`"string" | "boolean" | "undefined"\`, received \`"null"\` | ||
**head.0.content**: Expected type \`"string"\`, received \`"number"\`" | ||
` | ||
); | ||
}); | ||
|
||
test('errors with bad sidebar config', () => { | ||
expect(() => | ||
parseStarlightConfigWithFriendlyErrors({ | ||
title: 'Test', | ||
sidebar: [{ label: 'Example', href: '/' } as any], | ||
}) | ||
).toThrowErrorMatchingInlineSnapshot( | ||
` | ||
"[AstroUserError]: | ||
Invalid config passed to starlight integration | ||
Hint: | ||
**sidebar.0**: Did not match union. | ||
> Expected type \`{ link: string } | { items: array } | { autogenerate: object }\` | ||
> Received \`{ "label": "Example", "href": "/" }\`" | ||
` | ||
); | ||
}); | ||
|
||
test('errors with bad nested sidebar config', () => { | ||
expect(() => | ||
parseStarlightConfigWithFriendlyErrors({ | ||
title: 'Test', | ||
sidebar: [ | ||
{ | ||
label: 'Example', | ||
items: [ | ||
{ label: 'Nested Example 1', link: '/' }, | ||
{ label: 'Nested Example 2', link: true }, | ||
], | ||
} as any, | ||
], | ||
}) | ||
).toThrowErrorMatchingInlineSnapshot(` | ||
"[AstroUserError]: | ||
Invalid config passed to starlight integration | ||
Hint: | ||
**sidebar.0.items.1**: Did not match union. | ||
> Expected type \`{ link: string } | { items: array } | { autogenerate: object }\` | ||
> Received \`{ "label": "Example", "items": [ { "label": "Nested Example 1", "link": "/" }, { "label": "Nested Example 2", "link": true } ] }\`" | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/starlight/__tests__/snapshot-serializer-astro-error.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { AstroError } from 'astro/errors'; | ||
import type { SnapshotSerializer } from 'vitest'; | ||
|
||
export default { | ||
/** Check if a value should be handled by this serializer, i.e. if it is an `AstroError`. */ | ||
test(val) { | ||
return !!val && AstroError.is(val); | ||
}, | ||
/** Customize serialization of Astro errors to include the `hint`. Vitest only uses `message` by default. */ | ||
serialize({ name, message, hint }: AstroError, config, indentation, depth, refs, printer) { | ||
const prettyError = `[${name}]:\n${indent(message)}\nHint:\n${indent(hint)}`; | ||
return printer(prettyError, config, indentation, depth, refs); | ||
}, | ||
} satisfies SnapshotSerializer; | ||
|
||
/** Indent each line in `string` with a given character. */ | ||
function indent(string = '', indentation = '\t') { | ||
return string | ||
.split('\n') | ||
.map((line) => indentation + line) | ||
.join('\n'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.