From c0704b2b816b1e585bc14d5cfb7e8f44e9c46fd6 Mon Sep 17 00:00:00 2001 From: Michael Carpenter Date: Thu, 9 Apr 2026 16:23:29 -0700 Subject: [PATCH] feat(@twilio/runtime-handler): export Response class for testing Export the Response class from the package root, enabling unit testing of Twilio Functions without custom mock implementations. Previously, the main entry point was a placeholder that threw on import. Now require('@twilio/runtime-handler') returns { Response }, providing the same class used by the local dev server with full method support (setStatusCode, setBody, appendHeader, setCookie, etc). Closes #384 --- .changeset/export-response-class.md | 5 +++++ .../__tests__/runtime-handler.test.ts | 14 ++++++++++---- packages/runtime-handler/package.json | 9 +++++---- packages/runtime-handler/src/index.ts | 2 +- 4 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 .changeset/export-response-class.md diff --git a/.changeset/export-response-class.md b/.changeset/export-response-class.md new file mode 100644 index 00000000..604b6c29 --- /dev/null +++ b/.changeset/export-response-class.md @@ -0,0 +1,5 @@ +--- +"@twilio/runtime-handler": minor +--- + +Export Response class from the package root for use in testing. Previously, the package's main entry was a placeholder that threw on import. Now `const { Response } = require('@twilio/runtime-handler')` provides the same Response class used by the local dev runtime, enabling proper unit testing of Twilio Functions without custom mocks. diff --git a/packages/runtime-handler/__tests__/runtime-handler.test.ts b/packages/runtime-handler/__tests__/runtime-handler.test.ts index 629907f5..e544e1f1 100644 --- a/packages/runtime-handler/__tests__/runtime-handler.test.ts +++ b/packages/runtime-handler/__tests__/runtime-handler.test.ts @@ -1,5 +1,11 @@ -test('base should not be importable', () => { - expect(() => { - require('..'); - }).toThrow(); +test('base export includes Response class', () => { + const { Response } = require('..'); + expect(Response).toBeDefined(); + expect(typeof Response).toBe('function'); + + const res = new Response(); + expect(res.setStatusCode).toBeDefined(); + expect(res.setBody).toBeDefined(); + expect(res.appendHeader).toBeDefined(); + expect(res.setCookie).toBeDefined(); }); diff --git a/packages/runtime-handler/package.json b/packages/runtime-handler/package.json index ba9a62d0..3abad227 100644 --- a/packages/runtime-handler/package.json +++ b/packages/runtime-handler/package.json @@ -9,11 +9,12 @@ "author": "Twilio Inc. (https://www.twilio.com/labs)", "homepage": "https://github.com/twilio-labs/serverless-toolkit/tree/main/packages/runtime-handler#readme", "license": "MIT", - "main": "dist/invalid.js", - "types": "dist/invalid.d.ts", + "main": "dist/index.js", + "types": "dist/index.d.ts", "exports": { - ".": "./dist/invalid.js", - "./dev": "./dist/dev-runtime/dev-runtime.js" + ".": "./dist/index.js", + "./dev": "./dist/dev-runtime/dev-runtime.js", + "./testing": "./dist/index.js" }, "directories": { "src": "src", diff --git a/packages/runtime-handler/src/index.ts b/packages/runtime-handler/src/index.ts index ff7bd09c..2d250c54 100644 --- a/packages/runtime-handler/src/index.ts +++ b/packages/runtime-handler/src/index.ts @@ -1 +1 @@ -// placeholder +export { Response } from './dev-runtime/internal/response';