Skip to content

Conversation

@crespowang
Copy link
Collaborator

@crespowang crespowang commented Nov 24, 2025

Description

Investigated Test Failures

Problem: Tests were failing with "Unauthorized" errors when running npm test

Root Cause Discovered:

  • Serverless Framework v4 uses a compiled binary (Go) instead of pure JavaScript
  • The binary makes HTTP requests to https://core.serverless.com/api for authentication
  • Nock mocks don't work because they can only intercept requests in the same Node.js
    process, not external binaries
  • The mock API key approach was fundamentally broken

Solution: Switched to using real Serverless API keys for testing


Removed Node.js 14, 16, and 18 Support 🗑️

AWS only supports NodeJS 20+

Key Changes:

  • src/config/supportedRuntimes.js: Removed nodejs14.x, nodejs16.x, nodejs18.x from
    supported runtimes
    • Now only supports: Node.js 20, 22, and 24
  • src/config/constants.js: Updated DEFAULT_LAMBDA_RUNTIME from nodejs14.x → nodejs20.x
  • .github/workflows/nodejs.yml: Updated CI matrix to test only Node.js 20, 22, 24
  • package.json: Updated minimum Node version: >=18.12.0 → >=20.0.0
  • Deleted: tests/integration/docker/nodejs/{nodejs14.x,nodejs16.x,nodejs18.x}/ directories
  • Updated: 71 serverless.yml files changed runtime: nodejs14.x/16.x/18.x → runtime:
    nodejs20.x

Implemented Real API Key Authentication

Problem: Tests needed a way to authenticate with Serverless Framework v4

Solution Implemented:

A. Added Pre-Test Validation (tests/mochaHooks.cjs)

  • Checks if SERVERLESS_ACCESS_KEY environment variable is set before any tests run
  • If not set: immediately exits with process.exit(1) and displays helpful error message
  • Prevents cascading errors from running tests without authentication

B. Updated Test Setup (tests/_testHelpers/setupTeardown.js)

  • Removed hardcoded MOCK_ACCESS_KEY
  • Now reads SERVERLESS_ACCESS_KEY from environment
  • Removed nock mock setup (no longer needed)
  • Simplified code with comment explaining validation happens in mochaHooks

C. Updated CI Workflow (.github/workflows/nodejs.yml)

  • Added SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }} to environment
  • Requires GitHub repository secret to be configured

D. Cleaned Up Unused Files

  • Deleted: tests/_testHelpers/serverlessApiMockSetup.cjs (non-functional nock mock)
  • Removed: Unused join import from desm package

Motivation and Context

How Has This Been Tested?

Screenshots (if appropriate):

@crespowang crespowang self-assigned this Nov 24, 2025
This commit modernizes the codebase to support only Node.js 20+ and fixes
test failures caused by Serverless Framework v4's authentication mechanism.

## Summary of Changes

### 1. Removed Node.js 14, 16, and 18 Support (78 files modified)

- Update supported runtimes to Node.js 20, 22, and 24 only
- Remove nodejs14.x, nodejs16.x, nodejs18.x from supportedRuntimes
- Update DEFAULT_LAMBDA_RUNTIME from nodejs14.x to nodejs20.x
- Update package.json minimum Node version: >=18.12.0 → >=20.0.0
- Update CI workflow to test only Node.js 20, 22, 24
- Delete nodejs14.x, nodejs16.x, nodejs18.x test directories
- Update 71 serverless.yml files to use nodejs20.x runtime

### 2. Fixed Test Authentication for Serverless Framework v4

**Root Cause**: Serverless v4 uses a compiled binary that makes HTTP requests
for authentication. Nock mocks cannot intercept requests from external binaries,
only from the same Node.js process.

**Solution**: Require real SERVERLESS_ACCESS_KEY environment variable

- Add pre-test validation in mochaHooks.cjs that exits immediately if key not set
- Update setupTeardown.js to use SERVERLESS_ACCESS_KEY from environment
- Remove non-functional nock mock setup (serverlessApiMockSetup.cjs)
- Add SERVERLESS_ACCESS_KEY to GitHub Actions workflow (requires secret)
- Display helpful error message with instructions when key is missing

### 3. Files Changed

Core configuration:
- src/config/supportedRuntimes.js
- src/config/constants.js
- package.json
- .github/workflows/nodejs.yml

Test infrastructure:
- tests/mochaHooks.cjs (added validation)
- tests/_testHelpers/setupTeardown.js (use real API key)
- tests/_testHelpers/serverlessApiMockSetup.cjs (deleted)

Configuration files:
- 71 serverless.yml files updated across tests/, examples/, and integration tests
- 2 markdown issue templates updated

### How to Run Tests

Locally:
```bash
# Option 1: Login with Serverless
npx serverless login

# Option 2: Set API key directly
export SERVERLESS_ACCESS_KEY=your_key_here

# Run tests
npm test
```

In CI: Add SERVERLESS_ACCESS_KEY to GitHub repository secrets

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@crespowang crespowang changed the title feat: Add support for Node.js 24.x Drop pre-NodeJS 20.x support and add support for NodeJS 24.x and Fix broken tests Nov 24, 2025
@darkmavis1980
Copy link

Waiting for this PR to be merged as now I can't use serverless-offline with node.js 24, which is now officially supported by AWS

@crespowang
Copy link
Collaborator Author

@dherault Is there anything else need to be done to get this merged?

@darkmavis1980
Copy link

I tested this branch and it seems to work fine for me, but there are 4 failing checks

cwd: servicePath,
env: {
SERVERLESS_ACCESS_KEY: "MOCK_ACCESS_KEY",
SERVERLESS_ACCESS_KEY: serverlessAccessKey,

Choose a reason for hiding this comment

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

Do you need to pass an actual serverless key here instead of using a mock?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Serverless Framework v4 uses a compiled binary (Go) instead of pure JavaScript
The binary makes HTTP requests to https://core.serverless.com/api for authentication
Nock mocks don't work because they can only intercept requests in the same Node.js
process, not external binaries so the mock API key approach was fundamentally broken

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.

3 participants