@atakama/capio

0.0.1 • Public • Published

Capio - async log, io and generic global state capture

When writing unit test for complex async code it's somtimes difficult to isolate compoenent output.

Node's async_hooks library provides enough information to allow globals to be swapped in and out.

Alternately, the deprecated domain module is used if needed.

Capio wraps both of these libraries, meaning you don't have to think about which version of node you're running.

Console log capture:

Consider this:

test1 = async ()=>{
    await thing1()
}

test2 = async ()=>{
    await thing2()
}

When run linearly, we can use the simple capture_console hook to capture output:

However, if we want to run thing1 and thing2 in parallel, the logs will get jumbled up.

Capio injects a 'captio' member to errors, which allows this to work:

run1 = async (test) => {
    try {
        await capio.captureLog(test)
    } except (e) {
        for (args of e.capio) {
            console.log(...args)
        }
    }
}

Promise.all(tests.map(t=>run1(t)))

I/O Spies

Capio can be used to 'spy' streams as well:

spied = await capio.captureIo(test, [socket, process.stdout], {spy: true})

console.log("socket", spied[0])
console.log("stdout", spied[1])

Low level tool for framework devs

This will call swapIn every time async execution contexts are run in the test, and will call swapOut evert time they are finished.

await capio.capture(test, swapIn, swapOut)

Readme

Keywords

Package Sidebar

Install

npm i @atakama/capio

Weekly Downloads

3

Version

0.0.1

License

ISC

Unpacked Size

8.23 kB

Total Files

5

Last publish

Collaborators

  • alexanderpinkerton
  • earonesty