最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - MouseEventConstructor is not a constructor - Stack Overflow

programmeradmin5浏览0评论

When I execute my tests locally they pass with no problems but when tests proceed on the server I get:

TypeError: MouseEventConstructor is not a constructor (evaluating 'new MouseEvent('mousedown',
EXEC : error : TypeError: MouseEventConstructor is not a constructor (evaluating 'new MouseEvent('mousedown', 
        {
            'which': 1,
            'view': window,
            'bubbles': true,
            'cancelable': true
        })')

for the code:

HTMLElement.prototype.mouseDownLeftButton = function () {
        var event = new MouseEvent('mousedown',
        {
            'which': 1,
            'view': window,
            'bubbles': true,
            'cancelable': true
        });
        this.dispatchEvent(event);
    };

which is totally fine. Is there any other way to create a new MouseEvent ?

When I execute my tests locally they pass with no problems but when tests proceed on the server I get:

TypeError: MouseEventConstructor is not a constructor (evaluating 'new MouseEvent('mousedown',
EXEC : error : TypeError: MouseEventConstructor is not a constructor (evaluating 'new MouseEvent('mousedown', 
        {
            'which': 1,
            'view': window,
            'bubbles': true,
            'cancelable': true
        })')

for the code:

HTMLElement.prototype.mouseDownLeftButton = function () {
        var event = new MouseEvent('mousedown',
        {
            'which': 1,
            'view': window,
            'bubbles': true,
            'cancelable': true
        });
        this.dispatchEvent(event);
    };

which is totally fine. Is there any other way to create a new MouseEvent ?

Share Improve this question edited Mar 21, 2017 at 21:28 Bergi 666k161 gold badges1k silver badges1.5k bronze badges asked Mar 21, 2017 at 14:15 YodaYoda 18.1k72 gold badges216 silver badges367 bronze badges 2
  • 1 "Locally", "on the server" - please be more specific about the used environment. Sounds like there simply is no DOM. – Bergi Commented Mar 21, 2017 at 14:17
  • @Bergi Means that when I run my qUnit tests locally everything is fine but when the server runs them as a part of Continous Integeration they fail because of this reason. I have no knowledge about configuration of the server itself. Is there an older version syntax than new MouseEvent available? About "no DOM" the RHS is failing not the LHS, so I think that dom is intact. Thanks. – Yoda Commented Mar 21, 2017 at 19:18
Add a ment  | 

2 Answers 2

Reset to default 5

There is a polyfill from MDN that will resolve the issue:

https://developer.mozilla/en-US/docs/Web/API/MouseEvent/MouseEvent#Polyfill

(function (window) {
    try {
        new MouseEvent('test');
        return false; // No need to polyfill
    } catch (e) {
        // Need to polyfill - fall through
    }

    // Polyfills DOM4 MouseEvent

    var MouseEvent = function (eventType, params) {
        params = params || { bubbles: false, cancelable: false };
        var mouseEvent = document.createEvent('MouseEvent');
        mouseEvent.initMouseEvent(eventType, params.bubbles, params.cancelable, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);

        return mouseEvent;
    };

    MouseEvent.prototype = Event.prototype;

    window.MouseEvent = MouseEvent;
})(window);

Most likely your local test infra uses real browser and on server it uses PhantomJS.

The latter still doesn't support new MouseEvent: https://github./ariya/phantomjs/issues/11289

I had to do the following trick to make tests pass:

    function createMouseEvent(typeArg: string): MouseEvent {
        let event = document.createEvent('MouseEvent');
        event.initMouseEvent(typeArg,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined,
            undefined);
        return event;
    }
发布评论

评论列表(0)

  1. 暂无评论