helpers for mocha, when using bluebird promises
These are a few small helpers for mocha, that simplify your test boilerplate if you use bluebird.
Each function is intended to be a mixin for a unit test, so for example, if you have a test like this:
it"adds numbers"2 + 2shouldeql4;;
you can add each helper as a wrapper around the test function, like this:
it"adds numbers"Promiseresolve2 + 2thensumshouldeql4;;;
Wrap a test function that returns a future:
() => Promisecode to execute as a test
done hooks are attached to the future so that the test doesn't complete until the future is resolved.
it"waits 100 ms" futurereturn Qdelay100;;
Wrap a test function so that it runs inside a temporary folder:
(folder) => Promisecode to execute while the folder exists
The folder's name is passed as the first parameter to the function, and it's expected to return a Promise. When the Promise is resolved, the temporary folder is deleted (along with any contents).
This function requires
future also, since the cleanup is attached to the result future.
it"creates a file" futurewithTempFolderfswriteFileSyncfolder + "/new-file" "hello!";;
Execute a program as a future:
command: passed to
options: passed to
The parameters are passed to
child_process.exec as-is and a future is returned. If the exec is successful, the future is resolved with an object with these fields:
If the exec fails, the future is rejected, and the error object will have those three fields added to it.
it"runs echo" futurereturn exec"echo hello"thenresultstdoutshouldmatch/hello/;;;
Try running assertions a few times, until they pass (or run out of time):
timeout: milliseconds to wait before giving up (default: 1000)
frequency: milliseconds to wait between attempts (default: 50)
function: code to execute on each attempt
To assert that a background process will have some effect in a non-deterministic (but relatively short) time, you can group your assertions in an
eventually block. If an exception is thrown the first time -- for example, by a failing assertion -- it will delay for a small period of time, then try again, repeating until the code executes without exception or the time runs out.