Skip to content

🧪 test: Add test for Arcjet rate limit error response - #11

Open
somyaknotfound wants to merge 1 commit into
mainfrom
feat/test-arcjet-rate-limit-7414135327331669581
Open

🧪 test: Add test for Arcjet rate limit error response#11
somyaknotfound wants to merge 1 commit into
mainfrom
feat/test-arcjet-rate-limit-7414135327331669581

Conversation

@somyaknotfound

Copy link
Copy Markdown
Owner

🎯 What: The testing gap addressed is the lack of unit tests for the securityMiddleware in src/arcjet.js, specifically the testing of its error responses including when a rate limit is hit.
📊 Coverage: Scenarios covered:

  • Allowed request calls next() successfully.
  • Rate limited request returns a 429 status code with an appropriate error message.
  • Denied requests not caused by a rate limit return a 403 status code with a Forbidden message.
  • Expected Internal errors when arcjet.protect() throws return a 503 status code with a Service Unavailable message.
    Result: Enhanced the testing coverage specifically for our security middleware to handle these varying responses correctly and confidently, adding easy execution from the CLI via npm test.

PR created automatically by Jules for task 7414135327331669581 started by @somyaknotfound

Adds test coverage to `src/arcjet.js` ensuring that the `securityMiddleware`
correctly handles rate-limited requests, regular denied requests,
allowed requests, and internally thrown errors by mocking `@arcjet/node`
responses using the `node:test` framework.

Also updates `package.json` adding the `--experimental-test-module-mocks`
flag to the `test` script to properly resolve the mocks.

Co-authored-by: somyaknotfound <118343482+somyaknotfound@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 9, 2026 06:51
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@coderabbitai

coderabbitai Bot commented Mar 9, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@somyaknotfound has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 27 minutes and 31 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 47e522ab-9867-4498-b459-26c9a4034b26

📥 Commits

Reviewing files that changed from the base of the PR and between 469b8b0 and 4d63138.

📒 Files selected for processing (2)
  • package.json
  • tests/arcjet.test.js
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/test-arcjet-rate-limit-7414135327331669581

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds unit test coverage for the Arcjet securityMiddleware (src/arcjet.js) to validate expected HTTP responses for allow/deny/rate-limit and error cases, and wires up an npm test command to run the suite.

Changes:

  • Added node:test-based unit tests for securityMiddleware covering allow, rate-limit (429), deny (403), and protect-throw (503) behaviors.
  • Added an npm test script using Node’s test runner with experimental ESM module mocking enabled.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
tests/arcjet.test.js New unit tests for Arcjet middleware responses using node:test + mock.module()
package.json Adds npm test script invoking node --test with --experimental-test-module-mocks

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread tests/arcjet.test.js
Comment on lines +28 to +31
test('securityMiddleware allows request when Arcjet allows', async () => {
mockArcjetInstance.protect.mock.mockImplementationOnce(async () => ({
isDenied: () => false
}));

Copilot AI Mar 9, 2026

Copy link

Choose a reason for hiding this comment

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

These tests share a single mockArcjetInstance.protect mock and use mockImplementationOnce(). Since Node’s test runner can execute tests concurrently, the one-time implementations can be consumed by a different test than intended, making the suite flaky. Consider isolating the mock per test (create a new Arcjet instance/middleware per test) or disable concurrency for this file/tests and reset the mock between tests.

Copilot uses AI. Check for mistakes.
Comment thread tests/arcjet.test.js
Comment on lines +107 to +114
// Suppress console.error for this specific test
const originalConsoleError = console.error;
console.error = () => {};

t.after(() => {
console.error = originalConsoleError;
});

Copilot AI Mar 9, 2026

Copy link

Choose a reason for hiding this comment

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

Overriding console.error globally can interfere with other tests (especially if tests run concurrently) and may not be restored if the process exits early. Prefer using the test runner’s mocking utilities (e.g., mock.method(console, 'error', ...) or t.mock.method(...)) so the stub is scoped to the test and automatically restored.

Suggested change
// Suppress console.error for this specific test
const originalConsoleError = console.error;
console.error = () => {};
t.after(() => {
console.error = originalConsoleError;
});
// Suppress console.error for this specific test using scoped mock
mock.method(console, 'error', () => {});

Copilot uses AI. Check for mistakes.
Comment thread package.json
Comment on lines +10 to +11
"db:demo": "node src/db-demo.js",
"test": "node --experimental-test-module-mocks --test"

Copilot AI Mar 9, 2026

Copy link

Choose a reason for hiding this comment

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

Because the test suite relies on module mocking (mock.module(...)), it requires running Node with --experimental-test-module-mocks. Adding this to the default npm test script may be fine, but consider documenting the required Node version/flags (e.g., via an engines.node constraint or README note) to prevent CI/local failures on older Node versions.

Copilot uses AI. Check for mistakes.
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.

2 participants