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

Enable BASE_URL tests #1828

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions packages/starlight/__tests__/basics/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ describe('fileWithBase()', () => {
});
});

// 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', () => {
describe('with base', () => {
test('prepends base', async () => {
// Reset the modules registry so that re-importing `../../utils/base` re-evaluates the module
// and re-computes the base. Re-importing the module is necessary because top-level imports
// cannot be re-evaluated.
vi.resetModules();
// Set the base URL.
vi.stubEnv('BASE_URL', '/base/');
// Re-import the module to re-evaluate it.
const { fileWithBase } = await import('../../utils/base');

expect(fileWithBase('/img.svg')).toBe('/base/img.svg');

vi.unstubAllEnvs();
vi.resetModules();
});
});
});
Expand All @@ -33,5 +40,17 @@ describe('pathWithBase()', () => {
});
});

describe.todo('with base');
describe('with base', () => {
test('prepends base', async () => {
// See the first test with a base in this file for an explanation of the environment stubbing.
vi.resetModules();
vi.stubEnv('BASE_URL', '/base/');
const { pathWithBase } = await import('../../utils/base');

expect(pathWithBase('/path/')).toBe('/base/path/');

vi.unstubAllEnvs();
vi.resetModules();
});
});
});
5 changes: 1 addition & 4 deletions packages/starlight/__tests__/basics/slugs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ describe('urlToSlug', () => {
);
});

// It is currently not possible to test this as stubbing BASE_URL is not supported due to
// `vite-plugin-env` controlling it and the lack of a way to pass in an Astro config using
// `getViteConfig()` from `astro/config`.
test.todo('returns slugs with a custom `base` option', () => {
test('returns slugs with a custom `base` option', () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Note that this test does not need the whole resetModules()stubEnv()import() dance as contrary to base.ts, BASE_URL is not used/evaluated at the top-level scope of the module but in the scope of the function under test.

vi.stubEnv('BASE_URL', '/base/');
expect(urlToSlug(new URL('https://example.com/base'))).toBe('');
expect(urlToSlug(new URL('https://example.com/base/slug'))).toBe('slug');
Expand Down
Loading