node/test/parallel/test-stream-writable-null.js
Robert Nagy 1f209129c7 stream: throw invalid argument errors
Logic errors that do not depend on stream
state should throw instead of invoke callback
and emit error.

PR-URL: https://github.com/nodejs/node/pull/31831
Refs: https://github.com/nodejs/node/pull/31818
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
2020-02-25 12:07:30 +01:00

48 lines
994 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const stream = require('stream');
class MyWritable extends stream.Writable {
constructor(options) {
super({ autoDestroy: false, ...options });
}
_write(chunk, encoding, callback) {
assert.notStrictEqual(chunk, null);
callback();
}
}
{
const m = new MyWritable({ objectMode: true });
m.on('error', common.mustNotCall());
assert.throws(() => {
m.write(null);
}, {
code: 'ERR_STREAM_NULL_VALUES'
});
}
{
const m = new MyWritable();
m.on('error', common.mustNotCall());
assert.throws(() => {
m.write(false);
}, {
code: 'ERR_INVALID_ARG_TYPE'
});
}
{ // Should not throw.
const m = new MyWritable({ objectMode: true });
m.write(false, assert.ifError);
}
{ // Should not throw.
const m = new MyWritable({ objectMode: true }).on('error', (e) => {
assert.ifError(e || new Error('should not get here'));
});
m.write(false, assert.ifError);
}