getalog

console.log geta6

jest-cliでcollectCoverageが動かない時の直し方メモ

備忘録

  • babel*@5 (勘弁して
  • jest-cli@0.8.2

TL;DR

preprocessor.jsbabel.transformretainLinesオプションを渡して、キャッシュをクリアする

くわしいめも

jestの設定はこんな感じ

{
  "name": "test",
  "rootDir": "./src",
  "cacheDirectory": "<rootDir>/../tmp",
  "collectCoverage": true,
  "scriptPreprocessor": "<rootDir>/../preprocessor.js",
  "preprocessorIgnorePatterns": [
    "node_modules"
  ],
  "unmockedModulePathPatterns": [...]
}

最初に書いたpreprocessor.jsがこれ

var babel = require('babel-core');

module.exports = {
  process: function(src, filename) {
    if (!babel.canCompile(filename)) {
      /* ごにょごにょする */
    } else {
      return babel.transform(src, { filename: filename }).code;
    }
  }
};

出るエラーがこちら

Failed with unexpected error.
/path/to/project/node_modules/jest-cli/src/jest.js:230
        throw error;
        ^

TypeError: Cannot read property 'text' of undefined
    at /path/to/project/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:236:45
    at Array.forEach (native)
    at annotateFunctions (/path/to/project/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:219:26)
    at HtmlReport.Report.mix.writeDetailPage (/path/to/project/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:422:9)
    at /path/to/project/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:484:26
    at SyncFileWriter.extend.writeFile (/path/to/project/node_modules/jest-cli/node_modules/istanbul/lib/util/file-writer.js:57:9)
    at FileWriter.extend.writeFile (/path/to/project/node_modules/jest-cli/node_modules/istanbul/lib/util/file-writer.js:147:23)
    at /path/to/project/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:483:24
    at Array.forEach (native)
    at HtmlReport.Report.mix.writeFiles (/path/to/project/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:477:23)
npm ERR! Test failed.  See above for more details.

どうやらjestじゃなくてistanbulのエラーっぽい

このエラー、なんか見たことあると思ったらbabelでretainLinesをつけずにtranspileしたコードをistanbulにかけるとlinenoが取れなくなって出るやつでした

なので、preprocessorでretainLinesを渡すようにした、ついでにcoverageちゃんととれるようにignore lineつけるようにする

var babel = require('babel-core');

module.exports = {
  process: function(src, filename) {
    if (!babel.canCompile(filename)) {
      /* ごにょごにょする */
    } else {
      return babel.transform(src, { filename: filename, retainLines: true, auxiliaryCommentBefore: 'istanbul ignore next' }).code;
    }
  }
};

キャッシュディレクトリにpreprocess-cacheってのができてると思うので、削除する

で、動かすとなおる