cenfun / monocart-coverage-reports

A code coverage tool to generate native V8 reports or Istanbul reports.
MIT License
31 stars 5 forks source link

[Bug] 分支不准确 #35

Closed fanksy closed 2 months ago

fanksy commented 3 months ago

版本:2.8.2

生成的codecov.json是准确的,但是istanbul格式的coverage-final.json误差却很大

coverage-final.json

  "1": {
        "loc": {
          "start": { "line": 42, "column": 26 },
          "end": { "line": 63, "column": 15 }
        },
        "type": "SwitchStatement",
        "locations": [
          {
            "start": { "line": 42, "column": 7 },
            "end": { "line": 58, "column": 96 }
          },
          {
            "start": { "line": 58, "column": 96 },
            "end": { "line": 62, "column": 5 }
          },
          {
            "start": { "line": 62, "column": 5 },
            "end": { "line": 63, "column": 15 }
          },
          {
            "start": { "line": 63, "column": 15 },
            "end": { "line": 63, "column": 15 }
          },
          {
            "start": { "line": 63, "column": 15 },
            "end": { "line": 63, "column": 15 }
          }
        ],
        "line": 42
      }

codecov.json

"src/app.js": {
      "4": 1,
      "5": 1,
      "6": 1,
      "7": 1,
      "8": 1,
      "9": 1,
      "10": 1,
      "11": 1,
      "12": 1,
      "13": 1,
      "14": 1,
      "15": 1,
      "16": 1,
      "17": 1,
      "18": 1,
      "20": 1,
      "22": 2,
      "23": 2,
      "24": 2,
      "33": 6,
      "34": 98,
      "35": 98,
      "36": 98,
      "37": 98,
      "38": 98,
      "39": 98,
      "40": 6,
      "42": 8,
      "43": 8,
      "44": 8,
      "45": 8,
      "47": 8,
      "48": 8,
      "49": 8,
      "51": 8,
      "52": 8,
      "54": 8,
      "55": 8,
      "56": 8,
      "57": 8,
      "58": 8,
      "59": 8,
      "60": 8,
      "61": 8,
      "62": 8,
      "63": 8,
      "64": 2,
      "66": 1
    }

42行这里是没有分支的,而istanbul报告却多了5个分支,codecov结果是准确的

源码:

import { getApp } from '@base/ddd';
import { currentLang } from '@base/i18n';
import App from 'components/App';
import config from 'config';
import flatten from 'lodash/flatten';
import intl from 'react-intl-universal';
import { init as sensorsInit } from 'tools/ext/sensors';
import 'utils/initLazyImgObserver';
import loadScript from 'utils/loadScript';
import 'utils/svgSeoOptimization';
import './global.less';
import './init.js';
import showError from './plugins/showError';
import routes from './router.config';
import { check } from './tools/xgray';

window.getApp = getApp;

export function Root(props) {
  return <App>{props.children}</App>;
}

/**
 * flatten routes
 *
 * @param   {[type]}  _routes  [_routes description]
 *
 * @return  {[type]}           [return description]
 */
function resolveRoutes(_routes) {
  return _routes.map((v) => {
    if (v.routes) {
      return [...resolveRoutes(v.routes)];
    }
    return v;
  });
}

export async function bootstrap() {
  sensorsInit();
  const { hosts } = config;
  const _HOST = hosts.WEB;
  // bootstrap tdk
  import('@kk/tdk').then(({ default: tdkManager }) => {
    tdkManager.setHost(_HOST);
  });

  const _routes = resolveRoutes(routes[0].routes);
  window._CRTS__ = flatten(_routes);

  const app = getApp();
  app.use({
    onError: showError,
  });
  await loadScript(`${__webpack_path__}/${currentLang}.js`);
  await intl.init({
    currentLocale: currentLang,
    locales: _DATA,
  });
  check();
}

export { routes };
cenfun commented 3 months ago

codecov.json是直接从V8原始数据读取的,仅包含行指标 coverage-final.json是从V8转换成Istanbul数据获取的,除了行指标,还有其他比如分支指标

多余的分支数据,问题可能出在编译代码和源代码的差异,得debug一下才知道,基于你提供的暂时看不出所以然 比如源码用到一个语法,a.?b.?c,编译后会转换成可能是3个条件分支的判断 如果你能提供一个demo仓库我可以帮你看看,或者你可以自己使用 logging: "debug"调试一下

fanksy commented 3 months ago

“a.?b.?c,编译后会转换成可能是3个条件分支的判断”,编译后确实有这种情况,但是我报告的绝对不是这种情况,42行仅仅是一个方法的定义,“export async function bootstrap()”,这个再怎么编译,也不可能编译出分支。问题的根源还是在于分支还原定位的准确性不够。可惜codecov.json数据不适合做增量统计。

fanksy commented 3 months ago

codecov.json是直接从V8原始数据读取的,仅包含行指标 coverage-final.json是从V8转换成Istanbul数据获取的,除了行指标,还有其他比如分支指标

多余的分支数据,问题可能出在编译代码和源代码的差异,得debug一下才知道,基于你提供的暂时看不出所以然 比如源码用到一个语法,a.?b.?c,编译后会转换成可能是3个条件分支的判断 如果你能提供一个demo仓库我可以帮你看看,或者你可以自己使用 logging: "debug"调试一下

由于涉及公司源码,所以不能提供,我看看能不能写个demo还原下

cenfun commented 3 months ago

所以才需要调试,才能查到根本原因 能否拷贝一个简单例子,能复现这个问题就好

之前也有过一些非常特殊的情况,但是可以通过写代码针对特殊情况做特殊修正的,但现在我得知道是个什么情况,所以需要能重现问题的例子

cenfun commented 2 months ago

先close了,等需要的时候再提新的