Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

fakeAsync in node throws TypeError: Cannot read property 'length' of undefined #760

@tbosch

Description

@tbosch

Node 6.9.5.

Stack:

TypeError: Cannot read property 'length' of undefined
    at onwriteDrain (_stream_writable.js:396:12)
    at afterWrite (_stream_writable.js:386:5)
    at ZoneDelegate.invokeTask (/Users/tbosch/projects/angular/node_modules/zone.js/dist/zone-node.js:399:31)
    at ProxyZoneSpec.onInvokeTask (/Users/tbosch/projects/angular/node_modules/zone.js/dist/proxy.js:103:39)
    at ZoneDelegate.invokeTask (/Users/tbosch/projects/angular/node_modules/zone.js/dist/zone-node.js:398:36)
    at Zone.runTask (/Users/tbosch/projects/angular/node_modules/zone.js/dist/zone-node.js:165:47)
    at ZoneTask.ZoneTask.cancelFn.invoke (/Users/tbosch/projects/angular/node_modules/zone.js/dist/zone-node.js:466:38)
    at FakeAsyncTestZoneSpec.flushMicrotasks (/Users/tbosch/projects/angular/node_modules/zone.js/dist/fake-async-test.js:204:17)
    at flushMicrotasks (../../../../../../packages/core/testing/src/fake_async.ts:135:27)

This happens after the test is done and fakeAsync flushes the microtasks.
The code in _stream_writable.js is from node, and contains the following:

process.nextTick(afterWrite, stream, state, finished, cb);
...
function afterWrite(stream, state, finished, cb) { ...}

The error above occurs because the call to process.nextTick does not forward the extra arguments to the afterWrite callback when it happens.

Workaround / patch in zone-node.js:

function patchMicroTask(obj, funcName, metaCreator) {
    var setNative = null;
    function scheduleTask(task) {
        var data = task.data;
        data.args[data.callbackIndex] = function () {
            task.invoke.apply(this, arguments);
        };
        // old: setNative.apply(data.target, data.args);
        // new:
        setNative.apply(data.target, [].slice.call(data.args, 0, data.callbackIndex+1));
        return task;
    }
    setNative = patchMethod(obj, funcName, function (delegate) { return function (self, args) {
        var meta = metaCreator(self, args);
        if (meta.callbackIndex >= 0 && typeof args[meta.callbackIndex] === 'function') {
            // new
            var cbArgs = [].slice.call(args, meta.callbackIndex+1);
            cbArgs.unshift(null);
            var task = Zone.current.scheduleMicroTask(meta.name, Function.bind.apply(args[meta.callbackIndex], cbArgs), meta, scheduleTask);
            // old: var task = Zone.current.scheduleMicroTask(meta.name, args[meta.callbackIndex], meta, scheduleTask);
            return task;
        }
        else {
            // cause an error by calling it directly.
            return delegate.apply(self, args);
        }
    }; });
}

I.e. bind the arguments early to the callback, so that fakeAsync does not have to pass them anymore.

Note that this is probably a design bug as:

  • the last argument to Zone.current.scheduleMicroTask contains method specific knowledge (in this case that the remaining argument have to be passed to the callback)
  • fakeAsync is never calling this callback.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions