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

How to begin with file system instrumentation mode #245

Closed hiteshsardana99 closed 7 years ago

hiteshsardana99 commented 7 years ago

Hello Everyone . I am a beginner in JSCover . I just want to know , how to begin with file system mode .

what i have done till now - 1) Downloaded JSCover and paste my all the files and test cases in the doc/example/ folder . 2) Create server by calling example/server/jscover-server.bat or by cmd 3) By Opening perticular link on browser (i.e. localhost:8080/ ) , it showed to me code coverage of my files and also generate the report in the target folder .

what i have to do -

1) I have integrate JSCover with my project , that will automatically test my js file by taking test cases without creating server , just show me the result or report.

what i want to know -

1) I think for achieving my goal , i have to begin on file system automation .
2) please help me from where i should start ? 3) do i need to use selenium , phantomJs , htmlunit etc ? 4) I want to modify JScover according to my own requirements , please guide me . 5) what is proxy mode , is it useful for me ? thanks in advance

tntim96 commented 7 years ago

To learn about JSCover, you can review the examples and read the manual - both of these are also available in the ZIP download.

You may also be interested in the JSCover maven plugin if your tests are written in Jasmine, QUnit or Mocha.

You know your project best, so you can probably make the best decision once you're familiar with the tools.

hiteshsardana99 commented 7 years ago

Sir , i have installed the maven and downloaded ur https://github.com/tntim96/JSCover-maven-plugin project . i want to know , how to run this project using maven , so that i can understand this example .

tntim96 commented 7 years ago

If you needed to install maven, it may not be the tool for you. What type of project is it (Java, Node, Rails)? What is your test JS file (Jasmine, QUnit, Mocha, other)? You haven't provided enough information for anyone to help.

You are best off trying a number of samples and seeing which resembles your project most closely and using a similar technique.

hiteshsardana99 commented 7 years ago

hey , i need your suggestion about which mode will be beneficial for me to use JSCover in my project .

Currently what we are doing -

we have developed our own environment in which we are using Qunit to test our test cases . we have created our own browser because in other browser there is security issues it does not allows js files to read and write . We have thousands of test cases and we have customized the test cases and usage of qunit , we are using it our own way .

But now we want to use jscover for code coverage

After that , i have used ur JSCover and run our files in server mode but again due to compatibility issue my test cases are getting failed and whenever i try to test my all test cases , the browser get crashed . Because JSCover increase the file size after instrumenting .

what we want --

when we are using qunit for testing test cases at the same time , JSCover will start and at the end i will get the report but without using server mode .

I think by using file system mode , our browser will not crashed .

I also want to know what is the difference between file system mode and local storage file system?

If i am right than please suggest how to use JSCover in better way .

hiteshsardana99 commented 7 years ago

One more thing i want to ask

I have run JSCover using file system mode and instrumented code is stored at target folder . Now i want to write my own script that will generate the report or how can i use your jscoverage_serializeCoverageToJSON() function . I mean do i need to call the instrumented files one by one than pass from this function .

tntim96 commented 7 years ago

we are using Qunit...how can i use your jscoverage_serializeCoverageToJSON() function...we have created our own browser

If your browser can write (like PhantomJS), you might be able to do something like:

                var json = page.evaluate(function(){
                    return jscoverage_serializeCoverageToJSON();
                });
                try {
                    fs.write(system.args[2] + '/jscoverage.json', json, 'w');
                } catch(e) {
                    console.log(e);
                }

as done in run-jscover-qunit.js.

For an example that uses this technique, see https://github.com/tntim96/JSCover/tree/master/examples/phantomJS-file-system

hiteshsardana99 commented 7 years ago

hey , thanks for your help . I have generated json data .

I want to know one thing , how unit testing is done in file system mode . In server mode , i call my Qunit.html file and Qunit will help in unit testing and JSCover keeps track which lines or functions are tested and make record of that . But in file system mode , we just specify only source or target folder . It automatically generate the instrumental code . My question is - when JSCover is coverting my code into instrumental code using filesystem at the same time unit testing is done by the JSCover or not ?.

I have some test cases that need to be test on browser . Still in that case , can i use filesystem mode?

tntim96 commented 7 years ago

Once you have instrumented the code, you have to run your QUnit tests, but pointing at the instrumented code (instead of the original code). So you may have to temporarily edit your QUnit tests, or copy your tests to the target directory as well so that they point at the instrumented code.

hiteshsardana99 commented 7 years ago

it means , In filesystem mode , unit testing is not performed . For unit testing , i have to use instrumented code rather than original code . I have another doubt , again why we are using instrumented code for unit testing and why we cannot use original code ? And what changes i have to made in Qunit file.

hiteshsardana99 commented 7 years ago

And one more thing , after performing unit testing on instrumented file . Will it effect or make some changes in instrument file or not?

tntim96 commented 7 years ago

why we are using instrumented code for unit testing and why we cannot use original code

Pretty much all coverage tools work this way. That's how they collected coverage data.

And what changes i have to made in Qunit file.

Either move it so it loads the instrumented files, or change it to point to the instrumented files.

hiteshsardana99 commented 7 years ago

Hey , thank you so much , your suggestions help me a lot .

Now i am facing one problem and again i need your suggestion

I have set of modules and in one module there are lot of js that need to be tested by JSCover Actually , I am facing one issue , whenever i try to generate the instrument code of all the modules and then i do unit testing , it will not work fine but whenever i ll remove one module ( lets name that mode 'X module' that is creating problem ) after that again i will generate instrument code of rest of the modules and do unit testing , its work fine .

If alone , i ll try to generate instrument code of that X module and do unit testing ,its work fine . I am confused why JSCover showing different behavior with same code or module .

After that i looked out , which line is creating problem in instrumented code of X module . I fount this below line _$jscoverage['/js/notification.js'].branchData['182'][1].init(10, 65, 'this._emailAddressTextBox && this._emailAddressTextBox.collection');

And i have compared the files of X module , when it is working alone and when it is not working with rest of the modules , i found that visit function number is different by 1 in both of the files .

for eg In file of X module that is working , visit function no is function visit178_182_1(result) {

In file of X module that is not working , visit function no is function visit179_182_1(result)

In every file visit function no is different by 1 only .

I do not think , it is creating problem but i have one doubt , If both the modules ( one is working and other is not working) are having same files and same code than why visit number is different by 1 ? It should be same .

When i am generating instrument code of all the modules , i am doing it one by one . I mean one module is not effecting other module . I am doing it separate .

Please give your valuable suggestion .

tntim96 commented 7 years ago

i found that visit function number is different by 1 in both of the files

That probably isn't the problem, as long as it has changed the same way everywhere in the file (which is expected).

why visit number is different by 1

JSCover generates a unique function with three numbers, a unique ID which is incremented, the line number and the condition number for that line. The first number will vary for one file depending on whether it is instrumented together or alone, so it is probably better to instrument everything together. You can still do this but load your instrumented code separately.

If alone , i ll try to generate instrument code of that X module and do unit testing ,its work fine

Instrumenting code will make it run more slowly. Is it possible it is a timing issue? If not, can you create some isolated code that exhibits the problem and share it?

hiteshsardana99 commented 7 years ago

yesterday , i figure out what error exactly i was facing .

In one instrumented js file , error is coming

error

_$jscoverage['/js/notification.js'].branchData['182'][1] is undefined

( i have marked that perticular line , where error is showing . Because of that error , my browser is crashing in unit testing .

I found that control is not going in the if condition where $jscoverage['/js/notification.js'].branchData is supposed to be initialize . Before if condition , i have checked the value of this $jscoverage['/js/notification.js'].branchData , its showing [ object object ] . it means already initialize . After that i have added one instrument line manually before if condition .

_$jscoverage['/js/notification.js'].branchData =null

Than its start working fine .

But its wrong , i want that it should work automatically . Every time i dont want to waste my time . i perticular file . What exactly i am facing the issue ?

As i told you , i have set of modules . I am generating instrumented code of those modules separately in other location and after that placing that instrument code at same location , where original code is .

Yes i am facing timing issue , it is taking lot of time in generating instrument code .

hiteshsardana99 commented 7 years ago

I want to know one thing , how JSCover utilizing or using memory . Because i have two modules Lets say one is X module and other is Y module , both the modules are having same name file , not same content. Path of X module is ../X/js/oneFile.js Path of Y module is ../Y/js/oneFile.js

As i have seen your code , i think your code is making some list and in that list , it is saving content like [js/oneFile] at this perticular location . I am not sure , may be i am facing this issue just because of this problem. Beacuse after adding that line ( _$jscoverage['/js/notification.js'].branchData =null ) in js file of X module , again i have faced same issue in Y module . I did same thing with Y module also . Its start working fine . As i told you , both the module are having same name file .

tntim96 commented 7 years ago

Yes, this looks like the problem, but I haven't replicated it yet, so am not sure whether the path is actually the same, or whether JSCover has a problem building paths. Looking into this now...

tntim96 commented 7 years ago

JSCover records the path as seen by the browser. If it is coming out as /js/notification.js instead of /X/js/oneFile.js, it's because that is the path seen by the browser when you run the test.

You can either introduce the 'X' into you path, or rename the modules so there is no clash.

If you can think of something specific, reasonable and reusable for JSCover to do to avoid the clash, I'd be happy to try to implement it.

hiteshsardana99 commented 7 years ago

ohk i got it ..

Now i have generated the instrument code of all the modules together , its working fine because now its taking path /X/js/oneFile.js .

bdw thank you so much for your valuable suggestion .

tntim96 commented 7 years ago

Great!