Machina also supports test code, which can be run parallel to machine and can test behaviour of each parts.
You have access to same storage, and you can also use fetch, in case of fetch http://localhost
will request
machine itself. All test cases have to be wrapped by it function like in this example:
Always wrap your cases to it
function!
assert(condition: boolean) it(caseName: string, async () => void)
In HTTP callbacks, you receive context through the req.context
parameter:
async function uploadFile(req: ProcessedRequest) { await fetch(req.context, 'https://api.example.com/upload', { ... }); }
In test code, you use the machineContext
variable directly:
it('Test upload', async () => { const result = await fetch(machineContext, 'http://localhost/uploadFile', { ... }); });
The machineContext
is automatically available in test environment and provides the same functionality as req.context
in HTTP callbacks.
it('Setting', async () => { const result = await fetch(machineContext, 'http://localhost/testRequest', { method: 'post', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ text: 'Hello world' }) }) assert(result.status === 200) })
© 2025 Routzie Routzie.com