Move some helpers from test.js to utils.js

Depends on #643.
Changes regarding this function are from #643.