在Jasmine中,array.includes不起作用(必须由其他函数替换)。为什么?

问题描述 投票:8回答:1

我的问题似乎很奇怪。我有一个带有一个新的,非常简单的函数的构造函数,它应该检查变量是否包含在数组中。它完美地工作(我在表单中使用此功能)。

但是......我不能在这个函数上写任何单元测试,因为Karma / Jasmine看不到数组的“包含”功能。

有人可以建议我做什么吗?这里的情况,有点简化:

//要测试的构造函数

    vm.isNameAlreadyUsed = function () {
    //debut logging:
        console.log ("vm.allNames ",vm.allNames);  // output: vm.allNames ['A', 'B', 'C']
        console.log ("and vm.nameToBeChecked is ",vm.nameToBeChecked);    //output: and vm.nameToBeChecked is 'A'

        return vm.allNames.includes(vm.nameToBeChecked);
        // The previous works as expected at runtime, but it causes the following exception in karma/jasmine:
        // TypeError: undefined is not a constructor (evaluating 'vm.allNames.includes(vm.nameToBeChecked)
    };

//测试(业力/茉莉花)

        theConstructor.allNames = ["A", "B", "C"];
        theConstructor.nameToBeChecked = "A";
        var result theConstructor.isNameAlreadyUsed();           //error!
        expect(result).toBeTruthy();

茉莉花有可能看不到“包含”吗?数组已填充,变量也... ...为什么应该是那里的任何构造函数?

TypeError: undefined is not a constructor (evaluating 'vm.allNames.includes(vm.nameToBeChecked)

谢谢


UPDATE

我注意到在茉莉花中,任何对“包含”的调用都会导致错误。这不取决于哪里。例如,它足以在jasmine文件中编写以下代码,以获得提及...构造函数的错误(?!?):

[1, 2, 3].includes(2);
// TypeError: undefined is not a constructor (evaluating '[1, 2, 3].includes(2)') in  ...
javascript arrays unit-testing karma-jasmine
1个回答
1
投票

我猜这很可能是由于以下两点之一:

  1. 您的节点版本可能<6.0.0,因为在[email protected]之前本地不支持Array.prototype.includes,或者
  2. karma config file中的浏览器设置为“IE”,因为Internet Explorer不支持Array.prototype.includes
© www.soinside.com 2019 - 2024. All rights reserved.