tntim96 / JSCover

JSCover is a JavaScript Code Coverage Tool that measures line, branch and function coverage
GNU General Public License v2.0
399 stars 84 forks source link

`jscoverage.json` not getting generated using -gf option #322

Closed shreyxskar closed 1 year ago

shreyxskar commented 1 year ago

Hi,

I tried running the example https://github.com/tntim96/JSCover/blob/master/examples/phantomJS-file-system/phantom-fs-jasmine.bat

This works perfectly fine and generates the jscoverage.json at the mentioned location

However, if I remove the below line (line 6) from same example phantomjs src\test\javascript\lib\PhantomJS\run-jscover-jasmine2.js target/phantom-jscover-jasmine-fs/test/javascript/spec/suite.html target\phantom-jscover-jasmine-fs and rerun the same batch file then I'm not getting any jscoverage.json

Please help here.

tntim96 commented 1 year ago

Line 6 is needed. PhantomJS is now obsolete, but it is the javascript engine (similar to node or a browser) that runs the example.

shreyxskar commented 1 year ago

Query 1 - Any other way to generate this JSON? Is it the same function we need to call - jscoverage_serializeCoverageToJSON() for getting it? Query 2 - Even if we don't get this JSON (worst case anyhow) is there any way to know that coverage will actually be captured by it?

tntim96 commented 1 year ago

Is it the same function we need to call - jscoverage_serializeCoverageToJSON() for getting it?

Correct

Even if we don't get this JSON (worst case anyhow) is there any way to know that coverage will actually be captured by it?

The example shows how it does work. It's up to you to see if you can get it work for your scenario.

shreyxskar commented 1 year ago

I tried running this example - https://github.com/tntim96/JSCover/tree/master/examples/localStorage-file-system with Selenium IDE but getting this error while generating JSON: image

I tried changing storeEval with execute script but getting this error: image

EDIT1: I ran jscover-filesystem.bat with --no-branch option as well

tntim96 commented 1 year ago

Works fine for me in FireFox. What browser were you using?

shreyxskar commented 1 year ago

I'm using Chrome. No problem with that, I was able to fetch data from jscoverage_serializeCoverageToJSON() using BAT files. I added another line after line 6 in this example- https://github.com/tntim96/JSCover/blob/master/examples/phantomJS-file-system/phantom-fs-jasmine.bat It looked like: python src\test\python\file1.py target\phantom-jscover-jasmine-fs The contents of this file1.py was nothing but calling jscoverage_serializeCoverageToJSON() (in javascript) from a python script (using js2py module in python - https://github.com/PiotrDabkowski/Js2Py)

While running the script (same BAT file with python script call) I'm getting this error:

python src\test\python\file1.py target\phantom-jscover-jasmine-fs Traceback (most recent call last): File "path\jsCover\src\test\python\file1.py", line 14, in f.write(res()) File "path\jsCover\src\test\python\lib\site-packages\js2py\base.py", line 1203, in call return to_python(parent._obj.callprop(meth, args)) File "path\jsCover\src\test\python\lib\site-packages\js2py\base.py", line 996, in callprop return cand.call(self, args) File "path\jsCover\src\test\python\lib\site-packages\js2py\base.py", line 1464, in call return Js(self.code(args)) File "", line 7, in PyJsHoisted_jscoverageserializeCoverageToJSON File "path\jsCover\src\test\python\lib\site-packages\js2py\base.py", line 1153, in get return self.prototype.get(prop, throw) File "path\jsCover\src\test\python\lib\site-packages\js2py\base.py", line 1158, in get raise MakeError('ReferenceError', '%s is not defined' % prop) js2py.internals.simplex.JsException: ReferenceError: _$jscoverage is not defined

I believe we can still use the javascript variable _$jscoverage that stores the coverage data, right? But here it is giving reference error.

NOTE: Contents of file1.py:

import js2py import os import sys

if len(sys.argv) != 2: print("Wrong syntax") sys.exit(1)

filepath = sys.argv[1]

js_code = """<complete function - https://github.com/tntim96/JSCover/blob/8b9734a715381f7ff291a40b22997e47fc59dbe8/src/main/resources/jscoverage-common.js#L45>""" res = js2py.eval_js(js_code) with open(os.path.join(filepath, "python.json"), "w") as f: f.write(res()) print("Written")

tntim96 commented 1 year ago

I'm not sure of your setup so not sure I can help.