not.custom_matcher error - Jasmine

别等时光非礼了梦想. 提交于 2019-12-06 13:26:09

To me it looks like your actual ends undefined or null, because you avoid to return -1.

With the line return classes.split(' ').indexOf(expected) !== -1; you only return on found values, so not found values get not returned.

One experimental thing you could try is to remove the condition !== -1, so also a not found gets returned. However, I don't know the side impacts of this; it could break just at another part or (as .not is looking for some kind of false-return), it still might not work and even your positive cases with expect().toHaveClass() could end successful when they should fail.

The safer option I see is to create another custom matcher toNotHaveClass and then return only on === -1, so only if a value isn't found. Basically this:

jasmine.addMatchers({
    toNotHaveClass: function() {
        return {
            compare: function(actual, expected) {
                return {
                    pass: actual.getAttribute('class').then(function(classes) {
                        return classes.split(' ').indexOf(expected) === -1;
                    })
                }
            }
        }
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!