node/test/parallel/test-fs-make-callback.js
Sakthipriyan Vairamani (thefourtheye) 4cb5f3daa3
fs: throw on invalid callbacks for async functions
If an asynchronous function is passed no callback function, there is no
way to return the result. This patch throws an error if the callback
passed is not valid or none passed at all.

PR-URL: https://github.com/nodejs/node/pull/12562

Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2017-05-10 00:04:01 +05:30

26 lines
675 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const cbTypeError = common.expectsError({code: 'ERR_INVALID_CALLBACK'});
const callbackThrowValues = [null, true, false, 0, 1, 'foo', /foo/, [], {}];
const { sep } = require('path');
common.refreshTmpDir();
function testMakeCallback(cb) {
return function() {
// fs.mkdtemp() calls makeCallback() on its third argument
fs.mkdtemp(`${common.tmpDir}${sep}`, {}, cb);
};
}
function invalidCallbackThrowsTests() {
callbackThrowValues.forEach((value) => {
assert.throws(testMakeCallback(value), cbTypeError);
});
}
invalidCallbackThrowsTests();