Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

twilio-functions-utils

Package Overview
Dependencies
Maintainers
1
Versions
69
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

twilio-functions-utils - npm Package Compare versions

Comparing version 2.0.4 to 2.1.0

lib/twilio.mock.js

2

index.js
const { useInjection, typeOf } = require('./lib/use.injection');
const { useMock } = require('./lib/use.mock');
const { BadRequestError } = require('./lib/errors/bad-request.error');

@@ -9,2 +10,3 @@ const { InternalServerError } = require('./lib/errors/internal-server.error');

module.exports = {
useMock,
useInjection,

@@ -11,0 +13,0 @@ Response,

3

package.json
{
"name": "twilio-functions-utils",
"version": "2.0.4",
"version": "2.1.0",
"description": "Twilio Functions utils library",

@@ -37,2 +37,3 @@ "main": "index.js",

"dependencies": {
"@folder/readdir": "^3.1.0",
"@twilio/runtime-handler": "^1.2.3",

@@ -39,0 +40,0 @@ "lodash": "^4.17.21",

@@ -165,4 +165,61 @@

## Testing with `useMock`
The Twilio Serverless structure make it hard for testing sometimes. So this provides a method that works perfectly with useInjection ready functions. The `useMock` act like useInjection but mocking some required fragments as `getAssets` and `getFunctions`.
Exports your function:
```js
async function functionToBeTested(event) {
const something = await this.providers.myCustomProvider(event)
return Response(something)
}
exports.handler = useInjection(functionToBeTested, {
providers: {
myCustomProvider,
},
});
module.exports = { functionToBeTested }; // <--
```
You always need to import the twilio.mock for Response Twilio Global object on your testing files begining. **(Required)**
```js
require('twilio-functions-utils/lib/twilio.mock');
```
Use Twilio Functions Utils `useMock` to do the hard job and just write your tests with the generated function.
```js
/* global describe, it, expect */
require('twilio-functions-utils/lib/twilio.mock');
const { useMock, Response } = require('twilio-functions-utils');
const { functionToBeTested } = require('../../functions/functionToBeTested'); // <-- Import here!
// Create the test function from the function to be tested
const fn = useMock(functionToBeTested, {
providers: {
myCustomProvider: async (sid) => ({ sid }), // Mock the providers implementation.
},
});
describe('Function functionToBeTested', () => {
it('if {"someValue": true}', async () => {
const request = { TaskSid: '1234567', TaskAttributes: '{"someValue": true}' };
const res = await fn(request);
expect(res).toBeInstanceOf(Response);
expect(res.body).not.toEqual(request);
expect(res.body).toEqual({ sid: '1234567' });
});
});
```
## Author
- [Iago Calazans](https://github.com/iagocalazans) - 🛠 Senior Node.js Engineer at [Stone](https://www.stone.com.br/)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc