From f35d81ac2281096427cec07e316fe47ff62639b5 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Thu, 3 Sep 2026 00:30:19 +0530 Subject: [PATCH] test: cover the not-found page Unit tests for the resolver and the plugin's page discovery, a locale with its own `404.md` in the base build fixture with hosts that serve the nearest or only the root `404.html`, and a root `404.md` with an inheriting locale in the e2e site. Co-Authored-By: Claude Fable 5.1 --- __tests__/base/emit.test.ts | 49 ++++++ __tests__/base/fixture/.vitepress/config.ts | 9 ++ __tests__/base/fixture/zh/404.md | 7 + __tests__/base/fixture/zh/index.md | 3 + __tests__/base/helpers.ts | 12 +- __tests__/base/not-found.test.ts | 152 ++++++++++++++++++ __tests__/base/vitestGlobalSetup.ts | 30 +++- __tests__/e2e/.vitepress/config.ts | 5 + __tests__/e2e/404.md | 7 + __tests__/e2e/fr/index.md | 3 + __tests__/e2e/not-found.test.ts | 118 ++++++++++++++ .../unit/node/plugins/notFoundPlugin.test.ts | 129 +++++++++++++++ __tests__/unit/shared/shared.test.ts | 51 +++++- 13 files changed, 567 insertions(+), 8 deletions(-) create mode 100644 __tests__/base/fixture/zh/404.md create mode 100644 __tests__/base/fixture/zh/index.md create mode 100644 __tests__/base/not-found.test.ts create mode 100644 __tests__/e2e/404.md create mode 100644 __tests__/e2e/fr/index.md create mode 100644 __tests__/e2e/not-found.test.ts create mode 100644 __tests__/unit/node/plugins/notFoundPlugin.test.ts diff --git a/__tests__/base/emit.test.ts b/__tests__/base/emit.test.ts index aa805e6a..34bc8019 100644 --- a/__tests__/base/emit.test.ts +++ b/__tests__/base/emit.test.ts @@ -91,6 +91,14 @@ describe('relative base emit', () => { expect(html).toMatch(/href="\.\/assets\/style\.[\w-]+\.css"/) }) + test('locale 404 renders at its own depth', () => { + const html = read('relative', 'zh/404.html') + expect(html).toContain( + 'window.__VP_SITE_ROOT__=new URL("../",location).href' + ) + expect(html).toMatch(/href="\.\.\/assets\/style\.[\w-]+\.css"/) + }) + test('no sentinel leaks into emitted html or css', () => { for (const file of walk(dist('relative'))) { if (!/\.(html|css)$/.test(file)) continue @@ -208,3 +216,44 @@ describe('plain base emit is unchanged', () => { expect(html).not.toContain('crossorigin>') }) }) + +describe('not-found emit', () => { + for (const mode of ['plain', 'relative', 'cdn', 'mpa']) { + test(`${mode}: the theme page stands in for a missing root 404.md`, () => { + const html = read(mode, '404.html') + expect(html).toContain('
') + expect(html).toContain('class="NotFound"') + expect(html).toContain('404 | Base Fixture') + expect(html).toContain('') + expect(html).toContain(' { + const html = read(mode, 'zh/404.html') + expect(html).toContain('
') + expect(html).toContain('页面未找到') + expect(html).toContain('页面未找到 | Base Fixture') + expect(html).toContain('') + expect(html).toContain(' { + const sitemap = read(mode, 'sitemap.xml') + expect(sitemap).toContain('https://example.com/zh/') + expect(sitemap).toContain('https://example.com/sub/page.html') + expect(sitemap).not.toContain('404.html') + }) + } + + test('mpa: the not-found page needs no script', () => { + const html = read('mpa', '404.html') + expect(html).not.toContain('