Skip to content
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

Add unit testing #383

Merged
merged 33 commits into from
Jul 24, 2023
Merged

Add unit testing #383

merged 33 commits into from
Jul 24, 2023

Conversation

delucis
Copy link
Member

@delucis delucis commented Jul 22, 2023

What kind of changes does this PR include?

  • Changes to Starlight code
  • Something else!

Description

@changeset-bot
Copy link

changeset-bot bot commented Jul 22, 2023

🦋 Changeset detected

Latest commit: 5b58303

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@astrojs/starlight Patch

Not sure what this means? Click here to learn what changesets are.

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

@netlify
Copy link

netlify bot commented Jul 22, 2023

Deploy Preview for astro-starlight ready!

Name Link
🔨 Latest commit 5b58303
🔍 Latest deploy log https://app.netlify.com/sites/astro-starlight/deploys/64bea3e3fcfd0500081f4503
😎 Deploy Preview https://deploy-preview-383--astro-starlight.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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

@github-actions github-actions bot added 🚨 action Changes to GitHub Action workflows 🌟 core Changes to Starlight’s main package labels Jul 22, 2023
@astrobot-houston
Copy link
Collaborator

astrobot-houston commented Jul 22, 2023

size-limit report 📦

Path Size
/index.html 13.15 KB (0%)
/_astro/*.js 16.21 KB (0%)
/_astro/*.css 8.54 KB (0%)

Comment on lines +14 to +23
// TODO: Stubbing BASE_URL is not currently possible.
// Astro controls BASE_URL via its `vite-plugin-env`, which prevents Vitest’s stubbing from
// working and there’s also no way to pass in Astro config in Astro’s `getViteConfig` helper.
describe.todo('with base', () => {
test('prepends base', () => {
vi.stubEnv('BASE_URL', '/base/');
expect(fileWithBase('/img.svg')).toBe('/base/img.svg');
vi.unstubAllEnvs();
});
});
Copy link
Member Author

Choose a reason for hiding this comment

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

Vitest provides a stubbing helper to set import.meta.env values in tests, but this does not work for values controlled by Astro’s vite-plugin-env such as BASE_URL.

It is also not possible to set Astro’s base option via a vitest.config.ts file using Astro’s getViteConfig helper — instead Astro side loads an astro.config from disk. I couldn’t find a way to get it to detect a local test-suite-specific astro.config, so for now testing for custom base settings seems to be blocked.

Copy link
Member

Choose a reason for hiding this comment

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

This makes sense. I guess a potential approach could be a more e2e oriented test with a fixture specifying a base config option and checking after a build for example the outputted favicon path but may not be that worth it 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that would be the hardcore way (although given withastro/astro#7561 might also be tricky to set-up without cluttering the repo with fixture directories at the base level), but seems wrong for testing a single function output 😅

Comment on lines +8 to +16
export function defineVitestConfig(config: z.input<typeof StarlightConfigSchema>) {
return getViteConfig({
plugins: [
vitePluginStarlightUserConfig(StarlightConfigSchema.parse(config), {
root: new URL(import.meta.url),
}),
],
});
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This config helper wraps Astro’s getViteConfig to set virtual modules in the test environment using Starlight’s Vite plugin. If each test suite could contain an astro.config instead, we could perhaps use the Starlight integration directly instead.

packages/starlight/package.json Outdated Show resolved Hide resolved
@delucis delucis marked this pull request as ready for review July 24, 2023 10:45
@delucis delucis changed the title WIP: Add unit testing Add unit testing Jul 24, 2023
@delucis delucis added the 🌟 patch Change that triggers a patch release label Jul 24, 2023
Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

Setup looks great! Not leaving an approval since I don't know the entire scope of the changes, but that's a lot of tests for the initial implementation.

}),
});

function mockDoc(
Copy link
Member

Choose a reason for hiding this comment

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

This gave me a good chuckle 😄

@delucis delucis merged commit 0ebc47e into main Jul 24, 2023
@delucis delucis deleted the dx-196/testing branch July 24, 2023 16:19
@astrobot-houston astrobot-houston mentioned this pull request Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚨 action Changes to GitHub Action workflows 🌟 core Changes to Starlight’s main package 🌟 patch Change that triggers a patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add tests to help guard against regressions
4 participants