I bumped into the following error running jest with coverage enabled on a react-native project. Call stack and error is:
Failed with unexpected error.
/Users/graeme/src/react-native-stylish/node_modules/jest-cli/src/jest.js:190
throw error;
^
TypeError: Cannot read property 'text' of undefined
at /Users/graeme/src/react-native-stylish/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:283:53
at Array.forEach (native)
at annotateBranches (/Users/graeme/src/react-native-stylish/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:250:30)
at HtmlReport.Report.mix.writeDetailPage (/Users/graeme/src/react-native-stylish/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:421:9)
at /Users/graeme/src/react-native-stylish/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:484:26
at SyncFileWriter.extend.writeFile (/Users/graeme/src/react-native-stylish/node_modules/jest-cli/node_modules/istanbul/lib/util/file-writer.js:57:9)
at FileWriter.extend.writeFile (/Users/graeme/src/react-native-stylish/node_modules/jest-cli/node_modules/istanbul/lib/util/file-writer.js:147:23)
at /Users/graeme/src/react-native-stylish/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:483:24
at Array.forEach (native)
at HtmlReport.Report.mix.writeFiles (/Users/graeme/src/react-native-stylish/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:477:23)
at /Users/graeme/src/react-native-stylish/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:479:22
at Array.forEach (native)
at HtmlReport.Report.mix.writeFiles (/Users/graeme/src/react-native-stylish/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:477:23)
at HtmlReport.Report.mix.writeReport (/Users/graeme/src/react-native-stylish/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:561:14)
at LcovReport.Report.mix.writeReport (/Users/graeme/src/react-native-stylish/node_modules/jest-cli/node_modules/istanbul/lib/report/lcov.js:55:19)
at /Users/graeme/src/react-native-stylish/node_modules/jest-cli/node_modules/istanbul/lib/reporter.js:93:20
at Array.forEach (native)
at Object.Reporter.write (/Users/graeme/src/react-native-stylish/node_modules/jest-cli/node_modules/istanbul/lib/reporter.js:87:30)
at DefaultTestReporter.IstanbulTestReporter.onRunComplete (/Users/graeme/src/react-native-stylish/node_modules/jest-cli/src/IstanbulTestReporter.js:33:14)
at /Users/graeme/src/react-native-stylish/node_modules/jest-cli/src/TestRunner.js:457:40
at tryCatcher (/Users/graeme/src/react-native-stylish/node_modules/jest-cli/node_modules/bluebird/js/main/util.js:26:23)
at Promise._settlePromiseFromHandler (/Users/graeme/src/react-native-stylish/node_modules/jest-cli/node_modules/bluebird/js/main/promise.js:503:31)
at Promise._settlePromiseAt (/Users/graeme/src/react-native-stylish/node_modules/jest-cli/node_modules/bluebird/js/main/promise.js:577:18)
at Promise._settlePromises (/Users/graeme/src/react-native-stylish/node_modules/jest-cli/node_modules/bluebird/js/main/promise.js:693:14)
at Async._drainQueue (/Users/graeme/src/react-native-stylish/node_modules/jest-cli/node_modules/bluebird/js/main/async.js:123:16)
at Async._drainQueues (/Users/graeme/src/react-native-stylish/node_modules/jest-cli/node_modules/bluebird/js/main/async.js:133:10)
at Immediate.Async.drainQueues [as _onImmediate] (/Users/graeme/src/react-native-stylish/node_modules/jest-cli/node_modules/bluebird/js/main/async.js:15:14)
at processImmediate [as _immediateCallback] (timers.js:371:17)
The problem occurs at https://github.com/gotwarlost/istanbul/blob/master/lib/report/html.js#L283 where it appears that structuredText[startLine] can be null.

Adding check to skip lines where structuredText[startLine] was undefined produced what looks like well-formed html output.
+1
+1
+1
I am getting same error, but at almost every place where structuredText is accessed.
@graemej Did you have to patch every instance of text = structuredText[startLine].text; to make the reporting work?
See related issue: https://github.com/karma-runner/karma-coverage/issues/157
This error appeared in the transpiled code. Guys, are you using babel or something? Try to use fixed version from branch.
npm install --save-dev gotwarlost/istanbul#source-map
It works fine for me
+1
The root cause is that the source does not match what was instrumented. All patches to structured line access are indicative of a deeper problem. I will focus on solving that problem instead.
Is there another issue to track the fix for this issue?
Does anyone else still have the problem even when using the source-map branch?
@seb0zz Yes. Bumping this.
+1. Problem on jest-cli v0.7.1 as well.
i've had the same issue with isparta cover ava -- test/**/*.test.js and i was able to resolve it with babel-node node_modules/.bin/isparta cover ava -- test/**/*.test.js (with the source-map branch). i guess it's pure :sparkles: magic :sparkles: ...
I still have this problem, :(
On Jest CLI 0.8.0 with npm install --save-dev gotwarlost/istanbul#source-map and still have problem
Failed with unexpected error.
/Users/barton/projects/MFV/snowflake/node_modules/jest-cli/src/jest.js:232
throw error;
^
TypeError: Cannot read property 'text' of undefined
at /Users/barton/projects/MFV/snowflake/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:283:53
@gotwarlost I am also having similar issue
Failed with unexpected error.
/home/shameer/Projects/test/fr-webapp-qb/node_modules/jest-cli/src/jest.js:218
throw error;
^
TypeError: Cannot read property 'text' of undefined
at /home/shameer/Projects/test/fr-webapp-qb/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:236:45
at Array.forEach (native)
at annotateFunctions (/home/shameer/Projects/test/fr-webapp-qb/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:219:26)
at HtmlReport.Report.mix.writeDetailPage (/home/shameer/Projects/test/fr-webapp-qb/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:422:9)
at /home/shameer/Projects/test/fr-webapp-qb/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:484:26
at SyncFileWriter.extend.writeFile (/home/shameer/Projects/test/fr-webapp-qb/node_modules/jest-cli/node_modules/istanbul/lib/util/file-writer.js:57:9)
at FileWriter.extend.writeFile (/home/shameer/Projects/test/fr-webapp-qb/node_modules/jest-cli/node_modules/istanbul/lib/util/file-writer.js:147:23)
at /home/shameer/Projects/test/fr-webapp-qb/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:483:24
at Array.forEach (native)
at HtmlReport.Report.mix.writeFiles (/home/shameer/Projects/test/fr-webapp-qb/node_modules/jest-cli/node_modules/istanbul/lib/report/html.js:477:23)
npm ERR! Test failed. See above for more details.
Using Jest CLI v0.7.1 Istanbul v0.4.1
Tried the istanbul#source-map branch but still have this problem
If we test the below code by removing the conditional operator/'if' condition the test will not show the above error
import React, { PropTypes, Component } from 'react';
class ProductImage extends Component {
static propTypes = {
imageSrc: PropTypes.string.isRequired,
viewType: PropTypes.string.isRequired,
}
render() {
//if we remove the below line test will not show any such error!
const viewType = (this.props.viewType === 'large') ? 'TileLarge' : 'TileSmall';
return (
<div className={this.viewType}>
<img src={this.props.imageSrc} className="TileImage"/>
</div>
);
}
}
export default ProductImage;
hey so I'm getting this issue using karma-browserify, and karma-ng-html2js
I can get the issue to dissapear if I remove or comment out
require("./sbFlyout.html");
require("./sbDropdown.html");
from my flyout directive.
I'm gonna guess this could be causing the source to not match what istanbul is looking at, the weird part is I have several other html partial requires in my project and this just popped up now
@gotwarlost
I was able to fix my version of this issue by making the following change my my karma.conf.js file
Before:
// browserify configuration
browserify: {
files: [
'test/**/*.spec.js'
],
debug: true,
transform: [
['browserify-ng-html2js', {
module: 'templates',
extension: 'html'
}],
'browserify-istanbul',
'brfs',
'browserify-shim'
]
}
After (fixed):
// browserify configuration
browserify: {
files: [
'test/**/*.spec.js'
],
debug: true,
transform: [
['browserify-ng-html2js', {
module: 'templates',
extension: 'html'
}],
['browserify-istanbul', {
instrumenterConfig: {
embedSource: true
}
}],
'brfs',
'browserify-shim'
]
}
My coverage now generates just fine, and unlike the if-check or log level work around I can actually dive into the coverage report and get the detailed annotations the other work arounds were missing. I don't think this is a complete fix but I hope it helps some people until a more concrete solutions can be discovered and implemented.
@joeMalmsten This fix was just the ticket. Solved my error and got proper HTML output for all script files.
This seems to be related to miscounting lines by 1 in report/html.js... At least was in my case.
@graemej it is not miscounting by constant, it depends on your actual sources. It happens because your code is being transpilled and lines in output can mismatch by lines with source code.
To solve it, there is a branch source-map that uses information from source maps to build correct report, but it haven't merged yet.
+1
+1 Still getting this error.
Most helpful comment
@gotwarlost
I was able to fix my version of this issue by making the following change my my karma.conf.js file
Before:
After (fixed):
My coverage now generates just fine, and unlike the if-check or log level work around I can actually dive into the coverage report and get the detailed annotations the other work arounds were missing. I don't think this is a complete fix but I hope it helps some people until a more concrete solutions can be discovered and implemented.