Skip to content

Migrated test/unit/api bucket to vitest#27991

Merged
9larsons merged 1 commit into
mainfrom
vitest-migrate-api-bucket
May 20, 2026
Merged

Migrated test/unit/api bucket to vitest#27991
9larsons merged 1 commit into
mainfrom
vitest-migrate-api-bucket

Conversation

@9larsons
Copy link
Copy Markdown
Contributor

Summary

Continues the vitest migration (after #27898 / #27900 / #27974) by moving the test/unit/api bucket — 33 files, 280 tests — from mocha to vitest.

What's changed

  • vitest.config.ts — added test/unit/api/** to test.include
  • package.json — removed ./test/unit/api from test:unit:base so vitest is the sole runner
  • members.test.jsbefore()beforeAll()
  • serializers/input/pages.test.js + serializers/input/posts.test.js — four this.timeout(N) calls → the it(name, {timeout: N}, fn) options form
  • validators/input/pages.test.js + validators/input/posts.test.js — see below

Unhandled-rejection fix

validators/input/{pages,posts}.test.js had a beforeEach that stubbed Member.findPage with .returns(Promise.reject()). That eagerly creates a rejected promise at stub-setup time. When a test never exercises the stub, the promise is never consumed and Node flags it as an unhandled rejection — 60 of them across the two files ({ type: 'Unhandled Rejection', message: undefined }). Mocha silently ignored these; vitest surfaces them and fails the run.

Fix: switched to sinon's lazy .rejects() / .resolves(), which only create the promise when the stub is actually called.

Test plan

  • pnpm test:vitest test/unit/api (from ghost/core): 33 files / 280 tests pass, zero unhandled rejections
  • pnpm test:unit:base (mocha): 5386 tests pass
  • pnpm exec eslint clean for changed files
  • CI green

- continues the vitest migration (after #27898 / #27900 / #27974) by
  moving all 33 files in test/unit/api (280 tests) from mocha to vitest
- mocha-isms converted: members.test.js before() -> beforeAll(); four
  this.timeout() calls in serializers/input/{pages,posts}.test.js -> the
  it(name, {timeout}, fn) options form
- validators/input/{pages,posts}.test.js: the beforeEach stubbed
  Member.findPage with .returns(Promise.reject()), which eagerly creates
  a rejected promise at setup time. When a test never calls the stub,
  that promise is never consumed and surfaces as an unhandled rejection
  (60 of them under vitest, which fails the run; mocha silently ignored
  them). Switched to sinon's lazy .rejects()/.resolves() so the promise
  is only created when the stub is actually called
- added test/unit/api to vitest.config.ts include; removed it from
  test:unit:base so vitest is the sole runner
@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: 0268ec6d-3aeb-4dba-87cc-56e6889f7294

📥 Commits

Reviewing files that changed from the base of the PR and between c549856 and e423f5c.

📒 Files selected for processing (7)
  • ghost/core/package.json
  • ghost/core/test/unit/api/canary/utils/serializers/input/pages.test.js
  • ghost/core/test/unit/api/canary/utils/serializers/input/posts.test.js
  • ghost/core/test/unit/api/canary/utils/validators/input/pages.test.js
  • ghost/core/test/unit/api/canary/utils/validators/input/posts.test.js
  • ghost/core/test/unit/api/endpoints/members.test.js
  • ghost/core/vitest.config.ts

Walkthrough

This PR migrates the test/unit/api test suite from Mocha to Vitest. The core changes remove ./test/unit/api from the Mocha test:unit:base script in package.json and add test/unit/api/**/*.test.{js,ts} to Vitest's test.include configuration. Test files are updated for Vitest compatibility: timeout declarations move from this.timeout(X) calls inside test bodies to it(..., { timeout: X }) options, before() hooks change to beforeAll(), and Sinon stub patterns switch from manually returning promises to using .rejects() and .resolves() helpers.

Possibly related PRs

  • TryGhost/Ghost#27901: Introduces a new CI pnpm test:vitest step that directly depends on the vitest.config.ts changes in this PR to run the newly migrated test/unit/api tests.
  • TryGhost/Ghost#27900: Adjusts the same test runner configuration buckets in package.json and vitest.config.ts to rebalance test suite distribution between Mocha and Vitest.
  • TryGhost/Ghost#27898: Modifies vitest.config.ts test.include globs to affect which unit tests run under Vitest, using the same configuration pattern as this PR.

Suggested reviewers

  • EvanHahn
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The pull request objective regarding Vitest migration is unrelated to the linked issue #39 about image upload markdown functionality; no coding-related requirements from issue #39 are addressed. The pull request addresses Vitest migration but does not implement any requirements from the linked issue #39. Verify that issue #39 requirements are intentionally excluded or link the appropriate issue that describes the Vitest migration work.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main objective of the pull request: migrating the test/unit/api bucket from Mocha to Vitest.
Description check ✅ Passed The description thoroughly explains the changes, including what was migrated, how Mocha-specific APIs were converted, and documents the unhandled-rejection fix that was necessary for Vitest compatibility.
Out of Scope Changes check ✅ Passed All changes in the pull request are directly related to the Vitest migration objective: configuration updates, Mocha-to-Vitest API conversions, and unhandled-rejection fixes. No unrelated changes are present.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ 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-api-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.

@9larsons 9larsons merged commit d076d33 into main May 20, 2026
44 checks passed
@9larsons 9larsons deleted the vitest-migrate-api-bucket branch May 20, 2026 13:49
9larsons added a commit that referenced this pull request May 20, 2026
no ref

Continues the vitest migration (after #27898 / #27900 / #27974 / #27991)
by moving the `test/unit/frontend` bucket — 135 files, 1804 tests — from
mocha to vitest.
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