Testing Locally
Testing is among the most important tasks in the cycle of software development. EXM provides the necessary SDK functions to facilitate testing in a way that can be readable & efficient.
No testing your EXM applications locally can lead to:
- Exceeding the maximum of deployments allowed
- Slowness in development by needing to wait for eventual consistency to take place
- Increase of unnecessary account usage
In order to test applications locally inside EXM's environment, you will need to install EXM's Javascript SDK. For more information, click here.
After having installed the Javascript SDK, we'll take a look at the following examples.
user-registry.tests.js
user-registry.js
// import { ... } from '@execution-machine/sdk'
const { TestFunction, createWrite, FunctionType } = require("@execution-machine/sdk");
const { readFileSync } = require("fs");
const testAttempt = await TestFunction({
functionSource: readFileSync("users-registry.js"),
functionType: FunctionType.JAVASCRIPT,
functionInitState: {
users: []
},
writes: [createWrite({ username: "Andres" })]
});
assert(testAttempt.state.users, [{ username: "Andres" }]);
assert(testAttempt.result, 1);
- Necessary functions are imported from
@execution-machine/sdk
andfs
TestFunction
creates an execution context simulating in real time what EXM doescreateWrite
creates an object that satisfies the internal write operation interface
/**
*
* @param state is the current state your application holds
* @param action is an object containing { input, caller } . Most of the times you will only use `action.input` which contains the input passed as a write operation
* @returns {Promise<{ users: Array<{ username: string}> }>}
*/
export async function handle(state, action) {
const { username } = action.input;
state.users.push({ username });
return { state, result: state.users.length };
}
- The function above receives a property
username
in an object passed as input - the
username
property is added tostate.users
(an array) state.users
is initialized during deployment with initial state{"users": []
}
In the example above, we can see how
TestFunction
is called. - When this function is called, the
functionSource
buffer is provided throughreadFileSync
which is reading the application's source ode (user-registry.js) functionType
is explicitly specified to be a Javascript applicationfunctionInitState
contains the initial state of our simulated deployment, in this case, an empty array calledusers
writes
is an array with the instructions to be executed following the internal write operation interface.createWrite
facilitates abstract the use of such interface into a method that receives aninput
andtags
(optionally). The input passed increateWrite
is equivalent toaction.input
in your function.