Skip to content

Swapped models barrel for single-model requires in ghost/core unit tests#28750

Merged
9larsons merged 3 commits into
mainfrom
unit-models-single-require
Jun 19, 2026
Merged

Swapped models barrel for single-model requires in ghost/core unit tests#28750
9larsons merged 3 commits into
mainfrom
unit-models-single-require

Conversation

@9larsons

Copy link
Copy Markdown
Contributor

ref https://linear.app/tryghost/issue/PLA-45

57 files in test/unit/** required the full core/server/models barrel — which loads bookshelf, every model, and every plugin (~223ms cold per worker) — when each only exercises one or two models. This swaps 43 of them to require the specific model file(s) directly (e.g. const {Tag} = require('.../models/tag')), so test workers stop paying the cold barrel-load cost. Behaviour is unchanged: the single-model files export the same bindings the barrel re-exports, and models.Base maps to the base module (ghostBookshelf).

Kept on the barrel by design (14 files): tests that genuinely touch three-plus models; members/middleware.test.js (mutates models.Product on the shared barrel object the SUT reads) and milestone-queries.test.js (deliberate import-time full-layer require, documented in-file); and the six files needing models.Usermodels/user pulls in services/permissions, which re-requires the barrel, so requiring models/user as a worker's first model load re-enters the barrel mid-init and throws via author.js.

Tests-only change; no production code touched. Full unit suite green (7020 passing), lint clean.

9larsons added 3 commits June 19, 2026 11:27
… tests

ref https://linear.app/tryghost/issue/PLA-45

These unit tests required the full core/server/models barrel (bookshelf +
every model + every plugin) when they each only exercise one or two models.
Switched them to require the specific model file(s) directly so each test
worker stops paying the cold barrel-load cost. The single-model files export
the same {Model, Models} bindings the barrel re-exports, and models.Base maps
to the base module (ghostBookshelf), so behaviour is unchanged.

user.test.js deliberately stays on the barrel: models/user pulls in
services/permissions, which re-requires the barrel, so requiring models/user
as a worker's first model load re-enters the barrel mid-init and throws
("Cannot read properties of undefined (reading 'extend')" via author.js).
…es tests

ref https://linear.app/tryghost/issue/PLA-45

Same barrel-load reduction as the models-test commit, applied to the
server/services unit tests. Three files deliberately keep the full barrel
require and are untouched: members/middleware.test.js swaps models.Product on
the shared barrel object that the middleware under test reads;
milestone-queries.test.js requires the whole model layer at import time on
purpose (to construct Bookshelf against the real knex before db.knex is
stubbed) — its comment documents why; and auth/session/middleware.test.js +
invitations/accept.test.js both need models.User, which is not safe to
single-require (see the models-test commit message).
…tests

ref https://linear.app/tryghost/issue/PLA-45

Final slice of the barrel-load reduction, covering the api validator/endpoint
unit tests and the data exporter/importer tests. data/schema/validator.test.js
and data/schema/fixtures/fixture-manager.test.js keep the full barrel (they
exercise three-plus models each), and the api session/db/members tests keep it
too because they need models.User, which is not safe to single-require (see the
models-test commit message).
@coderabbitai

coderabbitai Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

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: 9b3243e5-018b-41c6-9199-aff23b964639

📥 Commits

Reviewing files that changed from the base of the PR and between 807fcf3 and 6f1d9b0.

📒 Files selected for processing (43)
  • 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/previews.test.js
  • ghost/core/test/unit/server/data/exporter/index.test.js
  • ghost/core/test/unit/server/data/importer/index.test.js
  • ghost/core/test/unit/server/models/api-key.test.js
  • ghost/core/test/unit/server/models/automation.test.js
  • ghost/core/test/unit/server/models/base/actions.test.js
  • ghost/core/test/unit/server/models/base/crud.test.js
  • ghost/core/test/unit/server/models/base/index.test.js
  • ghost/core/test/unit/server/models/base/relations.test.js
  • ghost/core/test/unit/server/models/comment.test.js
  • ghost/core/test/unit/server/models/custom-theme-setting.test.js
  • ghost/core/test/unit/server/models/integration.test.js
  • ghost/core/test/unit/server/models/invite.test.js
  • ghost/core/test/unit/server/models/member-click-event.test.js
  • ghost/core/test/unit/server/models/member-created-event.test.js
  • ghost/core/test/unit/server/models/member-feedback.test.js
  • ghost/core/test/unit/server/models/member-paid-subscription-event.test.js
  • ghost/core/test/unit/server/models/member-subscribe-event.test.js
  • ghost/core/test/unit/server/models/member.test.js
  • ghost/core/test/unit/server/models/milestone.test.js
  • ghost/core/test/unit/server/models/newsletter.test.js
  • ghost/core/test/unit/server/models/outbox.test.js
  • ghost/core/test/unit/server/models/permission.test.js
  • ghost/core/test/unit/server/models/post.test.js
  • ghost/core/test/unit/server/models/session.test.js
  • ghost/core/test/unit/server/models/settings.test.js
  • ghost/core/test/unit/server/models/single-use-token.test.js
  • ghost/core/test/unit/server/models/stripe-customer-subscription.test.js
  • ghost/core/test/unit/server/models/subscription-created-event.test.js
  • ghost/core/test/unit/server/models/tag.test.js
  • ghost/core/test/unit/server/models/welcome-email-automated-email.test.js
  • ghost/core/test/unit/server/models/welcome-email-automation-run.test.js
  • ghost/core/test/unit/server/services/auth/api-key/admin.test.js
  • ghost/core/test/unit/server/services/auth/api-key/content.test.js
  • ghost/core/test/unit/server/services/auth/session/store.test.js
  • ghost/core/test/unit/server/services/milestones/bookshelf-milestone-repository.test.js
  • ghost/core/test/unit/server/services/newsletters/service.test.js
  • ghost/core/test/unit/server/services/outbox/handlers/member-created.test.js
  • ghost/core/test/unit/server/services/post-scheduling/post-scheduling.test.js
  • ghost/core/test/unit/server/services/settings/settings-service.test.js
  • ghost/core/test/unit/server/services/webhooks/serialize.test.js

Walkthrough

Across approximately 40 unit test files in ghost/core/test/unit, the pattern of importing the aggregated models barrel (require('.../models')) and referencing individual models via models.ModelName.* is replaced with direct per-model imports (require('.../models/model-name')). All sinon stubs, model instantiations (new ModelName(), ModelName.forge()), and static method calls are updated to target the directly imported class. No test assertions, behavioral logic, or test structure changed.

Possibly related PRs

  • TryGhost/Ghost#27658: Renamed the welcomeEmailAutomation relation to automation in welcome-email-automation-run.js, which directly affects the relationship assertion tested in the welcome-email-automation-run.test.js file modified in this PR.
  • TryGhost/Ghost#28647: Modified the outbox member-created handler test's import and stubbing wiring, overlapping with the same test file updated in this PR.

Suggested labels

ok to merge for me

Suggested reviewers

  • kevinansfield
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: swapping barrel model imports for single-model requires across unit tests.
Description check ✅ Passed The description is detailed and directly related to the changeset, explaining the rationale, scope, and specific design decisions about which tests remain on the barrel.
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 unit-models-single-require

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.

@nx-cloud

nx-cloud Bot commented Jun 19, 2026

Copy link
Copy Markdown

🤖 Nx Cloud AI Fix

Ensure the fix-ci command is configured to always run in your CI pipeline to get automatic fixes in future runs. For more information, please see https://nx.dev/ci/features/self-healing-ci


View your CI Pipeline Execution ↗ for commit 6f1d9b0

Command Status Duration Result
nx run-many -t test:unit -p ghost ✅ Succeeded 34s View ↗
nx run-many -t lint -p ghost ✅ Succeeded 43s View ↗

💡 Verify your cache is correct by running tasks in a sandbox. Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-06-19 16:37:12 UTC

@9larsons 9larsons merged commit bfc5ef6 into main Jun 19, 2026
31 checks passed
@9larsons 9larsons deleted the unit-models-single-require branch June 19, 2026 16:49
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