Skip to content

Instantly share code, notes, and snippets.

@zapthedingbat
Created September 26, 2018 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zapthedingbat/2cd66e7506c4670fc0b7e36042cd9f46 to your computer and use it in GitHub Desktop.
Save zapthedingbat/2cd66e7506c4670fc0b7e36042cd9f46 to your computer and use it in GitHub Desktop.
Example reproduction of wallabyjs missing the setTimeout[util.promisify.custom] property used by the native node util.promisify module.

Example reproduction of wallabyjs missing the setTimeout[util.promisify.custom] property

When running tests in wallaby the setTimeout[util.promisify.custom] property is missing, which causes util.promisify to fail.

Reproduction steps

Running this gist with the command line npm test will result in a passing test.

test
    √ should wait one second and then return the given value (1004ms)

  1 passing (1s)

When run with wallaby the test fails.

​​​​​1 failing test, 0 passing​​​​​
  
  ​​​​test should wait one second and then return the given value​​​​ ​​​[6 ms]​​​​
    ​​​​
    ​​"callback" argument must be a function​​
      at Context.it ​test.js:8:8​

Details

The native nodejs util.promisify method expects to find an alternative implementation for use with promises defined on the setTimeout[util.promisify.custom] property. This property is in tact when run with the command line, but is missing when wallaby runs the tests.

The correct behaviour can be restored by defining an equivalent implementation on setTimeout[util.promisify.custom] in the wallaby setup method.

References

{
"name": "wallaby-example-promisify-settimeout",
"dependencies": {
"mocha": "^5.2.0"
},
"scripts": {
"test": "mocha"
}
}
const util = require('util');
// Promisify setTimeout so it can be awaited
const promisifiedSetTimeout = util.promisify(setTimeout);
describe('test', () => {
it('should wait one second and then return the given value', async () => {
await promisifiedSetTimeout(1000, 'hello');
})
})
module.exports = () => {
return {
files: [],
tests: [ 'test.js' ],
env: {
type: 'node',
runner: 'node'
},
testFramework: 'mocha',
debug: true,
setup: function () {
// Add the custom promisify implementation back to setTimeout to make wallaby work
/*
const util = require('util');
if (typeof util.promisify.custom !== 'undefined' && typeof setTimeout[util.promisify.custom] === 'undefined') {
setTimeout[util.promisify.custom] = (after, value) => new Promise(resolve => setTimeout(() => resolve(value), after));
}
*/
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment