Skip to content

fix userCollections?.docs #3205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

sgalcheung
Copy link

@sgalcheung sgalcheung commented May 20, 2025

  • check userCollections?.docs
  • add starlight-page example for test

Description

Capture docs as undefined.

// starlight-page.ts
// before
return userCollections?.docs.schema ?? docsSchema();

// after
const userSchema = userCollections?.docs?.schema;

  if (typeof userSchema === 'function') {
    return userSchema;
  } else if (userSchema) {
    return () => userSchema;
  } else {
    return docsSchema();
  }
- check `userCollections?.docs`
- add starlight-page example for test
Copy link

changeset-bot bot commented May 20, 2025

⚠️ No Changeset found

Latest commit: 3aef472

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

netlify bot commented May 20, 2025

Deploy Preview for astro-starlight ready!

Name Link
🔨 Latest commit 3aef472
🔍 Latest deploy log https://app.netlify.com/projects/astro-starlight/deploys/683812f64e78300008e7ecdd
😎 Deploy Preview https://deploy-preview-3205--astro-starlight.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 100 (no change from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 100 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions github-actions bot added the 🌟 core Changes to Starlight’s main package label May 20, 2025
Copy link
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @sgalcheung! I don’t think we need to add a full example for this though, so we should remove the examples/starlight-page/ addition.

@sgalcheung
Copy link
Author

Thanks for the PR @sgalcheung! I don’t think we need to add a full example for this though, so we should remove the examples/starlight-page/ addition.

In this case, this doesn't have any huge effect, but we could use this example for StarlightPage in the future.
Otherwise, I don't know how to write a better UT for it.

@HiDeoo
Copy link
Member

HiDeoo commented May 21, 2025

Thanks for your contribution 🙌 Small follow-up after Chris's review and your last comments:

In this case, this doesn't have any huge effect, but we could use this example for StarlightPage in the future.

Examples are meant to be installable, e.g. using pnpm create astro --template starlight/tailwind, pnpm create astro --template starlight/markdoc, etc. We try to limit the number of examples we have in the repo to a minimum as they need to be maintained, potentially documented and should also serve a good purpose.

I think it would also be very weird to have a Starlight example not having the Starlight expected docs collection configured.

In this case, it looks like the only benefit of the example seems to be testing but we should be able to test that without having to add a new example. Starlight already contains some tests:

  • Unit tests located in packages/starlight/__tests__
  • E2E tests located in packages/starlight/__e2e__

When it comes to starlight-page, we already have some tests in packages/starlight/__tests__/basics/starlight-page-route-data.test.ts so it should be a good place to add a test for the fix. A potential test could be to temporarily mock in a single test the import of virtual:starlight/collection-config and have the collections object be empty to simulate the absence of the docs collection.

I only tested quickly but it looks like a test similar to the following should work:

test('adds data to route shape when the `docs` collection is not defined', async () => {
	// Mock the collection config in this test to simulate the absence of the `docs` collection.
	vi.doMock('virtual:starlight/collection-config', () => ({ collections: {} }));

	const data = await generateStarlightPageRouteData({
		props: starlightPageProps,
		context: getRouteDataTestContext(starlightPagePathname),
	});
	expect(data.entry.data.title).toBe(starlightPageProps.frontmatter.title);

	// Undo the mock to restore the original behavior.
	vi.doUnmock('virtual:starlight/collection-config');
});

Before the changes from this PR, we get the expected error:

Cannot read properties of undefined (reading 'schema')

After the changes from this PR and using Chris's suggestion, we no longer get the error and data is properly added to the route data.

@sgalcheung
Copy link
Author

Thank you for improving the unit test @HiDeoo ! Could you commit this test code?

@sgalcheung sgalcheung changed the title fix https://github.com/withastro/starlight/discussions/3192 May 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🌟 core Changes to Starlight’s main package
3 participants