0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-event-emitter-prepend.js
Ruben Bridgewater c6b6c92185
lib: always show ERR_INVALID_ARG_TYPE received part
This makes a effort to make sure all of these errors will actually
also show the received input.
On top of that it refactors a few tests for better maintainability.
It will also change the returned type to always be a simple typeof
instead of special handling null.

PR-URL: https://github.com/nodejs/node/pull/19445
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
2018-03-25 01:45:37 +01:00

53 lines
1.3 KiB
JavaScript

'use strict';
const common = require('../common');
const EventEmitter = require('events');
const assert = require('assert');
const myEE = new EventEmitter();
let m = 0;
// This one comes last.
myEE.on('foo', common.mustCall(() => assert.strictEqual(m, 2)));
// This one comes second.
myEE.prependListener('foo', common.mustCall(() => assert.strictEqual(m++, 1)));
// This one comes first.
myEE.prependOnceListener('foo',
common.mustCall(() => assert.strictEqual(m++, 0)));
myEE.emit('foo');
// Verify that the listener must be a function
common.expectsError(() => {
const ee = new EventEmitter();
ee.prependOnceListener('foo', null);
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "listener" argument must be of type Function. ' +
'Received type object'
});
// Test fallback if prependListener is undefined.
const stream = require('stream');
const util = require('util');
delete EventEmitter.prototype.prependListener;
function Writable() {
this.writable = true;
stream.Stream.call(this);
}
util.inherits(Writable, stream.Stream);
function Readable() {
this.readable = true;
stream.Stream.call(this);
}
util.inherits(Readable, stream.Stream);
const w = new Writable();
const r = new Readable();
r.pipe(w);