javascript - Jest : TypeError: Cannot read property 'length' of undefined

标签 javascript unit-testing jestjs

enter image description here

为什么反对.anonymous

package.json:

package.json:
{
    "name": "SmartConverter",
    "version": "1.0.0",
    "main": "./src/js/main.js",
    "scripts": {
        "test": "jest --coverage",
        "build": "webpack --config ./scripts/*/webpack.config.js"
    },
    "repository": "https://github.com/raushankumarnitdgp/SmartConverter.git",
    "author": "raushankumarnitdgp <raushankumar.nitdgp@gmail.com>",
    "license": "MIT",
    "dependencies": {
        "babel-cli": "^6.24.1",
        "babel-core": "^6.25.0",
        "babel-jest": "^20.0.3",
        "babel-loader": "^7.1.1",
        "babel-preset-es2015": "^6.24.1",
        "eslint": "^4.4.1",
        "jest": "^20.0.4",
        "jest-cli": "^20.0.4",
        "regenerator-runtime": "^0.10.5",
        "webpack": "^3.5.2"
    }
}

最佳答案

您在错误 stracktrace 中看到的 anonymous 关键字只是告诉您错误发生在匿名函数中。匿名函数是没有名字的函数,通常用作回调,但并非总是如此。例如:

function main() {
  myLibrary.doSomething('foo', function() {
    console.log('I have finished')
  })
}

传递给 myLibrary.doSomething 的函数是一个匿名函数。您看到的错误似乎发生在 phone.js 文件的第 20 行。检查您在哪里使用 .length

关于javascript - Jest : TypeError: Cannot read property 'length' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45663988/

相关文章:

c++ - 如果最后一行不相等则追加

javascript - BeforeAll 与 BeforeEach。什么时候使用它们?

javascript - 在新键上动态运行 Object.defineProperty

javascript - AngularJS - 工厂变量不会在 Controller 中更新

javascript - 如何将 QUnit 测试集成到 Yii 中

typescript - 在 JestJS 中测试私有(private)方法

typescript - Jest 和 typescript : VS Code can't find names

javascript - 网络 worker 究竟是什么以及何时使用它们

javascript - 将数据库存储/备份到文件中,IndexeDDB、WebSQL 和 SQLite 的区别?

javascript - Typescript + Jasmine/Mocha,但没有全局类型?