仅在 运行 PhantomJS 时获取失败的测试(TypeError)

Getting failed tests (TypeError) only when running PhantomJS

tl;dr 如何消除 PhantomJS 中的这个错误?

回购

参考回购:https://github.com/Sawtaytoes/Tabletop-Helper/tree/d123927b8f65fc97234fd2543bf9103ac0db58ab

运行 yarn test.

问题

当 运行 来自 Chrome 与 PhantomJS 的相同测试时,我看到出现与 Webpack 包相关的错误,或者我是这么认为的。

错误

TypeError: undefined is not a constructor (evaluating '__WEBPACK_IMPORTED_MODULE_3_content_smash_up_decks__["a" /* sets */].find(function (set) { return title === set.title; })')

代码

sets
.find(set => title === set.title)
.decks
.reduce((combined, { title }) => ({ ...combined, [title]: setState }), {})

混乱

特别中断的行是 sets.find()。如果我忽略它并使其成为 sets[0],它就可以工作。在注销 sets.find() 的值时,它总是返回一个匹配的数组,因为编写测试是为了传递有效的集合标题。

另外两个有类似错误的测试也引用了 sets.find()。 PhantomJS 不喜欢的这个功能是什么?

缩小范围后,我能够让它只显示

TypeError: undefined is not a constructor (evaluating 'Array.prototype.find')

PhantomJS 2.1 及更早版本不支持 Array.prototype.find。解决方案是通过 npm 安装 PhantomJS 插件并在 Karma 配置中设置它:

ptim/phantomjs-polyfill-find

安装后,需要修改 Karma 配置。 files 属性 的第一个数组参数可以这样修改:

files: [
    `node_modules/phantomjs-polyfill-find/find-polyfill.js`,
    ...
],

注意:必须在路径.

中引用node_modules/