How to cover React jsx files in Istanbul?

后端 未结 4 1602
渐次进展
渐次进展 2021-02-13 03:14

I\'m trying to integrate my existing test processes to now include React, but am struggling on the code coverage part. I\'ve been able to get my unit tests working fine by foll

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-13 03:21

    A great tutorial can be found at https://istanbul.js.org/docs/tutorials/es2015/

    I just slightly modified it to include react. (I also used 'env' instead of 'es2015', but either should work.) Here are my configurations:

    npm i babel-cli babel-register babel-plugin-istanbul babel-preset-env babel-preset-react cross-env mocha nyc --save-dev
    

    .babelrc

    {
      "presets": ["env", "react"],
      "env": {
        "test": {
          "plugins": [
            "istanbul"
          ]
        }
      }
    }
    

    package.json

    "scripts": {
      "test": "cross-env NODE_ENV=test nyc mocha test/**/*.spec.js --compilers js:babel-register"
    }
    
    "nyc": {
      "require": [
        "babel-register"
      ],
      "reporter": [
        "lcov",
        "text"
      ],
      "sourceMap": false,
      "instrument": false
    }
    

提交回复
热议问题