From b2b5595693242f900990e4046ea5b168d02b6956 Mon Sep 17 00:00:00 2001 From: HiDeoo <494699+HiDeoo@users.noreply.github.com> Date: Fri, 3 May 2024 17:42:52 +0200 Subject: [PATCH] test: enable `BASE_URL` tests --- .../starlight/__tests__/basics/base.test.ts | 31 +++++++++++++++---- .../starlight/__tests__/basics/slugs.test.ts | 5 +-- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/packages/starlight/__tests__/basics/base.test.ts b/packages/starlight/__tests__/basics/base.test.ts index ab8fd173a0..72cabe6539 100644 --- a/packages/starlight/__tests__/basics/base.test.ts +++ b/packages/starlight/__tests__/basics/base.test.ts @@ -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(); }); }); }); @@ -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(); + }); + }); }); diff --git a/packages/starlight/__tests__/basics/slugs.test.ts b/packages/starlight/__tests__/basics/slugs.test.ts index 6b7c9bd16b..a3f8f3992c 100644 --- a/packages/starlight/__tests__/basics/slugs.test.ts +++ b/packages/starlight/__tests__/basics/slugs.test.ts @@ -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');