Skip to content

Updated sinon to v19 #23866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Updated sinon to v19 #23866

wants to merge 4 commits into from

Conversation

ErisDS
Copy link
Member

@ErisDS ErisDS commented Jun 16, 2025

  • Stepping through the major versions one-by-one

- Stepping through the major versions one-by-one
Copy link
Contributor

coderabbitai bot commented Jun 16, 2025

Walkthrough

This change updates the version of the sinon package in the devDependencies section of four separate package.json files: apps/admin-x-design-system, apps/admin-x-framework, apps/shade, and ghost/core. In each file, the sinon version is upgraded from 18.0.1 to 19.0.5. Additionally, multiple test files under ghost/core/test/e2e-api and ghost/core/test/unit were modified to refine the use of sinon.useFakeTimers by explicitly specifying which timer functions to fake (setTimeout, clearTimeout, setInterval, clearInterval, and Date), passing options objects instead of single arguments, enabling automatic time advancement in some tests, and adding proper restoration of fake timers after tests. No other dependencies, scripts, or configurations were modified, and there are no changes to the declarations of exported or public entities.

Possibly related PRs

  • Upgraded sinon to v18 #23848: Updates the sinon package version in the same set of package.json files, representing an earlier step in the sinon upgrade path (from 17.0.1 to 18.0.1).
  • Upgraded sinon to v17 #23844: Upgrades the sinon package version in ghost/core/package.json from 15.2.0 to 17.0.1, which is part of the sequential upgrades leading up to the current change.

Suggested labels

browser-tests

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

- Sinon v19 now fakes ALL timers by default including nextTick/queueMicroTask
- This breaks async operations in tests as they depend on nextTick to resolve
- Used explicit toFake array to exclude nextTick while keeping timer control
- Alternative shouldAdvanceTime:true considered but rejected for determinism
- Affects tests that mix timer mocking with HTTP requests/async operations
Copy link
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

🧹 Nitpick comments (3)
ghost/core/test/e2e-api/admin/actions.test.js (2)

25-27: Fake-timer scope could be broader & deterministic

You’re now explicitly listing the functions to fake, but you dropped setImmediate, clearImmediate and nextTick (faked by default in Sinon ≤18).
If any code under test schedules work with these, the ticks you perform won’t advance it, resulting in flakiness that only appears on CI.

While touching this line, you could also pin the start time for full repeatability.

-const clock = sinon.useFakeTimers({
-    toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'Date']
-});
+const clock = sinon.useFakeTimers({
+    now: Date.now(), // deterministic baseline
+    toFake: [
+        'setTimeout', 'clearTimeout',
+        'setInterval', 'clearInterval',
+        'setImmediate', 'clearImmediate',
+        'nextTick',
+        'Date'
+    ]
+});

141-142: Duplicate restore – move to finally or rely on the after hook

clock.restore() is already implicitly executed by sinon.restore() in the after hook.
Calling it here is harmless but redundant; if an assertion throws before this line, the call is skipped anyway.

Consider either:

  1. Removing these two lines and letting the after hook handle cleanup, or
  2. Wrapping the test body in try … finally { clock.restore(); } to guarantee restoration on failure.

Leaving as-is is safe but slightly noisy.

ghost/core/test/e2e-api/members/signin.test.js (1)

321-324: Include setImmediate/nextTick in fake timers for full coverage

Same remark as earlier: with Sinon 19 you need to list everything you expect to be faked.
Adding the two omissions avoids surprises if the rate-limit implementation ever switches to setImmediate/process.nextTick.

-clock = sinon.useFakeTimers({
-    now: new Date(),
-    toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'Date']
-});
+clock = sinon.useFakeTimers({
+    now: Date.now(),
+    toFake: [
+        'setTimeout', 'clearTimeout',
+        'setInterval', 'clearInterval',
+        'setImmediate', 'clearImmediate',
+        'nextTick',
+        'Date'
+    ]
+});
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2ed11b8 and 10880dc.

📒 Files selected for processing (4)
  • ghost/core/test/e2e-api/admin/actions.test.js (2 hunks)
  • ghost/core/test/e2e-api/admin/links.test.js (1 hunks)
  • ghost/core/test/e2e-api/members/feedback.test.js (1 hunks)
  • ghost/core/test/e2e-api/members/signin.test.js (2 hunks)
✅ Files skipped from review due to trivial changes (2)
  • ghost/core/test/e2e-api/members/feedback.test.js
  • ghost/core/test/e2e-api/admin/links.test.js
🧰 Additional context used
🧬 Code Graph Analysis (1)
ghost/core/test/e2e-api/members/signin.test.js (3)
ghost/core/test/e2e-api/admin/links.test.js (2)
  • clock (20-20)
  • sinon (3-3)
ghost/core/test/e2e-api/admin/actions.test.js (2)
  • clock (25-27)
  • sinon (2-2)
ghost/core/test/e2e-api/members/feedback.test.js (2)
  • clock (13-13)
  • sinon (5-5)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Setup

Comment on lines +188 to +191
clock = sinon.useFakeTimers({
now: startDate,
toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'Date']
});
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

startDate is shared between tests – take a fresh copy per run

startDate is declared once outside the beforeEach; after the first test it no longer reflects the real start of subsequent tests.
Although you pass the timestamp into useFakeTimers, re-using the mutated object risks hidden coupling between tests.

-beforeEach(async function () {
-    // Remove ms precision (not supported by MySQL)
-    startDate.setMilliseconds(0);
-    clock = sinon.useFakeTimers({
-        now: startDate,
+beforeEach(async function () {
+    // Fresh baseline, no ms precision (not supported by MySQL)
+    const now = new Date();
+    now.setMilliseconds(0);
+    clock = sinon.useFakeTimers({
+        now,
         toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'Date']
     });
 });

This keeps each test isolated and easier to reason about.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
clock = sinon.useFakeTimers({
now: startDate,
toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'Date']
});
beforeEach(async function () {
// Fresh baseline, no ms precision (not supported by MySQL)
const now = new Date();
now.setMilliseconds(0);
clock = sinon.useFakeTimers({
now,
toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'Date']
});
});
🤖 Prompt for AI Agents
In ghost/core/test/e2e-api/members/signin.test.js around lines 188 to 191, the
startDate object is declared once outside the beforeEach hook and reused across
tests, causing shared mutable state that can lead to coupling between tests. To
fix this, move the creation or cloning of startDate inside the beforeEach so
that each test gets a fresh, independent copy before calling
sinon.useFakeTimers. This ensures test isolation and prevents side effects from
previous tests.

ErisDS added 2 commits June 16, 2025 09:19
• Sinon v19 fake timers create different Date object instances

• Test was comparing Date objects created before/after fake timer setup

• Fixed by creating expected date using same faked Date constructor

• Ensures both dates are created with same timer context
Copy link
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: 0

🧹 Nitpick comments (2)
ghost/core/test/unit/server/adapters/scheduling/SchedulingDefault.test.js (2)

254-256: Store the fake-clock instance for clearer teardown & control

Inside this test you create a second fake clock but don’t keep a reference:

sinon.restore();
sinon.useFakeTimers({ shouldAdvanceTime: true });

Because the instance isn’t assigned, you lose programmatic control (runAll(), tick(), etc.) and rely on real passage of time. Capturing the handle also makes the final sinon.restore() unnecessary and avoids accidental leaks if another stub is added later.

-sinon.restore();
-sinon.useFakeTimers({
-    shouldAdvanceTime: true
-});
+sinon.restore();
+const pingClock = sinon.useFakeTimers({ shouldAdvanceTime: true });
+// … test body …
+pingClock.restore(); // explicit & local – avoids double restore confusion

337-339: Duplicate pattern – apply same explicit clock handle

Same comment as above: keep the returned fake-timer so the test remains deterministic and self-contained.

-sinon.useFakeTimers({
-    shouldAdvanceTime: true
-});
+const retryClock = sinon.useFakeTimers({ shouldAdvanceTime: true });
+// … test …
+retryClock.restore();
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 10880dc and 4a475a8.

📒 Files selected for processing (2)
  • ghost/core/test/unit/server/adapters/scheduling/SchedulingDefault.test.js (1 hunks)
  • ghost/core/test/unit/server/models/user.test.js (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • ghost/core/test/unit/server/models/user.test.js
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Setup
🔇 Additional comments (1)
ghost/core/test/unit/server/adapters/scheduling/SchedulingDefault.test.js (1)

16-18: Nice upgrade – automatic time-advancing clock simplifies tests

Switching to shouldAdvanceTime: true means all the setTimeout / setInterval calls progress in real time without manual clock.tick(...). This fits the test suite which already uses real-time polling in several places.

No further concerns here.

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