Skip to content

Commit

Permalink
Enable BASE_URL tests (#1828)
Browse files Browse the repository at this point in the history
  • Loading branch information
HiDeoo committed May 6, 2024
1 parent 95ace6d commit 24b92ca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
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', () => {
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

0 comments on commit 24b92ca

Please sign in to comment.