SM JavaScript test utilities
Package to help mock Endpoints, functions and middleware for Express and Azure Functions, without having to spin up a webserver.
This package was previously named azure-function-mocker. However, because we have added utilities for Express applications as well, has the package been renamed to
sm-js-test-utils
.
This package is maintained by the organization Studentmedia in Trondheim inc., hence the sm
prefix in the name. Any feedback, questions or change-requests can be issued on the projects github page. However, feel free to open pull requests with the changes implemented.
Installation
npm install --save-dev sm-js-test-utils
API-reference
Since the module contains utilities for both express and azure functions, are their respective features grouped together.
- Azure Functions utilities are located in
azfun
- Express utilities are located in
express
.
Request Mocker (azfun)
Creates a mocked HttpRequest, which is accepted as an argument by FunctionMocker.run
.
Parameters
name | type | Null | description | legal values |
---|---|---|---|---|
method | string |
Yes | The HTTP method to mock | POST , GET , PUT , PATCH , HEAD , OPTION |
params | object |
Yes | The URL parameters to include | All key-value objects |
query | object |
Yes | The Query parameteres to include | All key-value objects |
Examples
Simple GET request
const mockRequest = azure; const req = ; // GET, could also be explicitt and write it in.
Simple request with non-GET method
const mockRequest = azure; const req = ; // or 'PUT', 'PATCH', 'HEAD', 'OPTION', ...
Function Mocker (azfun)
Creates a mocked environment for which the function can run in. Simply said appends a mocked context to the first function parameter, and returns the updated context when the function has completed.
Examples
Simple HttpTriggered function
The most simple use-case to mock a function.
getHelloWorld
takes no other arguments than the required context
-argument,
and returns a simple HTTP 200 response with the body { message: 'Hello world' }
const FunctionMocker = azure; { contextres = status: 200 body: message: 'Hello world' ; context;} const func = getHelloWorld; const ctx = await func; console; // 200console; // { message: 'Hello world' }
HttpTriggered function with request object
A more complex version of the previous example, but yet quite simple.
const FunctionMocker mockRequest = azure; { if !reqparamsname contextres = status: 400 body: error: 'Missing name!' ; else contextres = status: 200 body: message: `Hello ` ; context;} const req = ; // Mocks a request with a POST-body const func = postHelloWorld; const ctx = await func; console; // { messsage: 'Hello Jon Snow' }
HttpTriggered function returning a Promise
An Azure Function also supports ending the function through Promise.resolve()
.
We will in this example use async/await
, as it saves us syntax-space, the function will
in reality return a promise (see ES2017 spec for more details).
const FunctionMocker = azure; { contextres = status: 200 body: message: 'Hello Async World' ;} const func = getHelloWorld; const ctx = await func; console; // { message: 'Hello Async World' }
As we see, there is no practical difference for the FunctionMocker
, and what your output after func.run()
is.
Context Mocker (azfun)
Function which takes an callback-function,
that is called when the Azure Function completes by using context.done()
Note: Azure Functions also supports the use of promises, thus an function can complete by calling
Promise.resolve()
, which won't trigger the callback-function
const mockContext = azure; // Alternative 1; // Alternative 2// Usefull when working with async/awaitconst context = ;
The last alternative is usefull when working with async/await
, as the constant context
will also contain the updated values.
You will however, rarelly need to work directly with mockContext()
as FunctionMocker
does this for you.
Contribution
- Clone the repository
- Run
npm install
- Write your code
- Write tests to your changes
- Lint your code
- Open a pull request
- Wait for feedback and QA
- Merge and Glory!
Submitting issues
Add a new issue in the issues tab. Write as detailed as you can
Known issues
A list of known issues. More details can typically be found on github.
mockContext()
- Problem with
context.log()
andcontext.log.info()
. Having issues with properly mockingcontext.log
, as it can be both an functioncontext.log()
and an objectcontext.log = { info: ..., error: ..., warn: ..., verbose: ... }
. Have therefore only included support for the "object" version of it, as it provides the most features.