Skip to content

Migrated test/unit/frontend bucket to vitest#27992

Merged
9larsons merged 2 commits into
mainfrom
vitest-migrate-frontend-bucket
May 20, 2026
Merged

Migrated test/unit/frontend bucket to vitest#27992
9larsons merged 2 commits into
mainfrom
vitest-migrate-frontend-bucket

Conversation

@9larsons
Copy link
Copy Markdown
Contributor

Summary

Continues the vitest migration (after #27898 / #27900 / #27974 / #27991) by moving the test/unit/frontend bucket — 135 files, 1804 tests — from mocha to vitest.

Conversions

  • 39 files using mocha's suite-scope before() / after()beforeAll() / afterAll()
  • frontend-caching + handle-image-sizes: this.beforeEach / this.afterEach (mocha-API misuse) → top-level beforeEach / afterEach
  • 6 files using the mocha done() callback converted off it:
    • static-theme.test.js — 42 uniform middleware tests → a local callStaticTheme() promise helper
    • handle-image-sizes + 4 services/routing/controllers files → a new shared test/utils/deferred.js helper that bridges done onto a promise (done() resolves, done(err) rejects), so existing call sites stay unchanged
  • handle-image-sizes: removed 5 dead fakeReq.url.match = fn lines — assigning a property to a string primitive is a silent no-op in mocha's sloppy mode and throws under vitest's strict mode. The override never actually installed anything; the tests still assert next() is reached.

Snapshot support

This is the first migrated bucket with snapshot tests (ghost-head.test.js, 83 snapshots). Two pieces of infrastructure:

  • vitest-setup.ts — added a beforeEach that bridges @tryghost/jest-snapshot's per-test config. The mocha hook reads this.currentTest; vitest has no mocha this, so we derive the same testPath / testTitle from the vitest task. testTitle must exactly match mocha's fullTitle() (describe names + test name joined by spaces) or committed .snap keys won't resolve.
  • vitest.config.ts — set resolveSnapshotPath to a separate path. Ghost's snapshot tests are managed by @tryghost/jest-snapshot (its own __snapshots__/*.snap files); without this, vitest's native snapshot system adopts those files, rewrites their header, and reports every entry as obsolete (failing the run under CI). Redirecting vitest's path isolates the two systems. Verified with CI=true (which forbids snapshot writes): 83/83 match, .snap untouched.

Test plan

  • pnpm test:vitest test/unit/frontend: 135 files / 1804 tests pass
  • CI=true pnpm test:vitest test/unit/frontend: same, and no .snap file modified
  • pnpm test:vitest (full): 254 files / 2963 tests pass — snapshot beforeEach doesn't disturb other buckets
  • pnpm test:unit:base (mocha): 3582 tests pass
  • pnpm exec eslint clean for changed files
  • CI green

- continues the vitest migration (after #27898 / #27900 / #27974 / #27991)
  by moving all 135 files in test/unit/frontend (1804 tests) from mocha
  to vitest

Conversions:
- 39 files using mocha's suite-scope before()/after() -> beforeAll()/afterAll()
- frontend-caching + handle-image-sizes: this.beforeEach/this.afterEach
  (mocha-API misuse) -> top-level beforeEach/afterEach
- 6 files using the mocha done() callback converted off it:
  - static-theme.test.js: 42 uniform middleware tests -> a callStaticTheme()
    promise helper
  - handle-image-sizes + 4 routing/controller files -> a new shared
    test/utils/deferred.js helper that bridges `done` onto a promise
    (done() resolves, done(err) rejects), so existing call sites are
    unchanged
- handle-image-sizes: removed 5 dead `fakeReq.url.match = fn` lines —
  assigning a property to a string primitive is a silent no-op in mocha's
  sloppy mode and throws under vitest's strict mode; the override never
  actually installed anything

Snapshot support (first migrated bucket with snapshot tests — ghost-head):
- vitest-setup.ts: added a beforeEach that bridges @tryghost/jest-snapshot's
  per-test config. The mocha hook reads `this.currentTest`; we derive the
  same testPath/testTitle from the vitest task (testTitle must match
  mocha's fullTitle() exactly or committed .snap keys won't resolve)
- vitest.config.ts: set resolveSnapshotPath to a separate path so vitest's
  native snapshot system doesn't adopt the jest-snapshot-managed
  __snapshots__/*.snap files and report their entries as obsolete

- vitest.config.ts include += test/unit/frontend; removed from test:unit:base
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 20, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 7358d9a6-52d5-4eb0-8657-6531f7986e9b

📥 Commits

Reviewing files that changed from the base of the PR and between e2ca086 and c2446e5.

📒 Files selected for processing (1)
  • ghost/core/test/unit/frontend/web/middleware/static-theme.test.js

Walkthrough

This PR updates Vitest config to include frontend tests and custom snapshot paths, adds a deferred() test utility and a Vitest setup hook, removes frontend tests from the Mocha unit script, converts many frontend tests from Mocha callback-style to promise/async patterns, and replaces per-test Mocha lifecycle hooks with suite-level hooks (beforeAll/afterAll) across the frontend test suite.

Possibly related PRs

Suggested labels

migration, ok to merge for me

Suggested reviewers

  • kevinansfield
  • vershwal
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 11.11% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Migrated test/unit/frontend bucket to vitest' accurately summarizes the main change—moving the frontend test directory from mocha to vitest.
Description check ✅ Passed The description is directly related to the changeset, detailing the migration of test/unit/frontend from mocha to vitest with specific conversions, snapshot support, and test results.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch vitest-migrate-frontend-bucket

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@ghost/core/test/unit/frontend/web/middleware/static-theme.test.js`:
- Around line 35-38: The helper callStaticTheme currently always resolves the
Promise; change its Promise executor to accept (resolve, reject) and invoke
staticTheme()(req, res, (err) => { if (err) return reject(err); resolve(); }) so
that when the middleware calls next(err) the test helper rejects with the error
instead of resolving; update references to callStaticTheme to handle returned
rejections where needed.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 896bac57-8212-4457-9be7-94cdbee6fa13

📥 Commits

Reviewing files that changed from the base of the PR and between d076d33 and e2ca086.

📒 Files selected for processing (49)
  • ghost/core/package.json
  • ghost/core/test/unit/frontend/apps/private-blogging/controller.test.js
  • ghost/core/test/unit/frontend/helpers/asset.test.js
  • ghost/core/test/unit/frontend/helpers/body-class.test.js
  • ghost/core/test/unit/frontend/helpers/cancel-link.test.js
  • ghost/core/test/unit/frontend/helpers/comment-count.test.js
  • ghost/core/test/unit/frontend/helpers/concat.test.js
  • ghost/core/test/unit/frontend/helpers/content-api-url.test.js
  • ghost/core/test/unit/frontend/helpers/content.test.js
  • ghost/core/test/unit/frontend/helpers/foreach.test.js
  • ghost/core/test/unit/frontend/helpers/img-url.test.js
  • ghost/core/test/unit/frontend/helpers/link-class.test.js
  • ghost/core/test/unit/frontend/helpers/link.test.js
  • ghost/core/test/unit/frontend/helpers/match.test.js
  • ghost/core/test/unit/frontend/helpers/meta-description.test.js
  • ghost/core/test/unit/frontend/helpers/meta-title.test.js
  • ghost/core/test/unit/frontend/helpers/navigation.test.js
  • ghost/core/test/unit/frontend/helpers/pagination.test.js
  • ghost/core/test/unit/frontend/helpers/price.test.js
  • ghost/core/test/unit/frontend/helpers/raw.test.js
  • ghost/core/test/unit/frontend/helpers/recommendations.test.js
  • ghost/core/test/unit/frontend/helpers/search.test.js
  • ghost/core/test/unit/frontend/helpers/social-accounts.test.js
  • ghost/core/test/unit/frontend/helpers/social-url.test.js
  • ghost/core/test/unit/frontend/helpers/split.test.js
  • ghost/core/test/unit/frontend/helpers/t-new.test.js
  • ghost/core/test/unit/frontend/helpers/t.test.js
  • ghost/core/test/unit/frontend/helpers/url.test.js
  • ghost/core/test/unit/frontend/meta/context-object.test.js
  • ghost/core/test/unit/frontend/meta/description.test.js
  • ghost/core/test/unit/frontend/meta/paginated-url.test.js
  • ghost/core/test/unit/frontend/public/private.test.js
  • ghost/core/test/unit/frontend/services/assets-minification/assets-minification-base.test.js
  • ghost/core/test/unit/frontend/services/assets-minification/minifier.test.js
  • ghost/core/test/unit/frontend/services/card-assets.test.js
  • ghost/core/test/unit/frontend/services/routing/controllers/entry.test.js
  • ghost/core/test/unit/frontend/services/routing/controllers/rss.test.js
  • ghost/core/test/unit/frontend/services/routing/controllers/static.test.js
  • ghost/core/test/unit/frontend/services/rss/generate-feed.test.js
  • ghost/core/test/unit/frontend/services/sitemap/manager.test.js
  • ghost/core/test/unit/frontend/services/theme-engine/handlebars/helpers.test.js
  • ghost/core/test/unit/frontend/services/theme-engine/preview.test.js
  • ghost/core/test/unit/frontend/utils/frontend-apps.test.js
  • ghost/core/test/unit/frontend/web/middleware/frontend-caching.test.js
  • ghost/core/test/unit/frontend/web/middleware/handle-image-sizes.test.js
  • ghost/core/test/unit/frontend/web/middleware/static-theme.test.js
  • ghost/core/test/utils/deferred.js
  • ghost/core/test/utils/vitest-setup.ts
  • ghost/core/vitest.config.ts

Comment thread ghost/core/test/unit/frontend/web/middleware/static-theme.test.js
- the test helper passed `resolve` straight in as the express `next`
  callback, so a `next(err)` resolved the promise (with the error as an
  ignored value) instead of failing the test
- this mirrored the pre-migration mocha test, which used `function next()`
  with no err param + a bare `done()` — same blind spot, carried over
  faithfully during the migration
- reject on error instead, matching the shared deferred() helper. No call
  sites change: all are bare `await callStaticTheme()`, so a rejection now
  surfaces as a test failure
@9larsons 9larsons enabled auto-merge (squash) May 20, 2026 15:27
@9larsons 9larsons merged commit 6df75d8 into main May 20, 2026
43 checks passed
@9larsons 9larsons deleted the vitest-migrate-frontend-bucket branch May 20, 2026 15:53
9larsons added a commit that referenced this pull request May 20, 2026
no ref

Continues the vitest migration (after #27898 / #27900 / #27974 / #27991
/ #27992) by moving the `test/unit/server/models` bucket — 34 files, 269
tests — from mocha to vitest.
9larsons added a commit that referenced this pull request May 21, 2026
no ref

Final big bucket of the vitest migration (after #27898 / #27900 / #27974
/ #27991 / #27992 / #27996) — moves `test/unit/server/services` (256
files, ~3290 tests) from mocha to vitest.

It also fixes a **vitest worker-teardown bug** that this DB-heavy bucket
exposed (the previous buckets happened not to trigger it).
9larsons added a commit that referenced this pull request May 21, 2026
no ref

Finishes the `ghost/core/test/unit` migration from mocha to vitest
(following #27898 / #27900 / #27974 / #27991 / #27992 / #27996 /
#28009). The last 3 mocha holdouts now run on vitest, so **the mocha
unit-test path is retired** — all of `test/unit` (550 files / 6573
tests) runs on vitest.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant