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

Viewed 10332

enter image description here

Why it object.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"
    }
}

2 Answers

I think it might be some props haven't been set yet, for instance, if you have a property called expenses inside ComponentX, but in jest file, you didn't pre-set this expenses property,

eg: <ComponentX />, forgot to pass props into this ComponentX, and Jest couldn't find where the expenses property is, expenses would be undefined, and then undefined.length definitely cause that undefined error.

How can we fix this expenses.length undefined issue?

we can do this: <ComponentX expenses={mockExpenses} />

Now, expenses.length should not be undefined, because of expenses have some data.

Related