twilio-functions-utils
Advanced tools
Comparing version 2.0.4 to 2.1.0
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, |
{ | ||
"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/) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19697
17
340
225
5
+ Added@folder/readdir@^3.1.0
+ Added@folder/readdir@3.1.0(transitive)
+ Added@types/node@22.10.0(transitive)
+ Addedaxios@1.7.8(transitive)
+ Addedundici-types@6.20.0(transitive)
- Removed@types/node@22.9.3(transitive)
- Removedaxios@1.7.7(transitive)
- Removedundici-types@6.19.8(transitive)