node/test/parallel/test-fs-read-stream-err.js
cjihrig 7dd82dd1c3 test: add common.mustNotCall()
This commit adds a mustNotCall() helper for testing. This provides
an alternative to using common.fail() as a callback, or creating
a callback function for the sole purpose of calling common.fail().

PR-URL: https://github.com/nodejs/node/pull/11152
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
2017-02-06 14:07:55 -05:00

43 lines
982 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const stream = fs.createReadStream(__filename, {
bufferSize: 64
});
const err = new Error('BAM');
stream.on('error', common.mustCall((err_) => {
process.nextTick(common.mustCall(() => {
assert.strictEqual(stream.fd, null);
assert.strictEqual(err_, err);
}));
}));
fs.close = common.mustCall((fd_, cb) => {
assert.strictEqual(fd_, stream.fd);
process.nextTick(cb);
});
const read = fs.read;
fs.read = function() {
// first time is ok.
read.apply(fs, arguments);
// then it breaks
fs.read = common.mustCall(function() {
const cb = arguments[arguments.length - 1];
process.nextTick(() => {
cb(err);
});
// and should not be called again!
fs.read = () => {
throw new Error('BOOM!');
};
});
};
stream.on('data', (buf) => {
stream.on('data', common.mustNotCall("no more 'data' events should follow"));
});