gotwarlost / istanbul

Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests and browser tests. Built for scale.
Other
8.7k stars 785 forks source link

TypeError: metrics.isEmpty is not a function #868

Closed risingsunomi closed 6 years ago

risingsunomi commented 6 years ago

Problem

When running npm test using jest with jest configuration

{
 ...,
"scripts": {
    "test": "jest"
  },
"jest": {
    "collectCoverage": true
  },
"devDependencies": {
    "eslint": "^4.18.2",
    "eslint-config-google": "^0.9.1",
    "istanbul": "^0.4.5",
    "istanbul-api": "^1.3.1",
    "istanbul-lib-report": "^1.1.3",
    "jest": "^22.4.2"
  }
}

In the package.json and using npm test, I am receiving this error at the end of a test

> newsirc@1.0.0 test /Users/netghost/Projects/js/newsirc
> jest

 PASS  __tests__/RSS_IO_test.js
  class RSS_IO
    ✓ starts construction at initialization (3ms)
    ✓ has an array of rss_urls (1ms)

-----------|----------|----------|----------|----------|-------------------|
File       |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
-----------|----------|----------|----------|----------|-------------------|
All files  |      100 |      100 |      100 |      100 |                   |

        Failed to write coverage reports:
        ERROR: TypeError: metrics.isEmpty is not a function
        STACK: TypeError: metrics.isEmpty is not a function
    at tableRow (/Users/netghost/Projects/js/newsirc/node_modules/istanbul-reports/lib/text/index.js:133:27)
    at TextReport.onSummary (/Users/netghost/Projects/js/newsirc/node_modules/istanbul-reports/lib/text/index.js:188:15)
    at TextReport.onDetail (/Users/netghost/Projects/js/newsirc/node_modules/istanbul-reports/lib/text/index.js:193:17)
    at Visitor.(anonymous function) [as onDetail] (/Users/netghost/Projects/js/newsirc/node_modules/istanbul-api/node_modules/istanbul-lib-report/lib/tree.js:34:30)
    at ReportNode.Node.visit (/Users/netghost/Projects/js/newsirc/node_modules/istanbul-api/node_modules/istanbul-lib-report/lib/tree.js:123:17)
    at /Users/netghost/Projects/js/newsirc/node_modules/istanbul-api/node_modules/istanbul-lib-report/lib/tree.js:116:23
    at Array.forEach (<anonymous>)
    at visitChildren (/Users/netghost/Projects/js/newsirc/node_modules/istanbul-api/node_modules/istanbul-lib-report/lib/tree.js:115:32)
    at ReportNode.Node.visit (/Users/netghost/Projects/js/newsirc/node_modules/istanbul-api/node_modules/istanbul-lib-report/lib/tree.js:126:5)
    at Tree.visit (/Users/netghost/Projects/js/newsirc/node_modules/istanbul-api/node_modules/istanbul-lib-report/lib/tree.js:158:20)

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        1.039s
Ran all test suites.

With a test ./tests/RSS_IO_test.js

describe('class RSS_IO', () => {
    const rssobj = new RSS_IO(localConfig.RSSUrls);
    it('starts construction at initialization', () => {
        expect(typeof rssobj).toBe('object');
    });

    it('has an array of rss_urls', () => {
        expect(typeof rssobj.rss_urls.length).toBe('number');
    });
});

of a class RSS_IO

class RSS_IO {

    constructor(rss_urls) {
            this.rss_urls = rss_urls;
    }
}

module.exports = RSS_IO;

Research

Looking at the file ./node_modules/istanbul-reports/lib/text/index.js around Line 133

function tableRow(node, context, colorizer, maxNameCols, level, skipEmpty) {
    var name = nodeName(node),
        metrics = node.getCoverageSummary(),
        isEmpty = metrics.isEmpty();
    if (skipEmpty && isEmpty) { return ''; }
    ...;
}

There seems to be a metrics object calling isEmpty either incorrectly by name/reference or not defined.

Ask

Is there any reason to why isEmpty() of the metrics object not be defined? Missing library? Missing install library?

Thank you

ryan-lang commented 6 years ago

facebook/jest#5772

dazip commented 6 years ago

https://github.com/istanbuljs/istanbuljs/pull/140

risingsunomi commented 6 years ago

@dazip I don't know how or where to implement --skip-empty through the jest settings in package.json

risingsunomi commented 6 years ago

@ryan-lang I have made my package.json

"devDependencies": {
    "eslint": "^4.18.2",
    "eslint-config-google": "^0.9.1",
    "istanbul": "^0.4.5",
    "istanbul-api": "^1.3.1",
    "istanbul-lib-report": "^1.1.3",
    "istanbul-reports": "1.1.4",
    "jest": "^22.4.2"
  }

but no fix

ryan-lang commented 6 years ago

@risingsunomi My workaround was to pin the following versions, delete node_modules and reinstall fresh. Although, this is shaky because you may have another dependency pulling in the newer broken version.

"istanbul-api": "1.2.2",
"istanbul-reports": "1.1.4",
risingsunomi commented 6 years ago

@ryan-lang ok thank you - trying that now

risingsunomi commented 6 years ago

@ryan-lang that worked! Thank you very much. Apprectiate it.

Fix in package.json

Delete your local node_modules

Put in your package.json

"devDependencies": {
    "eslint": "^4.18.2",
    "eslint-config-google": "^0.9.1",
    "istanbul": "^0.4.5",
    "istanbul-api": "1.2.2",
    "istanbul-reports": "1.1.4",
    "jest": "^22.4.2"
  },

With

"istanbul-api": "1.2.2",
"istanbul-reports": "1.1.4"